From 26e76a3699a74b3287e503a972e107abb2242db9 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Wed, 1 Dec 2021 14:05:19 -0500 Subject: [PATCH] split command execution to their own functions --- command_prompt.py | 116 ++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 55 deletions(-) diff --git a/command_prompt.py b/command_prompt.py index 99f28f0..170c0cb 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -295,66 +295,72 @@ class Command_prompt(Label): # parse command line self.parse_argv() + # single letter commands (which can be combined) # write if self.argv[0]=="w" or self.argv[0]=="w!" or self.argv[0]=="wq": - if len(self.argv)>2: - 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() - + self.run_write(self.argv) # edit file if self.argv[0]=="e": - if len(self.argv)>2: - self.message="error: could not open file: too many arguments -- usage: ':e '" - return - elif len(self.argv)==1: - self.message="error: could not open file: no argument found -- usage: ':e '" - 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() - + self.run_edit(self.argv) # quit if self.argv[0]=="q" or self.argv=="wq": 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 '" + return + elif len(argv)==1: + self.message="error: could not open file: no argument found -- usage: ':e '" + 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() +