command_prompt: error messages in prompt, not status bar

This commit is contained in:
Ian Jauslin 2021-10-16 16:48:42 -04:00
parent bf26ef22a5
commit b9fddf4d2d
2 changed files with 42 additions and 25 deletions

View File

@ -22,6 +22,9 @@ class Command_prompt(Label):
# the text, with no color information # the text, with no color information
self.raw_text="" self.raw_text=""
# a one-time message that shows up over the bar
self.message=""
# array of command with arguments # array of command with arguments
self.argv=[] self.argv=[]
@ -38,6 +41,19 @@ class Command_prompt(Label):
def draw(self): 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 # wrap text
window_size=int(self.width/self.char_width) window_size=int(self.width/self.char_width)
if self.cursor>=self.window_offset+window_size: if self.cursor>=self.window_offset+window_size:
@ -48,11 +64,8 @@ class Command_prompt(Label):
if self.window_offset==1: if self.window_offset==1:
self.window_offset=0 self.window_offset=0
with self.canvas.before:
# background
Color(0,0,0)
Rectangle(pos=self.pos,size=self.size)
# cursor # cursor
with self.canvas.before:
Color(1,1,1) Color(1,1,1)
# wrap cursor position # 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)) 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 # process modifiers
mods=self.process_modifiers(modifiers) mods=self.process_modifiers(modifiers)
# clear status bar
self.app.status_bar.text=""
# process command # process command
if keycode[1]=="enter": if keycode[1]=="enter":
self.insert=False self.insert=False
@ -103,7 +113,8 @@ class Command_prompt(Label):
self.tab_complete() self.tab_complete()
def on_textinput(self,window,text): def on_textinput(self,window,text):
#print(text) # reset status_bar
self.app.status_bar.draw()
if self.insert: if self.insert:
self.append_text_at_cursor(text) self.append_text_at_cursor(text)
elif text==':': elif text==':':
@ -262,13 +273,13 @@ class Command_prompt(Label):
return paths[0][end:] return paths[0][end:]
else: else:
# display in status bar # display in status bar
self.app.status_bar.raw_text="" self.app.status_bar.message=""
for path in paths: for path in paths:
name=os.path.basename(path) name=os.path.basename(path)
# add quotes if needed # add quotes if needed
if " " in name: if " " in name:
name="\""+name+"\"" name="\""+name+"\""
self.app.status_bar.raw_text+=name+" " self.app.status_bar.message+=name+" "
self.app.status_bar.draw() self.app.status_bar.draw()
return os.path.commonprefix(paths)[end:] return os.path.commonprefix(paths)[end:]
@ -281,30 +292,30 @@ class Command_prompt(Label):
# write # write
if self.argv[0]=="w" or self.argv[0]=="w!" or self.argv[0]=="wq": if self.argv[0]=="w" or self.argv[0]=="w!" or self.argv[0]=="wq":
if len(self.argv)>2: 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 return
elif len(self.argv)==1: elif len(self.argv)==1:
if os.path.isfile(self.app.openfile): if os.path.isfile(self.app.openfile):
self.app.painter.write(self.app.openfile) self.app.painter.write(self.app.openfile)
else: 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 return
else: else:
# check that the file is not a directory # check that the file is not a directory
if os.path.isdir(self.argv[1]): 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 return
# check that file does not already exist # 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]): 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 return
# check that the containing directory exists # 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])): 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 return
# check permissions # 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)): 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 return
else: else:
self.app.painter.write(self.argv[1]) self.app.painter.write(self.argv[1])
@ -312,23 +323,23 @@ class Command_prompt(Label):
# open file # open file
if self.argv[0]=="e": if self.argv[0]=="e":
if len(self.argv)>2: if len(self.argv)>2:
self.app.status_bar.set_text("error: could not open file: too many arguments -- usage: ':e <path to file>'") self.message="error: could not open file: too many arguments -- usage: ':e <path to file>'"
return return
elif len(self.argv)==1: elif len(self.argv)==1:
self.app.status_bar.set_text("error: could not open file: no argument found -- usage: ':e <path to file>'") self.message="error: could not open file: no argument found -- usage: ':e <path to file>'"
return return
else: else:
# check that the file is not a directory # check that the file is not a directory
if os.path.isdir(self.argv[1]): 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 return
# check that the file exists # check that the file exists
elif not os.path.isfile(self.argv[1]): 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 return
# check permissions # check permissions
elif not os.access(self.argv[1],os.R_OK): 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 return
else: else:
# select openfile # select openfile

View File

@ -14,6 +14,9 @@ class Status_bar(Label):
# unformatted text # unformatted text
self.raw_text="" self.raw_text=""
# a one-time message that shows up over the bar
self.message=""
# init Label # init Label
super(Status_bar,self).__init__(**kwargs) super(Status_bar,self).__init__(**kwargs)
@ -22,8 +25,11 @@ class Status_bar(Label):
self.draw() self.draw()
def draw(self): 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 # do not wrap
if len(self.raw_text)<self.width/self.char_width: self.text=self.raw_text[:min(len(self.raw_text),int(self.width/self.char_width))]
self.text=self.raw_text
else:
self.text=self.raw_text[:int(self.width/self.char_width)]