From 0b56326fe9090d8b766736a9bc81a196dd962a38 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sun, 2 Jul 2017 00:35:41 +0100 Subject: [PATCH 1/4] Refactor and split up. More readable IMHO. --- click_repl/__init__.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/click_repl/__init__.py b/click_repl/__init__.py index 7af0362..40377ae 100644 --- a/click_repl/__init__.py +++ b/click_repl/__init__.py @@ -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, @@ -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 From 40732998f384830b779078de4d62e96dd6bd4a02 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sun, 2 Jul 2017 00:37:28 +0100 Subject: [PATCH 2/4] Mark `release` target as a PHONY. --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index d257e7b..38ff3fb 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,3 @@ release: python setup.py sdist bdist_wheel upload +.PHONY: release From 62abd0083eb4048282da6034abbe1d857ed21a58 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sun, 2 Jul 2017 00:37:49 +0100 Subject: [PATCH 3/4] Add example REPL for fast testing. --- Makefile | 4 ++++ bin/testrepl.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 bin/testrepl.py diff --git a/Makefile b/Makefile index 38ff3fb..f1d62eb 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,7 @@ release: python setup.py sdist bdist_wheel upload .PHONY: release + +testrepl: + python bin/testrepl.py repl +.PHONY: testrepl diff --git a/bin/testrepl.py b/bin/testrepl.py new file mode 100644 index 0000000..87eba81 --- /dev/null +++ b/bin/testrepl.py @@ -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() From f4de8d1b81a38c94bace675a592d40c701e0e8b6 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sun, 2 Jul 2017 00:52:37 +0100 Subject: [PATCH 4/4] Silence outputs for targets. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index f1d62eb..b563b2f 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +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 + @python bin/testrepl.py repl .PHONY: testrepl