From c04d2c62f2fd8df2344a9981f6f90a4db524e382 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 15 Oct 2021 19:36:29 -0400 Subject: [PATCH] Parse command --- command_prompt.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/command_prompt.py b/command_prompt.py index 135cade..f048e21 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -18,6 +18,9 @@ class Command_prompt(Label): # the text, with no color information self.raw_text="" + # array of command with arguments + self.argv=[] + # init Label super(Command_prompt,self).__init__(**kwargs) @@ -42,10 +45,11 @@ class Command_prompt(Label): self.text=self.raw_text def on_key_down(self, keyboard, keycode, text, modifiers): - print(keycode,text,modifiers) + #print(keycode,text,modifiers) if self.insert: if keycode[1]=="enter": self.insert=False + self.parse_command() self.set_text("") elif keycode[1]=="left": self.move_cursor_relative(-1) @@ -57,7 +61,7 @@ class Command_prompt(Label): self.move_cursor(len(self.raw_text)) def on_textinput(self,window,text): - print(text) + #print(text) if self.insert: self.append_text_at_cursor(text) elif text==':': @@ -90,3 +94,41 @@ class Command_prompt(Label): def move_cursor(self,n): self.cursor=max(1,min(len(self.raw_text),n)) self.draw() + + # parse text as argv + def parse_command(self): + # init + self.argv=[""] + + # whether inside quotes + single_quoted=False + double_quoted=False + # whether after '\' + backslashed=False + + # start after ':' + # proceed one character at a time + for i in range(1,len(self.raw_text)): + char=self.raw_text[i] + + # split argument + if single_quoted==False and double_quoted==False and backslashed==False and char==' ': + # new argv + self.argv.append("") + + # quotes or '\' + elif double_quoted==False and backslashed==False and char=='\'': + single_quoted=not single_quoted + elif single_quoted==False and backslashed==False and char=='"': + double_quoted=not double_quoted + elif single_quoted==False and backslashed==False and char=='\\': + backslashed=True + + # write character + else: + self.argv[len(self.argv)-1]+=char + # reset backslash + backslashed=False + + +