42e8597e7137e9a7d930b6d725574289bd1499bf

Author
Pavle Portic <git@theedgeofrage.com>
Committer
Pavle Portic <git@theedgeofrage.com>
Date

Message

Add flux zsh completions

Diff

This diff is truncated to protect this page.

  1diff --git a/dot_config/zsh/completions/flux.zsh b/dot_config/zsh/completions/flux.zsh
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..10ae9425d4d1931799e460928d35c095d6a25cce
  4--- /dev/null
  5+++ b/dot_config/zsh/completions/flux.zsh
  6@@ -0,0 +1,178 @@
  7+#compdef _flux flux
  8+
  9+# zsh completion for flux                                 -*- shell-script -*-
 10+
 11+__flux_debug()
 12+{
 13+    local file="$BASH_COMP_DEBUG_FILE"
 14+    if [[ -n ${file} ]]; then
 15+        echo "$*" >> "${file}"
 16+    fi
 17+}
 18+
 19+_flux()
 20+{
 21+    local shellCompDirectiveError=1
 22+    local shellCompDirectiveNoSpace=2
 23+    local shellCompDirectiveNoFileComp=4
 24+    local shellCompDirectiveFilterFileExt=8
 25+    local shellCompDirectiveFilterDirs=16
 26+
 27+    local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace
 28+    local -a completions
 29+
 30+    __flux_debug "\n========= starting completion logic =========="
 31+    __flux_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
 32+
 33+    # The user could have moved the cursor backwards on the command-line.
 34+    # We need to trigger completion from the $CURRENT location, so we need
 35+    # to truncate the command-line ($words) up to the $CURRENT location.
 36+    # (We cannot use $CURSOR as its value does not work when a command is an alias.)
 37+    words=("${=words[1,CURRENT]}")
 38+    __flux_debug "Truncated words[*]: ${words[*]},"
 39+
 40+    lastParam=${words[-1]}
 41+    lastChar=${lastParam[-1]}
 42+    __flux_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"
 43+
 44+    # For zsh, when completing a flag with an = (e.g., flux -n=<TAB>)
 45+    # completions must be prefixed with the flag
 46+    setopt local_options BASH_REMATCH
 47+    if [[ "${lastParam}" =~ '-.*=' ]]; then
 48+        # We are dealing with a flag with an =
 49+        flagPrefix="-P ${BASH_REMATCH}"
 50+    fi
 51+
 52+    # Prepare the command to obtain completions
 53+    requestComp="${words[1]} __complete ${words[2,-1]}"
 54+    if [ "${lastChar}" = "" ]; then
 55+        # If the last parameter is complete (there is a space following it)
 56+        # We add an extra empty parameter so we can indicate this to the go completion code.
 57+        __flux_debug "Adding extra empty parameter"
 58+        requestComp="${requestComp} \"\""
 59+    fi
 60+
 61+    __flux_debug "About to call: eval ${requestComp}"
 62+
 63+    # Use eval to handle any environment variables and such
 64+    out=$(eval ${requestComp} 2>/dev/null)
 65+    __flux_debug "completion output: ${out}"
 66+
 67+    # Extract the directive integer following a : from the last line
 68+    local lastLine
 69+    while IFS='\n' read -r line; do
 70+        lastLine=${line}
 71+    done < <(printf "%s\n" "${out[@]}")
 72+    __flux_debug "last line: ${lastLine}"
 73+
 74+    if [ "${lastLine[1]}" = : ]; then
 75+        directive=${lastLine[2,-1]}
 76+        # Remove the directive including the : and the newline
 77+        local suffix
 78+        (( suffix=${#lastLine}+2))
 79+        out=${out[1,-$suffix]}
 80+    else
 81+        # There is no directive specified.  Leave $out as is.
 82+        __flux_debug "No directive found.  Setting do default"
 83+        directive=0
 84+    fi
 85+
 86+    __flux_debug "directive: ${directive}"
 87+    __flux_debug "completions: ${out}"
 88+    __flux_debug "flagPrefix: ${flagPrefix}"
 89+
 90+    if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
 91+        __flux_debug "Completion received error. Ignoring completions."
 92+        return
 93+    fi
 94+
 95+    while IFS='\n' read -r comp; do
 96+        if [ -n "$comp" ]; then
 97+            # If requested, completions are returned with a description.
 98+            # The description is preceded by a TAB character.
 99+            # For zsh's _describe, we need to use a : instead of a TAB.
100+            # We first need to escape any : as part of the completion itself.
101+            comp=${comp//:/\\:}
102+
103+            local tab=$(printf '\t')
104+            comp=${comp//$tab/:}
105+