Skip to content

Commit 487c5c9

Browse files
committed
added ability to add contexts/state to commands
1 parent a02699f commit 487c5c9

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

awsshell/app.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def __init__(self, completer, model_completer, docs,
234234
self._dot_cmd = DotCommandHandler()
235235
self._env = os.environ.copy()
236236
self._profile = None
237+
self.prompt_tokens = u'aws> '
237238
self._input = input
238239
self._output = output
239240

@@ -282,7 +283,10 @@ def run(self):
282283
while True:
283284
try:
284285
document = self.cli.run(reset_current_buffer=True)
285-
text = document.text
286+
if self.model_completer.context and isinstance(self.model_completer.context, list):
287+
text = " ".join(self.model_completer.context) + " " + document.text
288+
else:
289+
text = document.text
286290
except InputInterrupt:
287291
pass
288292
except (KeyboardInterrupt, EOFError):
@@ -300,6 +304,25 @@ def run(self):
300304
if text.startswith('!'):
301305
# Then run the rest as a normally shell command.
302306
full_cmd = text[1:]
307+
elif text.startswith('@') and len(text.split()) == 1:
308+
# Add word as context to completions
309+
self.model_completer.context.append(text.split()[0].strip('@'))
310+
self.model_completer.reset()
311+
self.prompt_tokens = u'aws ' + ' '.join(self.model_completer.context) + u' > '
312+
self.refresh_cli = True
313+
self.cli.request_redraw()
314+
continue
315+
elif 'exit' in text.split():
316+
# Remove most recently added context
317+
if self.model_completer.context:
318+
self.model_completer.context.pop()
319+
if self.model_completer.context:
320+
self.prompt_tokens = u'aws ' + ' '.join(self.model_completer.context) + u' > '
321+
else:
322+
self.prompt_tokens = u'aws > '
323+
self.refresh_cli = True
324+
self.cli.request_redraw()
325+
continue
303326
else:
304327
full_cmd = 'aws ' + text
305328
self.history.append(full_cmd)
@@ -331,7 +354,7 @@ def create_layout(self, display_completions_in_columns, toolbar):
331354
if self.config_section['theme'] == 'none':
332355
lexer = None
333356
return create_default_layout(
334-
self, u'aws> ', lexer=lexer, reserve_space_for_menu=True,
357+
self, self.prompt_tokens, lexer=lexer, reserve_space_for_menu=True,
335358
display_completions_in_columns=display_completions_in_columns,
336359
get_bottom_toolbar_tokens=toolbar.handler)
337360

awsshell/autocomplete.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, index_data, match_fuzzy=True):
2424
# This will get populated as a command is completed.
2525
self.cmd_path = [self._current_name]
2626
self.match_fuzzy = match_fuzzy
27+
self.context = []
2728
self._cache_all_args = []
2829

2930
@property
@@ -43,6 +44,15 @@ def reset(self):
4344
self._last_position = 0
4445
self.last_option = ''
4546
self.cmd_path = [self._current_name]
47+
for context in self.context:
48+
next_command = self._current['children'].get(context)
49+
if not next_command:
50+
self.context.remove(context)
51+
self.reset()
52+
return
53+
self._current = next_command
54+
self._current_name = context
55+
self.cmd_path.append(self._current_name)
4656
self._cache_all_args = []
4757

4858
def autocomplete(self, line):

tests/unit/test_autocomplete.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,31 @@ def test_global_arg_metadata_property(index_data):
380380
}
381381
completer = AWSCLIModelCompleter(index_data)
382382
assert '--global1' in completer.global_arg_metadata
383+
384+
def test_add_context_changes_context(index_data):
385+
index_data['aws']['commands'] = ['ec2']
386+
index_data['aws']['children'] = {
387+
'ec2': {
388+
'commands': ['create-tags'],
389+
'argument_metadata': {},
390+
'arguments': [],
391+
'children': {
392+
'create-tags': {
393+
'commands': [],
394+
'argument_metadata': {
395+
'--resources': {'example': '', 'minidoc': 'foo'},
396+
},
397+
'arguments': ['--resources'],
398+
'children': {},
399+
}
400+
}
401+
}
402+
}
403+
completer = AWSCLIModelCompleter(index_data)
404+
completer.reset()
405+
completer.autocomplete('c')
406+
assert completer.autocomplete('c') == ['ec2']
407+
completer.context = ['ec2']
408+
completer.reset()
409+
completer.autocomplete('c')
410+
assert completer.autocomplete('c') == ['create-tags']

0 commit comments

Comments
 (0)