Relative cursor movement

This commit is contained in:
Ian Jauslin 2021-10-09 16:43:46 -04:00
parent 7ad1a56627
commit c9c0bcbc51

View File

@ -48,9 +48,13 @@ class Command_prompt(Label):
self.insert=False self.insert=False
self.set_text("") self.set_text("")
elif keycode[1]=="left": elif keycode[1]=="left":
self.move_cursor(-1) self.move_cursor_relative(-1)
elif keycode[1]=="right": elif keycode[1]=="right":
self.move_cursor_relative(1)
elif len(modifiers)==1 and modifiers[0]=="ctrl" and text=="a":
self.move_cursor(1) self.move_cursor(1)
elif len(modifiers)==1 and modifiers[0]=="ctrl" and text=="e":
self.move_cursor(len(self.raw_text))
def on_textinput(self,window,text): def on_textinput(self,window,text):
print(text) print(text)
@ -76,8 +80,13 @@ class Command_prompt(Label):
self.draw() self.draw()
# move cursor n steps to right (left if negative) # move cursor n steps to right (left if negative)
def move_cursor(self,n): def move_cursor_relative(self,n):
if n==0 or (n>0 and self.cursor==len(self.raw_text)) or (n<0 and self.cursor==0): if n==0 or (n>0 and self.cursor==len(self.raw_text)) or (n<0 and self.cursor==0):
return return
self.cursor=max(0,min(len(self.raw_text),self.cursor+n)) 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() self.draw()