zsh breaks when there are escape codes in completion descriptions

This commit is contained in:
Kovid Goyal 2022-11-16 19:32:16 +05:30
parent 3e4df7d812
commit d4c103e53e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 16 additions and 3 deletions

View File

@ -60,12 +60,11 @@ func (self *Match) FormatForCompletionList(max_word_len int, f *markup.Context,
desc = f.Prettify(line)
multiline := false
max_desc_len := screen_width - 2
max_desc_len := screen_width - max_word_len - 3
if word_len > max_word_len {
multiline = true
} else {
word += strings.Repeat(" ", max_word_len-word_len)
max_desc_len = screen_width - max_word_len - 3
}
if wcswidth.Stringwidth(desc) > max_desc_len {
desc = wcswidth.TruncateToVisualLength(desc, max_desc_len-2) + "\x1b[m…"
@ -108,7 +107,7 @@ func serialize(completions *Completions, f *markup.Context, screen_width int) ([
fmt.Fprintln(&output, "compdescriptions=(")
limit := mg.max_visual_word_length(16)
for _, m := range mg.Matches {
fmt.Fprintln(&output, utils.QuoteStringForSH(m.FormatForCompletionList(limit, f, screen_width)))
fmt.Fprintln(&output, utils.QuoteStringForSH(wcswidth.StripEscapeCodes(m.FormatForCompletionList(limit, f, screen_width))))
}
fmt.Fprintln(&output, ")")
cmd.WriteString(" -l -d compdescriptions")

View File

@ -5,6 +5,7 @@ package wcswidth
import (
"fmt"
"strconv"
"strings"
"kitty/tools/utils"
)
@ -125,3 +126,16 @@ func Stringwidth(text string) int {
w := CreateWCWidthIterator()
return w.Parse(utils.UnsafeStringToBytes(text))
}
func StripEscapeCodes(text string) string {
out := strings.Builder{}
out.Grow(len(text))
p := EscapeCodeParser{}
p.HandleRune = func(ch rune) error {
out.WriteRune(ch)
return nil
}
p.ParseString(text)
return out.String()
}