Skip to content

Commit be3700a

Browse files
committed
Move shellcomplete out to separate module
1 parent 19c346a commit be3700a

File tree

3 files changed

+76
-53
lines changed

3 files changed

+76
-53
lines changed

awsshell/__init__.py

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88

99
from prompt_toolkit.shortcuts import get_input
1010
from prompt_toolkit.history import InMemoryHistory
11-
from prompt_toolkit.completion import Completer, Completion
1211

12+
from awsshell import shellcomplete
1313
from awsshell import autocomplete
1414
from awsshell import app
1515

1616

17-
NOOP = {'arguments': [], 'commands': [], 'children': {}}
18-
19-
2017
__version__ = '0.0.1'
2118

2219

@@ -37,61 +34,15 @@ def load_index(filename):
3734
return ast.literal_eval(f.read())
3835

3936

40-
class AWSShellCompleter(Completer):
41-
"""Completer class for the aws-shell.
42-
43-
This is the completer used specifically for the aws shell.
44-
Not to be confused with the AWSCLICompleter, which is more
45-
low level, and can be reused in contexts other than the
46-
aws shell.
47-
"""
48-
def __init__(self, completer):
49-
self._completer = completer
50-
51-
@property
52-
def completer(self):
53-
return self._completer
54-
55-
@completer.setter
56-
def completer(self, value):
57-
self._completer = value
58-
59-
def get_completions(self, document, complete_event):
60-
text_before_cursor = document.text_before_cursor
61-
word_before_cursor = ''
62-
if text_before_cursor.strip():
63-
word_before_cursor = text_before_cursor.split()[-1]
64-
completions = self._completer.autocomplete(text_before_cursor)
65-
arg_meta = self._completer.arg_metadata
66-
for completion in completions:
67-
if completion.startswith('--') and completion in arg_meta:
68-
# TODO: Need to handle merging in global options as well.
69-
meta = arg_meta[completion]
70-
if meta['required']:
71-
display_text = '%s (required)' % completion
72-
else:
73-
display_text = completion
74-
type_name = arg_meta[completion]['type_name']
75-
display_meta = '[%s] %s' % (type_name, arg_meta[completion]['minidoc'])
76-
else:
77-
display_text = completion
78-
display_meta = ''
79-
if text_before_cursor and text_before_cursor[-1] == ' ':
80-
location = 0
81-
else:
82-
location = -len(word_before_cursor)
83-
yield Completion(completion, location,
84-
display=display_text, display_meta=display_meta)
85-
86-
8737
def main():
8838
index_file = determine_index_filename()
8939
if not os.path.isfile(index_file):
9040
print("First run, creating autocomplete index...")
9141
from awsshell.makeindex import write_index
9242
write_index()
9343
index_data = load_index(index_file)
94-
completer = AWSShellCompleter(autocomplete.AWSCLICompleter(index_data))
44+
completer = shellcomplete.AWSShellCompleter(
45+
autocomplete.AWSCLICompleter(index_data))
9546
history = InMemoryHistory()
9647
shell = app.create_aws_shell(completer, history)
9748
shell.run()

awsshell/app.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,14 @@ def on_input_timeout(self, cli):
101101
buffer = cli.current_buffer
102102
document = buffer.document
103103
text = document.text
104+
command = self.completer.current_command
105+
docs = u''
106+
#if command:
107+
# output = subprocess.check_output(
108+
# self.completer.completer.cmd_path + ' help')
109+
# docs = output.decode('utf-8')
104110
cli.buffers['clidocs'].reset(
105-
initial_document=Document(text, cursor_position=0))
111+
initial_document=Document(command, cursor_position=0))
106112

107113
def create_cli_interface(self):
108114
# A CommandLineInterface from prompt_toolkit

awsshell/shellcomplete.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Autocompletion integration with python prompt toolkit.
2+
3+
This module integrates the low level autocomplete functionality
4+
provided in awsshell.autocomplete and integrates it with the
5+
interface required for autocompletion in the python prompt
6+
toolkit.
7+
8+
If you're interested in the heavy lifting of the autocompletion
9+
logic, see awsshell.autocomplete.
10+
11+
"""
12+
from prompt_toolkit.completion import Completer, Completion
13+
14+
15+
class AWSShellCompleter(Completer):
16+
"""Completer class for the aws-shell.
17+
18+
This is the completer used specifically for the aws shell.
19+
Not to be confused with the AWSCLICompleter, which is more
20+
low level, and can be reused in contexts other than the
21+
aws shell.
22+
"""
23+
def __init__(self, completer):
24+
self._completer = completer
25+
26+
@property
27+
def completer(self):
28+
return self._completer
29+
30+
@completer.setter
31+
def completer(self, value):
32+
self._completer = value
33+
34+
@property
35+
def current_command(self):
36+
return u' '.join(self._completer.cmd_path)
37+
38+
def get_completions(self, document, complete_event):
39+
text_before_cursor = document.text_before_cursor
40+
word_before_cursor = ''
41+
if text_before_cursor.strip():
42+
word_before_cursor = text_before_cursor.split()[-1]
43+
completions = self._completer.autocomplete(text_before_cursor)
44+
arg_meta = self._completer.arg_metadata
45+
for completion in completions:
46+
if completion.startswith('--') and completion in arg_meta:
47+
# TODO: Need to handle merging in global options as well.
48+
meta = arg_meta[completion]
49+
if meta['required']:
50+
display_text = '%s (required)' % completion
51+
else:
52+
display_text = completion
53+
type_name = arg_meta[completion]['type_name']
54+
display_meta = '[%s] %s' % (type_name, arg_meta[completion]['minidoc'])
55+
else:
56+
display_text = completion
57+
display_meta = ''
58+
if text_before_cursor and text_before_cursor[-1] == ' ':
59+
location = 0
60+
else:
61+
location = -len(word_before_cursor)
62+
yield Completion(completion, location,
63+
display=display_text, display_meta=display_meta)
64+
65+
66+

0 commit comments

Comments
 (0)