|
| 1 | +""" |
| 2 | +@author: Gabriele Girelli |
| 3 | +@contact: gigi.ga90@gmail.com |
| 4 | +""" |
| 5 | + |
| 6 | +import click # type: ignore |
| 7 | +import os |
| 8 | +from rich import print # type: ignore |
| 9 | +from shutil import copyfile |
| 10 | +import sys |
| 11 | + |
| 12 | +from clickomplete import __path__, __version__ |
| 13 | +from clickomplete.const import CONTEXT_SETTINGS |
| 14 | + |
| 15 | + |
| 16 | +@click.command(context_settings=CONTEXT_SETTINGS) |
| 17 | +@click.option( |
| 18 | + "--shell-type", |
| 19 | + "-s", |
| 20 | + help="shell type for which to activate autocompletion", |
| 21 | + type=click.Choice(["bash", "zsh", "fish"], case_sensitive=False), |
| 22 | + default="bash", |
| 23 | + show_default=True, |
| 24 | +) |
| 25 | +@click.option( |
| 26 | + "--regenerate", |
| 27 | + help="to regenerate autocompletion file, mainly for developers", |
| 28 | + type=click.BOOL, |
| 29 | + default=False, |
| 30 | + is_flag=True, |
| 31 | +) |
| 32 | +@click.version_option(__version__) |
| 33 | +def main(shell_type: str, regenerate: bool) -> None: |
| 34 | + user_home_path = os.path.expanduser("~") |
| 35 | + autocomplete_path = os.path.join( |
| 36 | + __path__[0], "autocomplete", f".clicko-complete.{shell_type}" |
| 37 | + ) |
| 38 | + |
| 39 | + if regenerate: |
| 40 | + regenerate_autocompletion_files(shell_type, autocomplete_path) |
| 41 | + |
| 42 | + if shell_type in ("bash", "zsh"): |
| 43 | + autocomplete_bash_or_zsh(user_home_path, autocomplete_path, shell_type) |
| 44 | + elif "fish" == shell_type: |
| 45 | + autocomplete_fish(user_home_path, autocomplete_path) |
| 46 | + |
| 47 | + print("Done. :thumbs_up: :smiley:") |
| 48 | + |
| 49 | + |
| 50 | +def regenerate_autocompletion_files(shell_type: str, autocomplete_path: str) -> None: |
| 51 | + os.system(f"_CLICKO_COMPLETE={shell_type}_source clicko > {autocomplete_path}") |
| 52 | + print(f"Regenerated {shell_type} completion file: {autocomplete_path}") |
| 53 | + |
| 54 | + |
| 55 | +def autocomplete_fish(user_home_path: str, autocomplete_path: str) -> None: |
| 56 | + destination_path = os.path.join( |
| 57 | + user_home_path, ".config/fish/completions/clicko.fish" |
| 58 | + ) |
| 59 | + |
| 60 | + if os.path.isfile(destination_path): |
| 61 | + print("Autocompletion was previously set up. Skipping.") |
| 62 | + sys.exit() |
| 63 | + |
| 64 | + copyfile( |
| 65 | + autocomplete_path, |
| 66 | + destination_path, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def autocomplete_bash_or_zsh( |
| 71 | + user_home_path: str, autocomplete_path: str, shell_type: str = "bash" |
| 72 | +) -> None: |
| 73 | + assert shell_type in ("bash", "zsh") |
| 74 | + |
| 75 | + autocompletion_string = f". {autocomplete_path} # CLICKO-AUTOCOMPLETE\n" |
| 76 | + run_command_path = os.path.join(user_home_path, f".{shell_type}rc") |
| 77 | + |
| 78 | + with open(run_command_path, "r") as OH: |
| 79 | + if autocompletion_string in OH.readlines(): |
| 80 | + print("Autocompletion was previously set up. Skipping.") |
| 81 | + sys.exit() |
| 82 | + |
| 83 | + with open(run_command_path, "a+") as OH: |
| 84 | + OH.write(f"\n{autocompletion_string}") |
0 commit comments