From 3ed84ff0c6fe9160237245c26adb6e0189ea6c8b Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Mon, 2 Nov 2020 12:06:21 +0100 Subject: [PATCH] Fix string truncation in ui/termstatus Fixes #3046. --- go.mod | 2 +- go.sum | 2 ++ internal/ui/termstatus/status.go | 34 ++++++++++++++++++++------- internal/ui/termstatus/status_test.go | 11 +++++---- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index efcda8cc8..3d531d3d1 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 golang.org/x/sys v0.0.0-20201007082116-8445cc04cbdf - golang.org/x/text v0.3.3 + golang.org/x/text v0.3.4 google.golang.org/api v0.32.0 gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect gopkg.in/ini.v1 v1.61.0 // indirect diff --git a/go.sum b/go.sum index 1503448bd..e2ddee96a 100644 --- a/go.sum +++ b/go.sum @@ -412,6 +412,8 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc= +golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/internal/ui/termstatus/status.go b/internal/ui/termstatus/status.go index b577f2f32..cfea18386 100644 --- a/internal/ui/termstatus/status.go +++ b/internal/ui/termstatus/status.go @@ -10,6 +10,7 @@ import ( "strings" "golang.org/x/crypto/ssh/terminal" + "golang.org/x/text/width" ) // Terminal is used to write messages and display status lines which can be @@ -268,18 +269,33 @@ func (t *Terminal) Errorf(msg string, args ...interface{}) { t.Error(s) } -// truncate returns a string that has at most maxlen characters. If maxlen is -// negative, the empty string is returned. -func truncate(s string, maxlen int) string { - if maxlen < 0 { - return "" - } - - if len(s) < maxlen { +// Truncate s to fit in width (number of terminal cells) w. +// If w is negative, returns the empty string. +func truncate(s string, w int) string { + if len(s) < w { + // Since the display width of a character is at most 2 + // and all of ASCII (single byte per rune) has width 1, + // no character takes more bytes to encode than its width. return s } - return s[:maxlen] + for i, r := range s { + // Determine width of the rune. This cannot be determined without + // knowing the terminal font, so let's just be careful and treat + // all ambigous characters as full-width, i.e., two cells. + wr := 2 + switch width.LookupRune(r).Kind() { + case width.Neutral, width.EastAsianNarrow: + wr = 1 + } + + w -= wr + if w < 0 { + return s[:i] + } + } + + return s } // SetStatus updates the status lines. diff --git a/internal/ui/termstatus/status_test.go b/internal/ui/termstatus/status_test.go index 6238d0532..d22605e31 100644 --- a/internal/ui/termstatus/status_test.go +++ b/internal/ui/termstatus/status_test.go @@ -5,7 +5,7 @@ import "testing" func TestTruncate(t *testing.T) { var tests = []struct { input string - maxlen int + width int output string }{ {"", 80, ""}, @@ -18,14 +18,17 @@ func TestTruncate(t *testing.T) { {"foo", 1, "f"}, {"foo", 0, ""}, {"foo", -1, ""}, + {"Löwen", 4, "Löwe"}, + {"あああああああああ/data", 10, "あああああ"}, + {"あああああああああ/data", 11, "あああああ"}, } for _, test := range tests { t.Run("", func(t *testing.T) { - out := truncate(test.input, test.maxlen) + out := truncate(test.input, test.width) if out != test.output { - t.Fatalf("wrong output for input %v, maxlen %d: want %q, got %q", - test.input, test.maxlen, test.output, out) + t.Fatalf("wrong output for input %v, width %d: want %q, got %q", + test.input, test.width, test.output, out) } }) }