Skip to content

Commit 3aa1885

Browse files
add description to zsh completion (#656)
1 parent b3a589e commit 3aa1885

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

commands.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ func completeCommand(ctx commandContext) error {
225225
return nil
226226
}
227227

228-
includeDescription := requester == "fish"
228+
// fish has always shown descriptions; zsh gained them in v2 of the protocol.
229+
// Gating zsh on the version keeps an older "zsh:v1" completion script (which
230+
// cannot parse descriptions) working against a newer resticprofile.
231+
includeDescription := requester == "fish" || (requester == "zsh" && requesterVersion >= 2)
229232

230233
completions := NewCompleter(ctx.ownCommands.All(), DefaultFlagsLoader, includeDescription).Complete(args)
231234
if len(completions) > 0 {

commands_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ func TestCompleteCall(t *testing.T) {
197197
{args: []string{"fish:v1", "--"}, expected: expectedFlagsWithDescriptions},
198198
{args: []string{"bash:v10", "--"}, expected: ""},
199199
{args: []string{"zsh:v1", "--"}, expected: expectedFlags},
200+
{args: []string{"zsh:v2", "--"}, expected: expectedFlagsWithDescriptions},
200201
}
201202

202203
for _, test := range testTable {

contrib/completion/zsh-completion.zsh

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,52 @@
1212
# Option 2 - Source directly (add to ~/.zshrc AFTER compinit):
1313
# source <(resticprofile generate --zsh-completion)
1414

15+
# _resticprofile_add adds completions, turning an optional tab-separated description
16+
# (sent by "zsh:v2") into a zsh description shown next to each match, like fish does.
17+
# compadd -d is used (rather than _describe) because it adds matches at the same level
18+
# as the plain builtin, so they coexist with the matches added by restic's _restic.
19+
function _resticprofile_add() {
20+
local -a values display
21+
local line value
22+
integer width=0
23+
24+
# First pass: collect values and the widest one (to align the description column)
25+
for line in "$@"; do
26+
value="${line%%$'\t'*}"
27+
values+=("${value}")
28+
[[ "${line}" == *$'\t'* ]] && (( ${#value} > width )) && width=${#value}
29+
done
30+
31+
# Second pass: build the display strings, padding values so descriptions line up
32+
for line in "$@"; do
33+
value="${line%%$'\t'*}"
34+
if [[ "${line}" == *$'\t'* ]]; then
35+
display+=("${(r:width:)value} -- ${line#*$'\t'}")
36+
else
37+
display+=("${value}")
38+
fi
39+
done
40+
41+
compadd -d display -- "${values[@]}"
42+
}
43+
1544
function _resticprofile() {
1645
local resticprofile="${words[1]}"
1746

1847
# Convert zsh's 1-indexed CURRENT to 0-indexed position relative to arguments
1948
local cursor_pos=$(( CURRENT - 1 ))
2049

21-
# Get completions from resticprofile
50+
# Get completions from resticprofile ("zsh:v2" enables tab-separated descriptions)
2251
local -a completions
23-
completions=("${(@f)$("${resticprofile}" complete "zsh:v1" "__POS:${cursor_pos}" "${words[2,-1]}" 2>/dev/null)}")
52+
completions=("${(@f)$("${resticprofile}" complete "zsh:v2" "__POS:${cursor_pos}" "${words[2,-1]}" 2>/dev/null)}")
2453

2554
(( ${#completions[@]} == 0 )) && return
2655

2756
local last="${completions[-1]}"
2857

2958
if [[ "${last}" == "__complete_file" ]]; then
3059
completions[-1]=()
31-
(( ${#completions[@]} )) && compadd -- "${completions[@]}"
60+
(( ${#completions[@]} )) && _resticprofile_add "${completions[@]}"
3261
_files
3362
return
3463
fi
@@ -42,7 +71,7 @@ function _resticprofile() {
4271
# Add resticprofile's own completions. These already carry the profile
4372
# prefix (e.g. "default.show") and must be added before the compset below,
4473
# while $PREFIX still holds the full "profile." prefixed word.
45-
(( ${#completions[@]} )) && compadd -- "${completions[@]}"
74+
(( ${#completions[@]} )) && _resticprofile_add "${completions[@]}"
4675

4776
# Build args for restic by stripping profile prefixes from the current words
4877
local -a restic_words=()
@@ -88,7 +117,7 @@ function _resticprofile() {
88117
return
89118
fi
90119

91-
compadd -- "${completions[@]}"
120+
_resticprofile_add "${completions[@]}"
92121
}
93122

94123
# Register the completion function (works when sourced after compinit)

0 commit comments

Comments
 (0)