From 3cb67f5989139b2cdac24e9211837e4bca4288cb Mon Sep 17 00:00:00 2001 From: Edmund Rosewright Date: Sat, 25 Jun 2022 18:45:18 +0000 Subject: Add fish shell completions --- README.md | 7 ++-- completions/_colorscript | 45 +++++++++++++++++++++++++ completions/colorscript.fish | 78 ++++++++++++++++++++++++++++++++++++++++++++ zsh_completion/_colorscript | 45 ------------------------- 4 files changed, 128 insertions(+), 47 deletions(-) create mode 100644 completions/_colorscript create mode 100644 completions/colorscript.fish delete mode 100644 zsh_completion/_colorscript diff --git a/README.md b/README.md index f6f83e7..6d5ace7 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Alternately, you could also install shell-color-scripts from the AUR using an AU yay -S shell-color-scripts -# Installing shell-color-scripts on other Linux distrtibutions +# Installing shell-color-scripts on other Linux distributions Download the source code from this repository or use a git clone: @@ -31,7 +31,10 @@ Download the source code from this repository or use a git clone: sudo make uninstall # optional for zsh completion - sudo cp zsh_completion/_colorscript /usr/share/zsh/site-functions + sudo cp completions/_colorscript /usr/share/zsh/site-functions + + # optional for fish shell completion + sudo cp completions/colorscript.fish /usr/share/fish/vendor_completions.d # Usage diff --git a/completions/_colorscript b/completions/_colorscript new file mode 100644 index 0000000..0912461 --- /dev/null +++ b/completions/_colorscript @@ -0,0 +1,45 @@ +#compdef colorscript + +DIR_COLORSCRIPTS="/opt/shell-color-scripts/colorscripts" +if command -v find &>/dev/null; then + LS_CMD="$(command -v find) ${DIR_COLORSCRIPTS} -maxdepth 1 -type f | xargs -I$ basename $" +else + LS_CMD="$(command -v ls) ${DIR_COLORSCRIPTS} | xargs -I$ basename $" +fi + +function _colorscript { + local curcontext="$curcontext" state line + typeset -A opt_args + + _arguments \ + '1: :->subcmd'\ + '2: :->colorscript' + + case $state in + subcmd) + _arguments "1:subcmd:((help:'Print help'\ + list:'List all color scripts.'\ + exec:'Run a specific color script.'\ + blacklist:'Blacklist a specific color script.'\ + all:'Run all the color scripts.'\ + random:'Run a random color script.'))" + ;; + *) + case $words[2] in + "exec") + for colorscript in $(sh -c $LS_CMD); do + compadd ${colorscript} + done + ;; + "blacklist") + for colorscript in $(sh -c $LS_CMD); do + compadd ${colorscript} + done + ;; + *) + ;; + esac + esac +} + +_colorscript "$@" diff --git a/completions/colorscript.fish b/completions/colorscript.fish new file mode 100644 index 0000000..f6abc7d --- /dev/null +++ b/completions/colorscript.fish @@ -0,0 +1,78 @@ +# Completions for fish shell +# This file should go in /usr/share/fish/vendor_completions.d +# according to https://fishshell.com/docs/current/completions.html +# function + +set dir_colorscripts "/opt/shell-color-scripts/colorscripts" +set dir_blacklisted_colorscripts "$dir_colorscripts/blacklisted" + +# Lists all filenames in a given directory +function list_filenames + set dir $argv[1] + if command -q find + # use find if available + set files (command find $dir -maxdepth 1 -type f) + else if command -q ls + # use ls if available + set files (command ls $dir) + else + # cannot provide coloscript name autocompletions for args of exec, blacklist, and unblacklist + return + end + + for file in $files + echo (basename $file) + end +end + +# List all colorscript names +function cs_names + echo (list_filenames $dir_colorscripts | string collect) +end + +# List all blacklisted colorscript names +function blacklisted_cs_names + echo (list_filenames $dir_blacklisted_colorscripts | string collect) +end + +# Description text +set -l help_desc 'Print help' +set -l list_desc 'List all installed color scripts' +set -l random_desc 'Run a random color script' +set -l exec_desc 'Run a specified color script by SCRIPT NAME or INDEX' +set -l blacklist_desc 'Blacklist a color script by SCRIPT NAME or INDEX' +set -l unblacklist_desc 'Unblacklist a color script by SCRIPT NAME or INDEX' +set -l all_desc 'List the outputs of all colorscripts with their SCRIPT NAME' + +# List of all available commands and flag-style commands +set -l commands -h --help help -l --list list -r --random random -e --exec exec \ + -b --blacklist blacklist -u --unblacklist unblacklist -a --all all + +# turn off built-in file completions +complete -c colorscript -f + +# Commands autocompletions +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -a help -d "$help_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -a list -d "$list_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -a random -d "$random_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -a exec -d "$exec_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -a blacklist -d "$blacklist_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -a unblacklist -d "$unblacklist_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -a all -d "$all_desc" + +# Flag-style commands autocompletions +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -s h -l help -d "$help_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -s l -l list -d "$list_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -s r -l random -d "$random_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -s e -l exec -d "$exec_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -s b -l blacklist -d "$blacklist_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -s u -l unblacklist -d "$unblacklist_desc" +complete -c colorscript -n "not __fish_seen_subcommand_from $commands" -s a -l all -d "$all_desc" + +# Coloscript name autocompletions +set -l commands_that_take_names -e --exec exec -b --blacklist blacklist +complete -c colorscript -n "__fish_seen_subcommand_from $commands_that_take_names" -a '(cs_names)' + +# Blacklisted colorscript name autocompletions +set -l commands_that_take_blacklisted_names -u --unblacklist unblacklist +complete -c colorscript -n "__fish_seen_subcommand_from $commands_that_take_blacklisted_names" -a '(blacklisted_cs_names)' \ No newline at end of file diff --git a/zsh_completion/_colorscript b/zsh_completion/_colorscript deleted file mode 100644 index 0912461..0000000 --- a/zsh_completion/_colorscript +++ /dev/null @@ -1,45 +0,0 @@ -#compdef colorscript - -DIR_COLORSCRIPTS="/opt/shell-color-scripts/colorscripts" -if command -v find &>/dev/null; then - LS_CMD="$(command -v find) ${DIR_COLORSCRIPTS} -maxdepth 1 -type f | xargs -I$ basename $" -else - LS_CMD="$(command -v ls) ${DIR_COLORSCRIPTS} | xargs -I$ basename $" -fi - -function _colorscript { - local curcontext="$curcontext" state line - typeset -A opt_args - - _arguments \ - '1: :->subcmd'\ - '2: :->colorscript' - - case $state in - subcmd) - _arguments "1:subcmd:((help:'Print help'\ - list:'List all color scripts.'\ - exec:'Run a specific color script.'\ - blacklist:'Blacklist a specific color script.'\ - all:'Run all the color scripts.'\ - random:'Run a random color script.'))" - ;; - *) - case $words[2] in - "exec") - for colorscript in $(sh -c $LS_CMD); do - compadd ${colorscript} - done - ;; - "blacklist") - for colorscript in $(sh -c $LS_CMD); do - compadd ${colorscript} - done - ;; - *) - ;; - esac - esac -} - -_colorscript "$@" -- cgit v1.3-2-g11bf