Properly truncate fullwidth/double-byte chars

This commit is contained in:
Christian Rocha 2020-06-08 13:43:55 -04:00 committed by Christian Muehlhaeuser
commit 562d02dc43
2 changed files with 4 additions and 11 deletions

View file

@ -266,14 +266,13 @@ func pagerStatusBarView(b *strings.Builder, m pagerModel) {
if len(noteText) == 0 {
noteText = "(No title)"
}
noteText = truncate(" "+noteText+" ", max(
0,
noteText = truncate(" "+noteText+" ", max(0,
m.width-len(logoText)-len(percentText)-len(helpNoteText),
))
// Empty space
emptyCell := te.String(" ").Background(statusBarBg.Color()).String()
padding := max(0, m.width-len(logoText)-len(noteText)-len(percentText)-len(helpNoteText))
padding := max(0, m.width-len(logoText)-runewidth.StringWidth(noteText)-len(percentText)-len(helpNoteText))
emptySpace := strings.Repeat(emptyCell, padding)
fmt.Fprintf(b, "%s%s%s%s%s",

View file

@ -14,6 +14,7 @@ import (
"github.com/charmbracelet/charm"
"github.com/charmbracelet/charm/ui/common"
"github.com/dustin/go-humanize"
runewidth "github.com/mattn/go-runewidth"
te "github.com/muesli/termenv"
)
@ -587,14 +588,7 @@ func wrapMarkdowns(t markdownType, md []*charm.Markdown) (m []*markdown) {
}
func truncate(str string, num int) string {
s := str
if len(str) > num {
if num > 1 {
num -= 1
}
s = str[0:num] + "…"
}
return s
return runewidth.Truncate(str, num, "…")
}
var magnitudes = []humanize.RelTimeMagnitude{