From 640cf1e2c11ebe8c36bdcaf37138d0ffb6b4a05c Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Mon, 27 Apr 2020 23:36:13 +0200 Subject: [PATCH] Add zsh and fish completion wrt #19 --- .bash-completion => shell-completions/bash | 0 shell-completions/fish | 19 +++++++++++++ shell-completions/zsh | 32 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) rename .bash-completion => shell-completions/bash (100%) create mode 100644 shell-completions/fish create mode 100644 shell-completions/zsh diff --git a/.bash-completion b/shell-completions/bash similarity index 100% rename from .bash-completion rename to shell-completions/bash diff --git a/shell-completions/fish b/shell-completions/fish new file mode 100644 index 0000000..1605ba5 --- /dev/null +++ b/shell-completions/fish @@ -0,0 +1,19 @@ + function _ghcup + set -l cl (commandline --tokenize --current-process) + # Hack around fish issue #3934 + set -l cn (commandline --tokenize --cut-at-cursor --current-process) + set -l cn (count $cn) + set -l tmpline --bash-completion-enriched --bash-completion-index $cn + for arg in $cl + set tmpline $tmpline --bash-completion-word $arg + end + for opt in (ghcup $tmpline) + if test -d $opt + echo -E "$opt/" + else + echo -E "$opt" + end + end +end + +complete --no-files --command ghcup --arguments '(_ghcup)' diff --git a/shell-completions/zsh b/shell-completions/zsh new file mode 100644 index 0000000..d2c41df --- /dev/null +++ b/shell-completions/zsh @@ -0,0 +1,32 @@ +#compdef ghcup + +local request +local completions +local word +local index=$((CURRENT - 1)) + +request=(--bash-completion-enriched --bash-completion-index $index) +for arg in ${words[@]}; do + request=(${request[@]} --bash-completion-word $arg) +done + +IFS=$'\n' completions=($( ghcup "${request[@]}" )) + +for word in $completions; do + local -a parts + + # Split the line at a tab if there is one. + IFS=$'\t' parts=($( echo $word )) + + if [[ -n $parts[2] ]]; then + if [[ $word[1] == "-" ]]; then + local desc=("$parts[1] ($parts[2])") + compadd -d desc -- $parts[1] + else + local desc=($(print -f "%-019s -- %s" $parts[1] $parts[2])) + compadd -l -d desc -- $parts[1] + fi + else + compadd -f -- $word + fi +done