From a7b551f4ac9653a92fc45f109c840569905fe17d Mon Sep 17 00:00:00 2001 From: Lucien A Date: Sat, 10 Jun 2023 20:51:46 +0200 Subject: [PATCH] add completion for :set command --- core/sessions.py | 49 ++++++++++++++++++++++++------------------------ core/terminal.py | 4 ++++ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/core/sessions.py b/core/sessions.py index b9c90a36..0de9d950 100644 --- a/core/sessions.py +++ b/core/sessions.py @@ -13,16 +13,12 @@ import ast import pprint -print_filters = ( - 'debug', - 'channel', - 'proxy' -) - -set_filters = ( - 'debug', - 'channel', - 'proxy' +arg_blacklist = ( + 'default_shell', + 'password', + 'command', + 'path', + 'url', ) class Session(dict): @@ -37,22 +33,26 @@ def _session_save_atexit(self): ) def print_to_user(self, module_filter = ''): - dlog.info(pprint.pformat(self)) - for mod_name, mod_value in self.items(): + for arg in self.get_stored_args(module_filter): + log.info(messages.sessions.set_s_s % arg) - if isinstance(mod_value, dict): - mod_args = mod_value.get('stored_args') + def get_stored_args(self, term = ''): + args = [] - # Is a module, print all the storable stored_arguments - for argument, arg_value in mod_args.items(): - if not module_filter or ("%s.%s" % (mod_name, argument)).startswith(module_filter): - log.info(messages.sessions.set_module_s_s_s % (mod_name, argument, arg_value)) - else: - # If is not a module, just print if matches with print_filters - if any(f for f in print_filters if f == mod_name and f.startswith(module_filter)): - log.info(messages.sessions.set_s_s % (mod_name, mod_value)) + for mod_name, mod_value in self.items(): + # Is a module, print all the storable stored_arguments + if isinstance(mod_value, dict): + for argument, arg_value in mod_value.get('stored_args', {}).items(): + path = ("%s.%s" % (mod_name, argument)) + if not term or path.startswith(term): + args.append((path, arg_value)) + # If is not a module, just print if matches with print_filters + elif mod_name not in arg_blacklist and mod_name.startswith(term): + args.append((mod_name, mod_value)) + + return args def get_connection_info(self): return template.Template(messages.sessions.connection_info).render( @@ -119,7 +119,7 @@ def unset(self, module_argument): log.info(messages.sessions.unset_module_s_s % (module_name, arg_name)) else: module_name = module_argument - if module_name not in self and module_name not in set_filters: + if module_name in arg_blacklist or module_name not in self: log.warning(messages.sessions.error_session_s_not_modified % (module_name)) else: self[module_name] = None @@ -155,7 +155,7 @@ def set(self, module_argument, value): log.info(messages.sessions.set_module_s_s_s % (module_name, arg_name, value)) else: module_name = module_argument - if module_name not in self and module_name not in set_filters: + if module_name in arg_blacklist or module_name not in self: log.warning(messages.sessions.error_session_s_not_modified % (module_name)) else: self[module_name] = value @@ -264,6 +264,7 @@ def __init__(self, url, password, volatile = False): 'debug': False, 'channel': None, 'default_shell': None, + 'proxy': None, } ) diff --git a/core/terminal.py b/core/terminal.py index 8860fea7..292519e4 100644 --- a/core/terminal.py +++ b/core/terminal.py @@ -285,6 +285,10 @@ def do_show(self, line, cmd): """Command "show" which prints session variables""" self.session.print_to_user(line) + + def complete_set(self, text, line, begidx, endidx): + if line.count(' ') < 2: + return [a[0] for a in self.session.get_stored_args(text)] def do_set(self, line, cmd): """Command "set" to set session variables."""