From 7ad1a566275aa59cbb50a18c2c5e261400b4b270 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Sat, 2 Oct 2021 18:08:57 -0400 Subject: [PATCH] command_prompt: Add cursor and basic cursor functionality --- command_prompt.py | 59 ++++++++++++++++++++++++++++++++++++++++++++--- jam.kv | 8 ++----- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/command_prompt.py b/command_prompt.py index 67eab68..ccba665 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -1,5 +1,6 @@ from kivy.uix.label import Label from kivy.core.window import Window +from kivy.graphics import Color,Rectangle class Command_prompt(Label): @@ -10,21 +11,73 @@ class Command_prompt(Label): # insert mode self.insert=False + # cursor position + self.cursor=0 + self.cursor_width=9 + + # the text, with no color information + self.raw_text="" + # 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(0,min(len(self.raw_text),self.cursor+n)) + self.draw() diff --git a/jam.kv b/jam.kv index 444ad73..da3cc48 100644 --- a/jam.kv +++ b/jam.kv @@ -16,14 +16,10 @@ : - canvas.before: - Color: - rgb: 0,0,0 - Rectangle: - pos: self.pos - size: self.size size_hint_y: None height: 20 + markup: True text_size: self.size font_name: "RobotoMono-Regular" + font_size: "15px"