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+
1544function _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