From 5cf7b19dcce89f847f6f6d62685abaeca2b63e1c Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Tue, 1 Jan 2019 21:52:05 +0800 Subject: [PATCH] Make runewidth treat ambiguous rune as short and fix ui display --- util/text/left_padded.go | 9 ++++++++- util/text/left_padded_test.go | 6 +++--- util/text/text.go | 7 +++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/util/text/left_padded.go b/util/text/left_padded.go index 24bd4d09268f3be98c205ac8a49e367aa89ad756..43e84966bdedc580087a8b17316e3ba07356db14 100644 --- a/util/text/left_padded.go +++ b/util/text/left_padded.go @@ -7,6 +7,13 @@ import ( "strings" ) +// Force runewidth not to treat ambiguous runes as wide chars, so that things +// like unicode ellipsis/up/down/left/right glyphs can have correct runewidth +// and can be displayed correctly in terminals. +func init() { + runewidth.DefaultCondition.EastAsianWidth = false +} + // LeftPadMaxLine pads a string on the left by a specified amount and pads the // string on the right to fill the maxLength func LeftPadMaxLine(text string, length, leftPad int) string { @@ -15,7 +22,7 @@ func LeftPadMaxLine(text string, length, leftPad int) string { scrWidth := runewidth.StringWidth(text) // truncate and ellipse if needed if scrWidth+leftPad > length { - rightPart = runewidth.Truncate(text, length-leftPad, "...") + rightPart = runewidth.Truncate(text, length-leftPad, "…") } else if scrWidth+leftPad < length { rightPart = runewidth.FillRight(text, length-leftPad) } diff --git a/util/text/left_padded_test.go b/util/text/left_padded_test.go index a8ea12fe5ece055e63dcf730bc1b9989cd259c6a..0be79e32d64ea792a228d18d58bb08eb331cb31f 100644 --- a/util/text/left_padded_test.go +++ b/util/text/left_padded_test.go @@ -16,7 +16,7 @@ func TestLeftPadMaxLine(t *testing.T) { }, { "foofoofoo", - "f...", + "foo…", 4, 0, }, @@ -28,13 +28,13 @@ func TestLeftPadMaxLine(t *testing.T) { }, { "foo", - " ...", + " f…", 4, 2, }, { "foofoofoo", - " f...", + " foo…", 6, 2, }, diff --git a/util/text/text.go b/util/text/text.go index e7ef4e47f9a3c0cbcc4df47b8d53f20c531fb496..f77fa0e219a2e1847ca84e23170fbadad9f86c2f 100644 --- a/util/text/text.go +++ b/util/text/text.go @@ -6,6 +6,13 @@ import ( "unicode/utf8" ) +// Force runewidth not to treat ambiguous runes as wide chars, so that things +// like unicode ellipsis/up/down/left/right glyphs can have correct runewidth +// and can be displayed correctly in terminals. +func init() { + runewidth.DefaultCondition.EastAsianWidth = false +} + // Wrap a text for an exact line size // Handle properly terminal color escape code func Wrap(text string, lineWidth int) (string, int) {