from kivy.uix.label import Label from kivy.core.window import Window from kivy.graphics import Color,Rectangle class Command_prompt(Label): def __init__(self,app,**kwargs): # app is used to share information between widgets self.app=app # insert mode self.insert=False # cursor position self.cursor=0 self.cursor_width=9 # the text, with no color information self.raw_text="" # array of command with arguments self.argv=[] # init Label super(Command_prompt,self).__init__(**kwargs) self.keyboard = Window.request_keyboard(None,self,"text") self.keyboard.bind(on_textinput=self.on_textinput,on_key_down=self.on_key_down) self.draw() def draw(self): with self.canvas.before: # background Color(0,0,0) Rectangle(pos=self.pos,size=self.size) # cursor Color(1,1,1) Rectangle(pos=(self.pos[0]+self.cursor*self.cursor_width,self.pos[1]),size=(self.cursor_width,self.height)) # make text under cursor black if self.cursor0 and self.cursor==len(self.raw_text)) or (n<0 and self.cursor==0): return self.cursor=max(1,min(len(self.raw_text),self.cursor+n)) self.draw() # move cursor to absolute position 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