Repository: rsteube/carapace
Pull request #457 (2022-04-02 14:07:40)

zsh: tabular completion when no description


Patch:
diff --git a/internal/shell/zsh/action.go b/internal/shell/zsh/action.go
index eae8b50..518d04b 100644
--- a/internal/shell/zsh/action.go
+++ b/internal/shell/zsh/action.go
@@ -20,6 +20,7 @@ func ActionRawValues(currentWord string, nospace bool, values common.RawValues)
 	filtered := make([]common.RawValue, 0)
 
 	maxLength := 0
+	hasDescriptions := false
 	for _, r := range values {
 		if strings.HasPrefix(r.Value, currentWord) {
 			filtered = append(filtered, r)
@@ -27,6 +28,7 @@ func ActionRawValues(currentWord string, nospace bool, values common.RawValues)
 				maxLength = length
 			}
 		}
+		hasDescriptions = hasDescriptions || r.Description != ""
 	}
 
 	vals := make([]string, len(filtered))
@@ -39,9 +41,14 @@ func ActionRawValues(currentWord string, nospace bool, values common.RawValues)
 		val.Description = sanitizer.Replace(val.Description)
 
 		if strings.TrimSpace(val.Description) == "" {
-			vals[index] = fmt.Sprintf("%v\t%v", val.Value, style.FormatAnsi(val.Display, val.Style))
+			if hasDescriptions {
+				vals[index] = fmt.Sprintf("%v\t%v", val.Value, style.FormatAnsi(val.Display, val.Style))
+			} else {
+				// TODO compadd strips display values of ansi escape codes during tabular completion (so leave that out that for now)
+				vals[index] = fmt.Sprintf("%v\t%v", val.Value, val.Display)
+			}
 		} else {
-			vals[index] = fmt.Sprintf("%v\t%v %v-- %v", val.Value, style.FormatAnsi(val.Display, val.Style), strings.Repeat(" ", maxLength-len(val.Display)), val.TrimmedDescription())
+			vals[index] = fmt.Sprintf("%v\t%v\002 %v-- %v", val.Value, style.FormatAnsi(val.Display, val.Style), strings.Repeat(" ", maxLength-len(val.Display)), val.TrimmedDescription())
 		}
 	}
 	return strings.Join(vals, "\n")
diff --git a/internal/shell/zsh/snippet.go b/internal/shell/zsh/snippet.go
index d810543..be9cd8b 100644
--- a/internal/shell/zsh/snippet.go
+++ b/internal/shell/zsh/snippet.go
@@ -29,14 +29,18 @@ function _%v_completion {
   # shellcheck disable=SC2034,2206
   local vals=(${c%%%%$'\t'*})
   # shellcheck disable=SC2034,2206
-  local descriptions=(${c##*$'\t'})
+  local displays=(${c##*$'\t'})
 
   local suffix=' '
   [[ ${vals[1]} == *$'\001' ]] && suffix=''
   # shellcheck disable=SC2034,2206
   vals=(${vals%%%%$'\001'*})
 
-  compadd -l -S "${suffix}" -d descriptions -a -- vals
+  if [[ ${displays[*]} == *$'\002'* ]]; then # with descriptions
+    compadd -l -S "${suffix}" -d displays -a -- vals
+  else
+    compadd -S "${suffix}" -d displays -a -- vals
+  fi
 }
 compquote '' 2>/dev/null && _%v_completion
 compdef _%v_completion %v


Test patch:
diff --git a/example/cmd/_test/zsh.sh b/example/cmd/_test/zsh.sh
index 439e265..9f594bd 100644
--- a/example/cmd/_test/zsh.sh
+++ b/example/cmd/_test/zsh.sh
@@ -17,14 +17,18 @@ function _example_completion {
   # shellcheck disable=SC2034,2206
   local vals=(${c%%$'\t'*})
   # shellcheck disable=SC2034,2206
-  local descriptions=(${c##*$'\t'})
+  local displays=(${c##*$'\t'})
 
   local suffix=' '
   [[ ${vals[1]} == *$'\001' ]] && suffix=''
   # shellcheck disable=SC2034,2206
   vals=(${vals%%$'\001'*})
 
-  compadd -l -S "${suffix}" -d descriptions -a -- vals
+  if [[ ${displays[*]} == *$'\002'* ]]; then # with descriptions
+    compadd -l -S "${suffix}" -d displays -a -- vals
+  else
+    compadd -S "${suffix}" -d displays -a -- vals
+  fi
 }
 compquote '' 2>/dev/null && _example_completion
 compdef _example_completion example