From c2a6f38004659de2b7c7c14f500f1cf7c4362ace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 31 Jan 2024 10:39:20 +0100 Subject: [PATCH 1/4] zsh: allow to eval zsh completion Allow to run `eval "$(my-prog --print-completion zsh)"` to enable completion in the current zsh environment. This change is inspired by https://github.com/pypa/pip/pull/12173 and https://github.com/pallets/click/pull/2544. --- shtab/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/shtab/__init__.py b/shtab/__init__.py index 99220e7..a34c474 100644 --- a/shtab/__init__.py +++ b/shtab/__init__.py @@ -659,7 +659,15 @@ def command_list(prefix, options): ${preamble} typeset -A opt_args -${root_prefix} "$@\"""").safe_substitute( + +if [[ $zsh_eval_context[-1] == eval ]]; then + # eval/source/. command, register function for later + compdef ${root_prefix} -N ${prog} +else + # autoload from fpath, call function directly + ${root_prefix} "$@\" +fi +""").safe_substitute( prog=prog, root_prefix=root_prefix, command_cases="\n".join(starmap(command_case, sorted(subcommands.items()))), From 004f89bddc7fa240df261b217ac5d701fb01648d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 31 Jan 2024 16:50:40 +0100 Subject: [PATCH 2/4] Update tests for eval support with zsh --- tests/test_shtab.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_shtab.py b/tests/test_shtab.py index d525d65..864fe63 100644 --- a/tests/test_shtab.py +++ b/tests/test_shtab.py @@ -106,8 +106,11 @@ def test_prog_scripts(shell, caplog, capsys): assert script_py == ["complete -o filenames -F _shtab_shtab script.py"] elif shell == "zsh": assert script_py == [ - "#compdef script.py", "_describe 'script.py commands' _commands", - "_shtab_shtab_options+=(': :_shtab_shtab_commands' '*::: :->script.py')", "script.py)"] + "#compdef script.py", + "_describe 'script.py commands' _commands", + "_shtab_shtab_options+=(': :_shtab_shtab_commands' '*::: :->script.py')", + "script.py)", + "compdef _shtab_shtab -N script.py",] elif shell == "tcsh": assert script_py == ["complete script.py \\"] else: @@ -271,7 +274,7 @@ def test_add_argument_to_positional(shell, caplog, capsys): assert exc.value.code == 0 completion, err = capsys.readouterr() print(completion) - assert completion_manual == completion.rstrip() + assert completion_manual.rstrip() == completion.rstrip() assert not err if shell == "bash": From 75232a6f2a9814f1d441c228b9f64897a7ca3993 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Wed, 31 Jan 2024 18:12:00 +0000 Subject: [PATCH 3/4] minor lint --- tests/test_shtab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shtab.py b/tests/test_shtab.py index 864fe63..6667f0e 100644 --- a/tests/test_shtab.py +++ b/tests/test_shtab.py @@ -110,7 +110,7 @@ def test_prog_scripts(shell, caplog, capsys): "_describe 'script.py commands' _commands", "_shtab_shtab_options+=(': :_shtab_shtab_commands' '*::: :->script.py')", "script.py)", - "compdef _shtab_shtab -N script.py",] + "compdef _shtab_shtab -N script.py"] elif shell == "tcsh": assert script_py == ["complete script.py \\"] else: From 6b3c4f37dcd7b8c7daa54fe7f1b5b2edf9ee69a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 31 Jan 2024 18:12:08 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_shtab.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_shtab.py b/tests/test_shtab.py index 6667f0e..de23513 100644 --- a/tests/test_shtab.py +++ b/tests/test_shtab.py @@ -106,10 +106,8 @@ def test_prog_scripts(shell, caplog, capsys): assert script_py == ["complete -o filenames -F _shtab_shtab script.py"] elif shell == "zsh": assert script_py == [ - "#compdef script.py", - "_describe 'script.py commands' _commands", - "_shtab_shtab_options+=(': :_shtab_shtab_commands' '*::: :->script.py')", - "script.py)", + "#compdef script.py", "_describe 'script.py commands' _commands", + "_shtab_shtab_options+=(': :_shtab_shtab_commands' '*::: :->script.py')", "script.py)", "compdef _shtab_shtab -N script.py"] elif shell == "tcsh": assert script_py == ["complete script.py \\"]