diff --git a/openad/app/main.py b/openad/app/main.py index 578ad34b..1b2bd0e1 100644 --- a/openad/app/main.py +++ b/openad/app/main.py @@ -511,6 +511,7 @@ def preloop(self): if readline and os.path.exists(self.histfile): # note history files can get corrupted so using try to compensate try: + readline.clear_history() readline.read_history_file(self.histfile) except Exception: # pylint: disable=broad-exception-caught # do not need to know exception # Create history file in case it doesn't exist yet. @@ -519,16 +520,29 @@ def preloop(self): # >> create new workspace foobar # >> ctrl+c # (Reboot) + print(readline.get_current_history_length()) + readline.set_history_length(self.histfile_size) readline.write_history_file(self.histfile) def postloop(self): """CMD class called function: Post loop is called by cmd to get an update the history file""" readline.set_history_length(self.histfile_size) - readline.write_history_file(self.histfile) + try: + readline.read_history_file(self.histfile) + readline.set_history_length(self.histfile_size) + readline.write_history_file(self.histfile) + except: + print(readline.get_current_history_length()) + readline.clear_history() + readline.set_history_length(self.histfile_size) + readline.write_history_file(self.histfile) def add_history(self, inp): """CMD class called function: adds history file""" - readline.add_history(inp) + inp = str(inp) + + if len(str(str(inp).strip())) < int(4096): + readline.add_history(str(str(inp).strip())) def complete(self, text, state): """CMD class called function: @@ -1068,12 +1082,14 @@ def api_remote( magic_prompt.api_variables = api_var_list # We now manage history. The history sometimes gets corrupted through no fault of ours. # If so, we just reset it. - try: - readline.read_history_file(magic_prompt.histfile) - except Exception: # pylint: disable=broad-exception-caught # could be a number of errors - readline.add_history("") - readline.write_history_file(magic_prompt.histfile) - readline.read_history_file(magic_prompt.histfile) + # try: + # readline.clear_history() + # readline.read_history_file(magic_prompt.histfile) + # except Exception: # pylint: disable=broad-exception-caught # could be a number of errors + # readline.add_history("") + # readline.write_history_file(magic_prompt.histfile) + # readline.clear_history() + # readline.read_history_file(magic_prompt.histfile) for i in arguments: inp = inp + a_space + i a_space = " " @@ -1105,9 +1121,11 @@ def api_remote( # Note, may be possible add code completion here #revisit else: magic_prompt.preloop() - magic_prompt.add_history(inp) + if len(str(inp.strip())) < int(4096): + magic_prompt.add_history(inp) magic_prompt.postloop() - readline.write_history_file(magic_prompt.histfile) + # Not required as post loop writes history file + # readline.write_history_file(magic_prompt.histfile) result = magic_prompt.default(inp) @@ -1170,8 +1188,10 @@ def cmd_line(): command_line.settings["workspace"] == words[1 + word_increment].upper() and command_line.settings["context"] == words[2 + word_increment].upper() ): + command_line.preloop() - command_line.add_history(str(" ".join(words[3 + word_increment :])).strip()) + if len(str(" ".join(words[3 + word_increment :])).strip()) < int(4096): + command_line.add_history(str(" ".join(words[3 + word_increment :])).strip()) command_line.postloop() result = command_line.default(str(" ".join(words[3 + word_increment :])).strip()) else: @@ -1179,7 +1199,8 @@ def cmd_line(): # Note, may be possible add code completion here #revisit command_line.preloop() - command_line.add_history(inp[+increment:].strip()) + if len(inp[+increment:].strip()) < int(4096): + command_line.add_history(inp[+increment:].strip()) command_line.postloop() result = command_line.default(inp[+increment:].strip()) command_line.do_exit("dummy do not remove") diff --git a/openad/core/lang_runs.py b/openad/core/lang_runs.py index 9efdb838..27232165 100644 --- a/openad/core/lang_runs.py +++ b/openad/core/lang_runs.py @@ -23,6 +23,7 @@ def _create_workspace_dir_if_nonexistent(cmd_pointer, dir_name): def save_run(cmd_pointer, parser): """Saves a Run""" _create_workspace_dir_if_nonexistent(cmd_pointer, "_runs") + readline.set_history_length(self.histfile_size) readline.write_history_file(cmd_pointer.histfile) # f =_meta_workspaces+'/'+ cmd_pointer.settings['workspace'].upper()+'/.cmd_history' diff --git a/openad/core/lang_workspaces.py b/openad/core/lang_workspaces.py index aa34894e..1f67152a 100644 --- a/openad/core/lang_workspaces.py +++ b/openad/core/lang_workspaces.py @@ -21,6 +21,7 @@ # Sets the current workspace from the fgiven workspaces available def set_workspace(cmd_pointer, parser): """Sets the current Workspace""" + readline.set_history_length(cmd_pointer.histfile_size) readline.write_history_file(cmd_pointer.histfile) current_workspace_name = cmd_pointer.settings["workspace"].upper() new_workspace_name = parser["Workspace_Name"].upper() @@ -33,14 +34,14 @@ def set_workspace(cmd_pointer, parser): cmd_pointer.settings["workspace"] = new_workspace_name write_registry(cmd_pointer.settings, cmd_pointer) cmd_pointer.histfile = os.path.expanduser(cmd_pointer.workspace_path(new_workspace_name) + "/.cmd_history") - readline.clear_history() - try: # Open history file if not corrupt if readline and os.path.exists(cmd_pointer.histfile): + readline.clear_history() readline.read_history_file(cmd_pointer.histfile) except Exception: + readline.set_history_length(cmd_pointer.histfile_size) readline.write_history_file(cmd_pointer.histfile) - + readline.set_history_length(cmd_pointer.histfile_size) readline.write_history_file(cmd_pointer.histfile) return output_success(msg("success_workspace_set", new_workspace_name)) @@ -132,6 +133,7 @@ def remove_workspace(cmd_pointer, parser): def create_workspace(cmd_pointer, parser): """Creates a Workspace""" # Make sure existing workspace history file is saved. + readline.set_history_length(cmd_pointer.histfile_size) readline.write_history_file(cmd_pointer.histfile) cmd_pointer.refresh_vector = True cmd_pointer.refresh_train = True @@ -206,7 +208,7 @@ def create_workspace(cmd_pointer, parser): write_registry(cmd_pointer.settings, cmd_pointer, True) write_registry(cmd_pointer.settings, cmd_pointer) - readline.clear_history() + readline.set_history_length(cmd_pointer.histfile_size) readline.write_history_file(cmd_pointer.histfile) # raise ValueError('This is a test error.\n') @later this causes the app to break permamenently. except Exception as err: