From 1bbc9506c244bc5156ae7d52c2c9a04c9756f7ed Mon Sep 17 00:00:00 2001 From: Benex254 Date: Sat, 10 Aug 2024 01:12:33 +0300 Subject: [PATCH] feat(cli): try to detect shell when generating completions --- fastanime/cli/commands/completions.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/fastanime/cli/commands/completions.py b/fastanime/cli/commands/completions.py index 923e831..f7f649a 100644 --- a/fastanime/cli/commands/completions.py +++ b/fastanime/cli/commands/completions.py @@ -6,7 +6,26 @@ import click @click.option("--zsh", is_flag=True) @click.option("--bash", is_flag=True) def completions(fish, zsh, bash): - if fish: + try: + import shellingham + + try: + current_shell, _ = shellingham.detect_shell() + except shellingham.ShellDetectionFailure: + current_shell = None + except ImportError: + import os + + shell = os.environ.get("SHELL", "") + if "fish" in shell: + current_shell = "fish" + elif "zsh" in shell: + current_shell = "zsh" + elif "bash" in shell: + current_shell = "bash" + else: + current_shell = None + if fish or current_shell == "fish" and not zsh and not bash: print( """ function _fastanime_completion; @@ -28,7 +47,7 @@ end; complete --no-files --command fastanime --arguments "(_fastanime_completion)"; """ ) - elif zsh: + elif zsh or current_shell == "zsh" and not bash: print( """ #compdef fastanime @@ -73,7 +92,7 @@ else fi """ ) - elif bash: + elif bash or current_shell == "bash": print( """ _fastanime_completion() { @@ -107,4 +126,4 @@ _fastanime_completion_setup; """ ) else: - print("Specify either --zsh/--fish/--bash") + print("Could not detect shell")