From 562d02dc43fff2cdca3d6be2f822667674dc3e79 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 8 Jun 2020 13:43:55 -0400 Subject: [PATCH] Properly truncate fullwidth/double-byte chars --- ui/pager.go | 5 ++--- ui/stash.go | 10 ++-------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/ui/pager.go b/ui/pager.go index 3f0564b..da4e385 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -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", diff --git a/ui/stash.go b/ui/stash.go index a4e476a..7ad09be 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -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{