Skip to content

Commit

Permalink
Merged PR#65 and click-contrib#69
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostOps77 authored Jan 31, 2023
1 parent 3dc44d9 commit 2ae1ac6
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions click_repl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class ClickExit(RuntimeError):
text_type = str # noqa


__version__ = "0.2.0"
__version__ = "0.2.1"

_internal_commands = dict()
_internal_commands = {}


def _register_internal_command(names, target, description=None):
Expand Down Expand Up @@ -89,8 +89,9 @@ def _help_internal():


class ClickCompleter(Completer):
def __init__(self, cli):
def __init__(self, cli, ctx=None):
self.cli = cli
self.ctx = ctx

def get_completions(self, document, complete_event=None):
# Code analogous to click._bashcomplete.do_complete
Expand Down Expand Up @@ -123,15 +124,36 @@ def get_completions(self, document, complete_event=None):
return

choices = []
param_choices = []
param_called = False
autocomplete_ctx = self.ctx or ctx

for param in ctx.command.params:
if isinstance(param, click.Option):
if getattr(param, 'hidden', False):
continue
for options in (param.opts, param.secondary_opts):
for o in options:
choices.append(
Completion(
text_type(o), -len(incomplete), display_meta=param.help
)
)

# We want to make sure if this parameter was called
if o in args[param.nargs*-1:]:
param_called = True

if param_called and hasattr(param, "autocompletion") and param.autocompletion is not None:
for autocomplete in param.autocompletion(autocomplete_ctx, args, incomplete):
if isinstance(autocomplete, tuple):
param_choices.append(Completion(
text_type(autocomplete[0]), -len(incomplete),
display_meta=autocomplete[1]
))
else:
param_choices.append(Completion(text_type(autocomplete), -len(incomplete)))

elif isinstance(param, click.Argument):
if isinstance(param.type, click.Choice):
for choice in param.type.choices:
Expand All @@ -140,6 +162,8 @@ def get_completions(self, document, complete_event=None):
if isinstance(ctx.command, click.MultiCommand):
for name in ctx.command.list_commands(ctx):
command = ctx.command.get_command(ctx, name)
if getattr(command, 'hidden', False):
continue
choices.append(
Completion(
text_type(name),
Expand All @@ -148,12 +172,18 @@ def get_completions(self, document, complete_event=None):
)
)


# If we are inside a parameter that was called, we want to show only
# relevant choices
if param_called:
choices = param_choices

for item in choices:
if item.text.startswith(incomplete):
yield item


def bootstrap_prompt(prompt_kwargs, group):
def bootstrap_prompt(prompt_kwargs, group, ctx=None):
"""
Bootstrap prompt_toolkit kwargs or use user defined values.
Expand All @@ -163,7 +193,7 @@ def bootstrap_prompt(prompt_kwargs, group):

defaults = {
"history": InMemoryHistory(),
"completer": ClickCompleter(group),
"completer": ClickCompleter(group, ctx=ctx),
"message": u"> ",
}

Expand Down Expand Up @@ -209,9 +239,9 @@ def repl( # noqa: C901
}
else:
available_commands = group_ctx.command.commands
available_commands.pop(repl_command_name, None)
original_command = available_commands.pop(repl_command_name, None)

prompt_kwargs = bootstrap_prompt(prompt_kwargs, group)
prompt_kwargs = bootstrap_prompt(prompt_kwargs, group, ctx=group_ctx)
session = PromptSession(**prompt_kwargs)

if isatty:
Expand Down Expand Up @@ -267,6 +297,9 @@ def get_command():
except ExitReplException:
break

if original_command is not None:
available_commands[repl_command_name] = original_command


def register_repl(group, name="repl"):
"""Register :func:`repl()` as sub-command *name* of *group*."""
Expand Down

0 comments on commit 2ae1ac6

Please sign in to comment.