From 69ed49c526ed0ce935cf746702e169b6b2cf8426 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Tue, 11 Dec 2018 15:16:17 -0500 Subject: [PATCH] Add asdf plugin to Fish and rename el8 Using de as a shortcut for docker-compose elation, becuase two keys is better than three! --- config/fish/completions/asdf.bash | 70 +++++++++++++ config/fish/completions/asdf.fish | 110 ++++++++++++++++++++ config/fish/config.fish | 1 + config/fish/fishfile | 1 + config/fish/functions/asdf.fish | 15 +++ config/fish/functions/{el8.fish => de.fish} | 2 +- 6 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 config/fish/completions/asdf.bash create mode 100644 config/fish/completions/asdf.fish create mode 100644 config/fish/config.fish create mode 100644 config/fish/functions/asdf.fish rename config/fish/functions/{el8.fish => de.fish} (83%) diff --git a/config/fish/completions/asdf.bash b/config/fish/completions/asdf.bash new file mode 100644 index 0000000..0d57b71 --- /dev/null +++ b/config/fish/completions/asdf.bash @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +_asdf () { + local cur + cur=${COMP_WORDS[COMP_CWORD]} + local cmd + cmd=${COMP_WORDS[1]} + local prev + prev=${COMP_WORDS[COMP_CWORD-1]} + local plugins + plugins=$(asdf plugin-list 2> /dev/null | tr '\n' ' ') + + # We can safely ignore warning SC2207 since it warns that it will uses the + # shell's sloppy word splitting and globbing. The possible commands here are + # all single words, and most likely won't contain special chars the shell will + # expand. + COMPREPLY=() + + case "$cmd" in + plugin-update) + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "$plugins --all" -- "$cur")) + ;; + plugin-remove|current|list|list-all) + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "$plugins" -- "$cur")) + ;; + plugin-add) + local available_plugins + available_plugins=$( (asdf plugin-list 2> /dev/null && asdf plugin-list-all 2> /dev/null) | sort | uniq -u) + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "$available_plugins" -- "$cur")) + ;; + install) + if [[ "$plugins" == *"$prev"* ]] ; then + local versions + versions=$(asdf list-all "$prev" 2> /dev/null) + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "$versions" -- "$cur")) + else + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "$plugins" -- "$cur")) + fi + ;; + update) + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "--head" -- "$cur")) + ;; + uninstall|where|reshim|local|global) + if [[ "$plugins" == *"$prev"* ]] ; then + local versions + versions=$(asdf list "$prev" 2> /dev/null) + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "$versions" -- "$cur")) + else + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "$plugins" -- "$cur")) + fi + ;; + *) + local cmds='current global help install list list-all local plugin-add plugin-list plugin-list-all plugin-remove plugin-update reshim uninstall update where which ' + # shellcheck disable=SC2207 + COMPREPLY=($(compgen -W "$cmds" -- "$cur")) + ;; + esac + + return 0 +} + +complete -F _asdf asdf diff --git a/config/fish/completions/asdf.fish b/config/fish/completions/asdf.fish new file mode 100644 index 0000000..4f03786 --- /dev/null +++ b/config/fish/completions/asdf.fish @@ -0,0 +1,110 @@ +function __fish_asdf_needs_command + set -l cmd (commandline -opc) + if test (count $cmd) -eq 1 + return 0 + end + return 1 +end + +function __fish_asdf_using_command -a current_command + set -l cmd (commandline -opc) + if test (count $cmd) -gt 1 + if test $current_command = $cmd[2] + return 0 + end + end + return 1 +end + +function __fish_asdf_arg_number -a number + set -l cmd (commandline -opc) + test (count $cmd) -eq $number +end + +function __fish_asdf_arg_at -a number + set -l cmd (commandline -opc) + echo $cmd[$number] +end + +function __fish_asdf_list_versions -a plugin + asdf list $plugin 2> /dev/null | sed -e 's/^\s*//' +end + +function __fish_asdf_list_all -a plugin + asdf list-all $plugin 2> /dev/null +end + +function __fish_asdf_plugin_list + asdf plugin-list 2> /dev/null +end + +function __fish_asdf_plugin_list_all + asdf plugin-list-all 2> /dev/null +end + +# plugin-add completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a plugin-add -d "Add git repo as plugin" +complete -f -c asdf -n '__fish_asdf_using_command plugin-add; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list_all | grep -v \'*\' | awk \'{ print $1 }\')' +complete -f -c asdf -n '__fish_asdf_using_command plugin-add; and __fish_asdf_arg_number 3' -a '(__fish_asdf_plugin_list_all | grep (__fish_asdf_arg_at 3) | awk \'{ print $2 }\')' +complete -f -c asdf -n '__fish_asdf_using_command plugin-add; and __fish_asdf_arg_number 4' + +# plugin-list completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a plugin-list -d "List installed plugins" + +# plugin-list-all completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a plugin-list-all -d "List all existing plugins" + +# plugin-remove completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a plugin-remove -d "Remove plugin and package versions" +complete -f -c asdf -n '__fish_asdf_using_command plugin-remove; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' + +# plugin-update completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a plugin-update -d "Update plugin" +complete -f -c asdf -n '__fish_asdf_using_command plugin-update; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' +complete -f -c asdf -n '__fish_asdf_using_command plugin-update; and __fish_asdf_arg_number 2' -a --all + +# install completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a install -d "Install a specific version of a package" +complete -f -c asdf -n '__fish_asdf_using_command install; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' +complete -f -c asdf -n '__fish_asdf_using_command install; and __fish_asdf_arg_number 3' -a '(__fish_asdf_list_all (__fish_asdf_arg_at 3))' + +# uninstall completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a uninstall -d "Remove a specific version of a package" +complete -f -c asdf -n '__fish_asdf_using_command uninstall; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' +complete -f -c asdf -n '__fish_asdf_using_command uninstall; and __fish_asdf_arg_number 3' -a '(__fish_asdf_list_versions (__fish_asdf_arg_at 3))' + +# current completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a current -d "Display version set or being used for package" +complete -f -c asdf -n '__fish_asdf_using_command current; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' + +# where completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a where -d "Display install path for an installed version" +complete -f -c asdf -n '__fish_asdf_using_command where; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' +complete -f -c asdf -n '__fish_asdf_using_command where; and __fish_asdf_arg_number 3' -a '(__fish_asdf_list_versions (__fish_asdf_arg_at 3))' + +# list completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a list -d "List installed versions of a package" +complete -f -c asdf -n '__fish_asdf_using_command list; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' + +# list-all completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a list-all -d "List all versions of a package" +complete -f -c asdf -n '__fish_asdf_using_command list-all; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' + +# reshim completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a reshim -d "Recreate shims for version of a package" +complete -f -c asdf -n '__fish_asdf_using_command reshim; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' +complete -f -c asdf -n '__fish_asdf_using_command reshim; and __fish_asdf_arg_number 3' -a '(__fish_asdf_list_versions (__fish_asdf_arg_at 3))' + +# local completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a local -d "Set local version for a plugin" +complete -f -c asdf -n '__fish_asdf_using_command local; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' +complete -f -c asdf -n '__fish_asdf_using_command local; and test (count (commandline -opc)) -gt 2' -a '(__fish_asdf_list_versions (__fish_asdf_arg_at 3)) system' + +# global completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a global -d "Set global version for a plugin" +complete -f -c asdf -n '__fish_asdf_using_command global; and __fish_asdf_arg_number 2' -a '(__fish_asdf_plugin_list)' +complete -f -c asdf -n '__fish_asdf_using_command global; and test (count (commandline -opc)) -gt 2' -a '(__fish_asdf_list_versions (__fish_asdf_arg_at 3)) system' + +# misc +complete -f -c asdf -n '__fish_asdf_needs_command' -l "help" -d "Displays help" +complete -f -c asdf -n '__fish_asdf_needs_command' -l "version" -d "Displays asdf version" diff --git a/config/fish/config.fish b/config/fish/config.fish new file mode 100644 index 0000000..941a839 --- /dev/null +++ b/config/fish/config.fish @@ -0,0 +1 @@ +source ~/.asdf/asdf.fish diff --git a/config/fish/fishfile b/config/fish/fishfile index b822b55..276514b 100644 --- a/config/fish/fishfile +++ b/config/fish/fishfile @@ -5,3 +5,4 @@ fishgretel/fasd oh-my-fish/theme-bobthefish yyuu/pyenv 2m/fish-gpg-tty +asdf-vm/asdf diff --git a/config/fish/functions/asdf.fish b/config/fish/functions/asdf.fish new file mode 100644 index 0000000..1e259a0 --- /dev/null +++ b/config/fish/functions/asdf.fish @@ -0,0 +1,15 @@ +#!/usr/bin/env fish + +set -l asdf_dir (dirname (status -f)) +set -l asdf_data_dir ( + if test -n "$ASDF_DATA_DIR"; echo $ASDF_DATA_DIR; + else; echo $HOME/.asdf; end) + +# Add asdf to PATH +set -l asdf_bin_dirs $asdf_dir/bin $asdf_dir/shims $asdf_data_dir/shims + +for x in $asdf_bin_dirs + if begin not contains $x $PATH; and test -d $x; end + set -gx PATH $x $PATH + end +end diff --git a/config/fish/functions/el8.fish b/config/fish/functions/de.fish similarity index 83% rename from config/fish/functions/el8.fish rename to config/fish/functions/de.fish index f30852c..b7f4ae2 100644 --- a/config/fish/functions/el8.fish +++ b/config/fish/functions/de.fish @@ -1,4 +1,4 @@ -function el8 +function de pushd ~/devel/elationemr/hippo docker-compose $argv popd