split command execution to their own functions

This commit is contained in:
Ian Jauslin 2021-12-01 14:05:19 -05:00
parent 92f9561e81
commit 26e76a3699

View File

@ -295,66 +295,72 @@ class Command_prompt(Label):
# parse command line # parse command line
self.parse_argv() self.parse_argv()
# single letter commands (which can be combined)
# 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: self.run_write(self.argv)
self.message="error: could not write to file: too many arguments -- usage: ':w [path to file]'"
return
elif len(self.argv)==1:
if self.app.openfile!="":
if self.app.readonly:
self.message="error: open file is readonly"
return
self.app.painter.write(self.app.openfile)
return
else:
self.message="error: no file is open for editing, specify a path"
return
(ret,self.message)=filecheck.check_write(self.argv[1],self.argv[0]=="w!")
# add comment if no overwrite
if ret==-2:
self.message+=" (use ':w!' to overwrite)"
if ret<0:
return
self.app.openfile=self.argv[1]
self.app.readonly=False
self.app.painter.write(self.argv[1])
# update status bar
self.app.status_bar.draw()
# edit file # edit file
if self.argv[0]=="e": if self.argv[0]=="e":
if len(self.argv)>2: self.run_edit(self.argv)
self.message="error: could not open file: too many arguments -- usage: ':e <path to file>'"
return
elif len(self.argv)==1:
self.message="error: could not open file: no argument found -- usage: ':e <path to file>'"
return
# check that the file can be edited
(ret,self.message)=filecheck.check_edit(self.argv[1])
# error
if ret<0:
return
if os.path.isfile(self.argv[1]):
# read the file
self.app.painter.read(self.argv[1])
# set readonly mode
self.app.readonly=not os.access(self.argv[1],os.W_OK)
else:
# new file, reset painter
self.app.painter.reset()
self.app.readonly=False
# select openfile in app
self.app.openfile=self.argv[1]
# update status bar
self.app.status_bar.draw()
# quit # quit
if self.argv[0]=="q" or self.argv=="wq": if self.argv[0]=="q" or self.argv=="wq":
self.app.stop() self.app.stop()
# wrie to file
def run_write(self,argv):
if len(argv)>2:
self.message="error: could not write to file: too many arguments -- usage: ':w [path to file]'"
return
elif len(argv)==1:
if self.app.openfile!="":
if self.app.readonly:
self.message="error: open file is readonly"
return
self.app.painter.write(self.app.openfile)
return
else:
self.message="error: no file is open for editing, specify a path"
return
(ret,self.message)=filecheck.check_write(argv[1],argv[0]=="w!")
# add comment if no overwrite
if ret==-2:
self.message+=" (use ':w!' to overwrite)"
if ret<0:
return
self.app.openfile=argv[1]
self.app.readonly=False
self.app.painter.write(argv[1])
# update status bar
self.app.status_bar.draw()
def run_edit(self,argv):
if len(argv)>2:
self.message="error: could not open file: too many arguments -- usage: ':e <path to file>'"
return
elif len(argv)==1:
self.message="error: could not open file: no argument found -- usage: ':e <path to file>'"
return
# check that the file can be edited
(ret,self.message)=filecheck.check_edit(argv[1])
# error
if ret<0:
return
if os.path.isfile(argv[1]):
# read the file
self.app.painter.read(argv[1])
# set readonly mode
self.app.readonly=not os.access(argv[1],os.W_OK)
else:
# new file, reset painter
self.app.painter.reset()
self.app.readonly=False
# select openfile in app
self.app.openfile=argv[1]
# update status bar
self.app.status_bar.draw()