Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactor and some other bits #32

Merged
merged 4 commits into from
Jul 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
release:
python setup.py sdist bdist_wheel upload
@python setup.py sdist bdist_wheel upload
.PHONY: release

testrepl:
@python bin/testrepl.py repl
.PHONY: testrepl
25 changes: 25 additions & 0 deletions bin/testrepl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import click
from click_repl import register_repl


@click.group()
def cli():
pass


@cli.command()
@click.option("--baz", is_flag=True)
def foo(baz):
print("Foo!")


@cli.command()
@click.option("--foo", is_flag=True)
def bar(foo):
print("Bar!")


register_repl(cli)

if __name__ == "__main__":
cli()
34 changes: 23 additions & 11 deletions click_repl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ def get_completions(self, document, complete_event=None):
yield item


def bootstrap_prompt(prompt_kwargs, group):
"""
Bootstrap prompt_toolkit kwargs or use user defined values.

:param prompt_kwargs: The user specified prompt kwargs.
"""
prompt_kwargs = prompt_kwargs or {}

defaults = {
'history': InMemoryHistory(),
'complete': ClickCompleter(group),
'message': u'> ',
}

for key in defaults:
default_value = defaults[key]
if key not in prompt_kwargs:
prompt_kwargs[key] = default_value

return prompt_kwargs


def repl(
old_ctx,
prompt_kwargs=None,
Expand Down Expand Up @@ -148,18 +170,8 @@ def repl(
available_commands.pop(repl_command_name, None)

if isatty:
prompt_kwargs = prompt_kwargs or {}
# don't set 'message' if caller defines 'get_prompt_tokens'
if not prompt_kwargs.get('get_prompt_tokens', None):
prompt_kwargs.setdefault('message', u'> ')
history = prompt_kwargs.pop('history', None) \
or InMemoryHistory()
completer = prompt_kwargs.pop('completer', None) \
or ClickCompleter(group)

def get_command():
return prompt(completer=completer, history=history,
**prompt_kwargs)
return prompt(**bootstrap_prompt(prompt_kwargs, group))
else:
get_command = sys.stdin.readline

Expand Down