command prompt: wrap text

This commit is contained in:
Ian Jauslin 2021-10-16 12:16:30 -04:00
parent 360ce780ae
commit 3d1fdee7d5

View File

@ -27,6 +27,9 @@ class Command_prompt(Label):
# array of command with arguments # array of command with arguments
self.argv=[] self.argv=[]
# display characters from window_offset onwards (for text wrapping)
self.window_offset=0
# init Label # init Label
super(Command_prompt,self).__init__(**kwargs) super(Command_prompt,self).__init__(**kwargs)
@ -37,19 +40,31 @@ class Command_prompt(Label):
def draw(self): def draw(self):
# wrap text
window_size=int(self.width/self.char_width)
if self.cursor>=self.window_offset+window_size:
self.window_offset=self.cursor-window_size+1
elif self.cursor<self.window_offset:
self.window_offset=self.cursor
# do not stop right before ':'
if self.window_offset==1:
self.window_offset=0
with self.canvas.before: with self.canvas.before:
# background # background
Color(0,0,0) Color(0,0,0)
Rectangle(pos=self.pos,size=self.size) Rectangle(pos=self.pos,size=self.size)
# cursor # cursor
Color(1,1,1) Color(1,1,1)
Rectangle(pos=(self.pos[0]+self.cursor*self.char_width,self.pos[1]),size=(self.char_width,self.height)) # wrap cursor position
Rectangle(pos=(self.pos[0]+(self.cursor-self.window_offset)*self.char_width,self.pos[1]),size=(self.char_width,self.height))
# make text under cursor black # make text under cursor black
if self.cursor<len(self.raw_text): end=min(len(self.raw_text),self.window_offset+window_size)
self.text=self.raw_text[:self.cursor]+"[color=000]"+self.raw_text[self.cursor]+"[/color]"+self.raw_text[self.cursor+1:] if self.cursor<end:
self.text=self.raw_text[self.window_offset:self.cursor]+"[color=000]"+self.raw_text[self.cursor]+"[/color]"+self.raw_text[self.cursor+1:end]
else: else:
self.text=self.raw_text self.text=self.raw_text[self.window_offset:end]
def on_key_down(self, keyboard, keycode, text, modifiers): def on_key_down(self, keyboard, keycode, text, modifiers):