Skip to content

Commit ed7b281

Browse files
committed
Small shell improvements (slcli shell)
* Adds descriptions to autocomplete list commands and options * Adds autosuggestion to slcli shell * Enable mouse support for slcli shell
1 parent 14d710e commit ed7b281

5 files changed

Lines changed: 28 additions & 20 deletions

File tree

SoftLayer/CLI/core.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ def __init__(self, *path, **attrs):
4444
click.MultiCommand.__init__(self, **attrs)
4545
self.path = path
4646

47-
def list_commands(self, ctx):
48-
"""Get module for click."""
47+
def list_commands(self, ctx, **kwargs):
48+
"""List all sub-commands."""
4949
env = ctx.ensure_object(environment.Environment)
5050
env.load()
51-
return sorted(env.list_commands(*self.path))
51+
52+
commands = env.list_commands(*self.path)
53+
54+
if kwargs.get('with_aliases', False) and len(self.path) == 0:
55+
commands.extend(env.aliases.keys())
56+
57+
return sorted(commands)
5258

5359
def get_command(self, ctx, name):
5460
"""Get command for click."""

SoftLayer/CLI/routes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@
198198
'vm': 'vs',
199199
'hardware': 'server',
200200
'hw': 'server',
201-
'bmetal': 'bmc',
202201
'virtual': 'vs',
203202
'lb': 'loadbal',
204203
}

SoftLayer/shell/completer.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
:license: MIT, see LICENSE for more details.
77
"""
8+
import itertools
89
import shlex
910

1011
import click
@@ -27,30 +28,29 @@ def _click_autocomplete(root, text):
2728
try:
2829
parts = shlex.split(text)
2930
except ValueError:
30-
return []
31+
raise StopIteration
3132

3233
location, incomplete = _click_resolve_command(root, parts)
3334

3435
if not text.endswith(' ') and not incomplete and text:
35-
return []
36+
raise StopIteration
3637

37-
options = []
3838
if incomplete and not incomplete[0:2].isalnum():
3939
for param in location.params:
4040
if not isinstance(param, click.Option):
4141
continue
42-
options.extend(param.opts)
43-
options.extend(param.secondary_opts)
42+
for opt in itertools.chain(param.opts, param.secondary_opts):
43+
if opt.startswith(incomplete):
44+
yield completion.Completion(opt, -len(incomplete),
45+
display_meta=param.help)
46+
4447
elif isinstance(location, (click.MultiCommand, click.core.Group)):
45-
options.extend(location.list_commands(click.Context(location)))
46-
47-
# collect options that starts with the incomplete section
48-
completions = []
49-
for option in options:
50-
if option.startswith(incomplete):
51-
completions.append(
52-
completion.Completion(option, -len(incomplete)))
53-
return completions
48+
ctx = click.Context(location)
49+
for command in location.list_commands(ctx):
50+
if command.startswith(incomplete):
51+
cmd = location.get_command(ctx, command)
52+
yield completion.Completion(command, -len(incomplete),
53+
display_meta=cmd.short_help)
5454

5555

5656
def _click_resolve_command(root, parts):

SoftLayer/shell/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import traceback
1414

1515
import click
16+
from prompt_toolkit import auto_suggest as p_auto_suggest
1617
from prompt_toolkit import history as p_history
1718
from prompt_toolkit import shortcuts as p_shortcuts
1819

@@ -52,10 +53,12 @@ def cli(ctx, env):
5253

5354
while True:
5455
try:
55-
line = p_shortcuts.get_input(
56+
line = p_shortcuts.prompt(
5657
u"(%s)> " % env.vars['last_exit_code'],
5758
completer=complete,
5859
history=history,
60+
auto_suggest=p_auto_suggest.AutoSuggestFromHistory(),
61+
mouse_support=True,
5962
)
6063
try:
6164
args = shlex.split(line)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
'prettytable >= 0.7.0',
3636
'click >= 5',
3737
'requests >= 2.7.0',
38-
'prompt_toolkit',
38+
'prompt_toolkit >= 0.53',
3939
],
4040
keywords=['softlayer', 'cloud'],
4141
classifiers=[

0 commit comments

Comments
 (0)