Skip to content

Commit 14ceb80

Browse files
committed
added ability to add contexts/state to commands
1 parent 1e1891a commit 14ceb80

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
@@ -233,6 +233,7 @@ def __init__(self, completer, model_completer, docs):
233233
self._dot_cmd = DotCommandHandler()
234234
self._env = os.environ.copy()
235235
self._profile = None
236+
self.prompt_tokens = u'aws> '
236237

237238
# These attrs come from the config file.
238239
self.config_obj = None
@@ -279,7 +280,10 @@ def run(self):
279280
while True:
280281
try:
281282
document = self.cli.run(reset_current_buffer=True)
282-
text = document.text
283+
if self.model_completer.context and isinstance(self.model_completer.context, list):
284+
text = " ".join(self.model_completer.context) + " " + document.text
285+
else:
286+
text = document.text
283287
except InputInterrupt:
284288
pass
285289
except (KeyboardInterrupt, EOFError):
@@ -297,6 +301,25 @@ def run(self):
297301
if text.startswith('!'):
298302
# Then run the rest as a normally shell command.
299303
full_cmd = text[1:]
304+
elif text.startswith('@') and len(text.split()) == 1:
305+
# Add word as context to completions
306+
self.model_completer.context.append(text.split()[0].strip('@'))
307+
self.model_completer.reset()
308+
self.prompt_tokens = u'aws ' + ' '.join(self.model_completer.context) + u' > '
309+
self.refresh_cli = True
310+
self.cli.request_redraw()
311+
continue
312+
elif 'exit' in text.split():
313+
# Remove most recently added context
314+
if self.model_completer.context:
315+
self.model_completer.context.pop()
316+
if self.model_completer.context:
317+
self.prompt_tokens = u'aws ' + ' '.join(self.model_completer.context) + u' > '
318+
else:
319+
self.prompt_tokens = u'aws > '
320+
self.refresh_cli = True
321+
self.cli.request_redraw()
322+
continue
300323
else:
301324
full_cmd = 'aws ' + text
302325
self.history.append(full_cmd)
@@ -328,7 +351,7 @@ def create_layout(self, display_completions_in_columns, toolbar):
328351
if self.config_section['theme'] == 'none':
329352
lexer = None
330353
return create_default_layout(
331-
self, u'aws> ', lexer=lexer, reserve_space_for_menu=True,
354+
self, self.prompt_tokens, lexer=lexer, reserve_space_for_menu=True,
332355
display_completions_in_columns=display_completions_in_columns,
333356
get_bottom_toolbar_tokens=toolbar.handler)
334357

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)