Skip to content

Commit

Permalink
zsh: usage support
Browse files Browse the repository at this point in the history
initial version without the `read` variant and formatting

Co-authored-by: maxlandon <maximelandon@gmail.com>
  • Loading branch information
rsteube and maxlandon committed Nov 27, 2022
1 parent 93ca86c commit 601ef2a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion example/cmd/_test/zsh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ function _example_completion {
fi

export ZLS_COLOURS="${lines[1]}"
[[ ! "${lines[2]}" == "NONE" ]] && _message -r "${lines[2]}"

# shellcheck disable=SC2034,2206
lines=(${lines[@]:1})
lines=(${lines[@]:2})
# shellcheck disable=SC2034,2206
local vals=(${lines%$'\t'*})
# shellcheck disable=SC2034,2206
Expand Down
19 changes: 18 additions & 1 deletion internal/shell/zsh/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func ActionRawValues(currentWord string, usage string, nospace common.SuffixMatc
if len(zstyles) > 1000 { // TODO disable styling for large amount of values (bad performance)
zstyles = make([]string, 0)
}
return fmt.Sprintf(":%v\n%v", strings.Join(zstyles, ":"), strings.Join(vals, "\n"))

return fmt.Sprintf(":%v\n%v\n%v", strings.Join(zstyles, ":"), santizeMessage(usage), strings.Join(vals, "\n"))
}

var zstyleQuoter = strings.NewReplacer(
Expand All @@ -113,3 +114,19 @@ var zstyleQuoter = strings.NewReplacer(
func formatZstyle(s, _styleValue, _styleDescription string) string {
return fmt.Sprintf("=(#b)%v=0=%v=%v=%v", s, style.SGR(_styleValue), style.SGR(_styleDescription+" bg-default"), style.SGR(_styleDescription))
}

func santizeMessage(s string) string { // TODO cleanup
s = strings.NewReplacer(
"\n", ``,
"\r", ``,
"\t", ``,
"\v", ``,
"\f", ``,
"\b", ``,
).Replace(s)

if s == "" {
return "NONE" // TODO fix empty line issue in script
}
return s
}
3 changes: 2 additions & 1 deletion internal/shell/zsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ function _%v_completion {
fi
export ZLS_COLOURS="${lines[1]}"
[[ ! "${lines[2]}" == "NONE" ]] && _message -r "${lines[2]}"
# shellcheck disable=SC2034,2206
lines=(${lines[@]:1})
lines=(${lines[@]:2})
# shellcheck disable=SC2034,2206
local vals=(${lines%%$'\t'*})
# shellcheck disable=SC2034,2206
Expand Down

0 comments on commit 601ef2a

Please sign in to comment.