Skip to content

Commit

Permalink
add completion for :set command
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanyMonk committed Jun 11, 2023
1 parent 0bae932 commit a7b551f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
49 changes: 25 additions & 24 deletions core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -264,6 +264,7 @@ def __init__(self, url, password, volatile = False):
'debug': False,
'channel': None,
'default_shell': None,
'proxy': None,
}
)

Expand Down
4 changes: 4 additions & 0 deletions core/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit a7b551f

Please sign in to comment.