Skip to content

Commit

Permalink
added ability to add contexts/state to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
chronologos committed Aug 5, 2016
1 parent a02699f commit 487c5c9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
27 changes: 25 additions & 2 deletions awsshell/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def __init__(self, completer, model_completer, docs,
self._dot_cmd = DotCommandHandler()
self._env = os.environ.copy()
self._profile = None
self.prompt_tokens = u'aws> '
self._input = input
self._output = output

Expand Down Expand Up @@ -282,7 +283,10 @@ def run(self):
while True:
try:
document = self.cli.run(reset_current_buffer=True)
text = document.text
if self.model_completer.context and isinstance(self.model_completer.context, list):
text = " ".join(self.model_completer.context) + " " + document.text
else:
text = document.text
except InputInterrupt:
pass
except (KeyboardInterrupt, EOFError):
Expand All @@ -300,6 +304,25 @@ def run(self):
if text.startswith('!'):
# Then run the rest as a normally shell command.
full_cmd = text[1:]
elif text.startswith('@') and len(text.split()) == 1:
# Add word as context to completions
self.model_completer.context.append(text.split()[0].strip('@'))
self.model_completer.reset()
self.prompt_tokens = u'aws ' + ' '.join(self.model_completer.context) + u' > '
self.refresh_cli = True
self.cli.request_redraw()
continue
elif 'exit' in text.split():
# Remove most recently added context
if self.model_completer.context:
self.model_completer.context.pop()
if self.model_completer.context:
self.prompt_tokens = u'aws ' + ' '.join(self.model_completer.context) + u' > '
else:
self.prompt_tokens = u'aws > '
self.refresh_cli = True
self.cli.request_redraw()
continue
else:
full_cmd = 'aws ' + text
self.history.append(full_cmd)
Expand Down Expand Up @@ -331,7 +354,7 @@ def create_layout(self, display_completions_in_columns, toolbar):
if self.config_section['theme'] == 'none':
lexer = None
return create_default_layout(
self, u'aws> ', lexer=lexer, reserve_space_for_menu=True,
self, self.prompt_tokens, lexer=lexer, reserve_space_for_menu=True,
display_completions_in_columns=display_completions_in_columns,
get_bottom_toolbar_tokens=toolbar.handler)

Expand Down
10 changes: 10 additions & 0 deletions awsshell/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, index_data, match_fuzzy=True):
# This will get populated as a command is completed.
self.cmd_path = [self._current_name]
self.match_fuzzy = match_fuzzy
self.context = []
self._cache_all_args = []

@property
Expand All @@ -43,6 +44,15 @@ def reset(self):
self._last_position = 0
self.last_option = ''
self.cmd_path = [self._current_name]
for context in self.context:
next_command = self._current['children'].get(context)
if not next_command:
self.context.remove(context)
self.reset()
return
self._current = next_command
self._current_name = context
self.cmd_path.append(self._current_name)
self._cache_all_args = []

def autocomplete(self, line):
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,31 @@ def test_global_arg_metadata_property(index_data):
}
completer = AWSCLIModelCompleter(index_data)
assert '--global1' in completer.global_arg_metadata

def test_add_context_changes_context(index_data):
index_data['aws']['commands'] = ['ec2']
index_data['aws']['children'] = {
'ec2': {
'commands': ['create-tags'],
'argument_metadata': {},
'arguments': [],
'children': {
'create-tags': {
'commands': [],
'argument_metadata': {
'--resources': {'example': '', 'minidoc': 'foo'},
},
'arguments': ['--resources'],
'children': {},
}
}
}
}
completer = AWSCLIModelCompleter(index_data)
completer.reset()
completer.autocomplete('c')
assert completer.autocomplete('c') == ['ec2']
completer.context = ['ec2']
completer.reset()
completer.autocomplete('c')
assert completer.autocomplete('c') == ['create-tags']

0 comments on commit 487c5c9

Please sign in to comment.