From b9fddf4d2d1cf7b5f77b8c1aa5a6e493d8097b53 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Sat, 16 Oct 2021 16:48:42 -0400 Subject: [PATCH] command_prompt: error messages in prompt, not status bar --- command_prompt.py | 53 ++++++++++++++++++++++++++++------------------- status_bar.py | 14 +++++++++---- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/command_prompt.py b/command_prompt.py index bc9e01a..8a5bcdd 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -22,6 +22,9 @@ class Command_prompt(Label): # the text, with no color information self.raw_text="" + # a one-time message that shows up over the bar + self.message="" + # array of command with arguments self.argv=[] @@ -38,6 +41,19 @@ class Command_prompt(Label): def draw(self): + + # background + with self.canvas.before: + Color(0,0,0) + Rectangle(pos=self.pos,size=self.size) + + # if message is not empty, draw message instead + if self.message!="": + # do not wrap + self.text=self.message[:min(len(self.message),int(self.width/self.char_width))] + self.message="" + return + # wrap text window_size=int(self.width/self.char_width) if self.cursor>=self.window_offset+window_size: @@ -48,11 +64,8 @@ class Command_prompt(Label): if self.window_offset==1: self.window_offset=0 + # cursor with self.canvas.before: - # background - Color(0,0,0) - Rectangle(pos=self.pos,size=self.size) - # cursor Color(1,1,1) # 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)) @@ -71,9 +84,6 @@ class Command_prompt(Label): # process modifiers mods=self.process_modifiers(modifiers) - # clear status bar - self.app.status_bar.text="" - # process command if keycode[1]=="enter": self.insert=False @@ -103,7 +113,8 @@ class Command_prompt(Label): self.tab_complete() def on_textinput(self,window,text): - #print(text) + # reset status_bar + self.app.status_bar.draw() if self.insert: self.append_text_at_cursor(text) elif text==':': @@ -262,13 +273,13 @@ class Command_prompt(Label): return paths[0][end:] else: # display in status bar - self.app.status_bar.raw_text="" + self.app.status_bar.message="" for path in paths: name=os.path.basename(path) # add quotes if needed if " " in name: name="\""+name+"\"" - self.app.status_bar.raw_text+=name+" " + self.app.status_bar.message+=name+" " self.app.status_bar.draw() return os.path.commonprefix(paths)[end:] @@ -281,30 +292,30 @@ class Command_prompt(Label): # write if self.argv[0]=="w" or self.argv[0]=="w!" or self.argv[0]=="wq": if len(self.argv)>2: - self.app.status_bar.set_text("error: could not write to file: too many arguments -- usage: ':w [path to file]'") + self.message="error: could not write to file: too many arguments -- usage: ':w [path to file]'" return elif len(self.argv)==1: if os.path.isfile(self.app.openfile): self.app.painter.write(self.app.openfile) else: - self.app.status_bar.set_text("error: no file is open for editing, specify a path") + self.message="error: no file is open for editing, specify a path" return else: # check that the file is not a directory if os.path.isdir(self.argv[1]): - self.app.status_bar.set_text("error: '"+self.argv[1]+"' is a directory") + self.message="error: '"+self.argv[1]+"' is a directory" return # check that file does not already exist elif self.argv[0]!="w!" and self.argv[1]!= self.app.openfile and os.path.isfile(self.argv[1]): - self.app.status_bar.set_text("error: '"+self.argv[1]+"' already exists (use ':w!' to overwrite)") + self.message="error: '"+self.argv[1]+"' already exists (use ':w!' to overwrite)" return # check that the containing directory exists elif len(os.path.dirname(self.argv[1]))>0 and not os.path.isdir(os.path.dirname(self.argv[1])): - self.app.status_bar.set_text("error: could not find directory '"+os.path.dirname(self.argv[1])+"'") + self.message="error: could not find directory '"+os.path.dirname(self.argv[1])+"'" return # check permissions elif (len(os.path.dirname(self.argv[1]))>0 and not os.access(os.path.dirname(self.argv[1]),os.W_OK)) or (len(os.path.dirname(self.argv[1]))==0 and not os.access(os.getcwd(),os.W_OK)): - self.app.status_bar.set_text("error: permission denied") + self.message="error: permission denied" return else: self.app.painter.write(self.argv[1]) @@ -312,23 +323,23 @@ class Command_prompt(Label): # open file if self.argv[0]=="e": if len(self.argv)>2: - self.app.status_bar.set_text("error: could not open file: too many arguments -- usage: ':e '") + self.message="error: could not open file: too many arguments -- usage: ':e '" return elif len(self.argv)==1: - self.app.status_bar.set_text("error: could not open file: no argument found -- usage: ':e '") + self.message="error: could not open file: no argument found -- usage: ':e '" return else: # check that the file is not a directory if os.path.isdir(self.argv[1]): - self.app.status_bar.set_text("error: '"+self.argv[1]+"' is a directory") + self.message="error: '"+self.argv[1]+"' is a directory" return # check that the file exists elif not os.path.isfile(self.argv[1]): - self.app.status_bar.set_text("error: could not find file '"+self.argv[1]+"'") + self.message="error: could not find file '"+self.argv[1]+"'" return # check permissions elif not os.access(self.argv[1],os.R_OK): - self.app.status_bar.set_text("error: permission denied") + self.message="error: permission denied" return else: # select openfile diff --git a/status_bar.py b/status_bar.py index 1739bf6..e69056c 100644 --- a/status_bar.py +++ b/status_bar.py @@ -14,6 +14,9 @@ class Status_bar(Label): # unformatted text self.raw_text="" + # a one-time message that shows up over the bar + self.message="" + # init Label super(Status_bar,self).__init__(**kwargs) @@ -22,8 +25,11 @@ class Status_bar(Label): self.draw() def draw(self): + # if message is not empty, draw message instead + if self.message!="": + self.text=self.message[:min(len(self.message),int(self.width/self.char_width))] + self.message="" + return + # do not wrap - if len(self.raw_text)