Parse command

This commit is contained in:
Ian Jauslin 2021-10-15 19:36:29 -04:00
parent c9c0bcbc51
commit c04d2c62f2

View File

@ -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