fix(readline): grip completion menu fixes

- dont print item left justified if the max number
of cells is 1 (this fixes issues in cjk as an example)
- trim items that are longer than the terminal width
pull/217/head
TorchedSammy 2022-11-25 18:39:18 -04:00
parent 8b547f2af0
commit 3eaeb6a5da
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 14 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"github.com/rivo/uniseg"
) )
// initGrid - Grid display details. Called each time we want to be sure to have // initGrid - Grid display details. Called each time we want to be sure to have
@ -13,8 +14,8 @@ func (g *CompletionGroup) initGrid(rl *Instance) {
// Compute size of each completion item box // Compute size of each completion item box
tcMaxLength := 1 tcMaxLength := 1
for i := range g.Suggestions { for i := range g.Suggestions {
if len(g.Suggestions[i]) > tcMaxLength { if uniseg.GraphemeClusterCount(g.Suggestions[i]) > tcMaxLength {
tcMaxLength = len([]rune(g.Suggestions[i])) tcMaxLength = uniseg.GraphemeClusterCount(g.Suggestions[i])
} }
} }
@ -103,7 +104,7 @@ func (g *CompletionGroup) writeGrid(rl *Instance) (comp string) {
rl.tcUsedY++ rl.tcUsedY++
} }
cellWidth := strconv.Itoa((GetTermWidth() / g.tcMaxX) - 2) cellWidth := strconv.Itoa((GetTermWidth() / g.tcMaxX) - 4)
x := 0 x := 0
y := 1 y := 1
@ -124,7 +125,15 @@ func (g *CompletionGroup) writeGrid(rl *Instance) (comp string) {
comp += seqInvert comp += seqInvert
} }
comp += fmt.Sprintf("%-"+cellWidth+"s %s", fmtEscape(g.Suggestions[i]), seqReset) sugg := g.Suggestions[i]
if len(sugg) > GetTermWidth() {
sugg = sugg[:GetTermWidth() - 4] + "..."
}
formatStr := "%-"+cellWidth+"s%s "
if g.tcMaxX == 1 {
formatStr = "%s%s"
}
comp += fmt.Sprintf(formatStr, fmtEscape(sugg), seqReset)
} }
// Always add a newline to the group if the end if not punctuated with one // Always add a newline to the group if the end if not punctuated with one