forked from TrueCloudLab/restic
Merge pull request #3050 from greatroar/widechars
Fix string truncation in ui/termstatus
This commit is contained in:
commit
f14436953a
4 changed files with 35 additions and 14 deletions
2
go.mod
2
go.mod
|
@ -38,7 +38,7 @@ require (
|
||||||
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
|
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
|
||||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
|
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
|
||||||
golang.org/x/sys v0.0.0-20201007082116-8445cc04cbdf
|
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
|
google.golang.org/api v0.32.0
|
||||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
|
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
|
||||||
gopkg.in/ini.v1 v1.61.0 // indirect
|
gopkg.in/ini.v1 v1.61.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -409,6 +409,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.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 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
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-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-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
|
"golang.org/x/text/width"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Terminal is used to write messages and display status lines which can be
|
// 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)
|
t.Error(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// truncate returns a string that has at most maxlen characters. If maxlen is
|
// Truncate s to fit in width (number of terminal cells) w.
|
||||||
// negative, the empty string is returned.
|
// If w is negative, returns the empty string.
|
||||||
func truncate(s string, maxlen int) string {
|
func truncate(s string, w int) string {
|
||||||
if maxlen < 0 {
|
if len(s) < w {
|
||||||
return ""
|
// 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.
|
||||||
if len(s) < maxlen {
|
|
||||||
return s
|
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.
|
// SetStatus updates the status lines.
|
||||||
|
|
|
@ -5,7 +5,7 @@ import "testing"
|
||||||
func TestTruncate(t *testing.T) {
|
func TestTruncate(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
input string
|
input string
|
||||||
maxlen int
|
width int
|
||||||
output string
|
output string
|
||||||
}{
|
}{
|
||||||
{"", 80, ""},
|
{"", 80, ""},
|
||||||
|
@ -18,14 +18,17 @@ func TestTruncate(t *testing.T) {
|
||||||
{"foo", 1, "f"},
|
{"foo", 1, "f"},
|
||||||
{"foo", 0, ""},
|
{"foo", 0, ""},
|
||||||
{"foo", -1, ""},
|
{"foo", -1, ""},
|
||||||
|
{"Löwen", 4, "Löwe"},
|
||||||
|
{"あああああああああ/data", 10, "あああああ"},
|
||||||
|
{"あああああああああ/data", 11, "あああああ"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run("", func(t *testing.T) {
|
t.Run("", func(t *testing.T) {
|
||||||
out := truncate(test.input, test.maxlen)
|
out := truncate(test.input, test.width)
|
||||||
if out != test.output {
|
if out != test.output {
|
||||||
t.Fatalf("wrong output for input %v, maxlen %d: want %q, got %q",
|
t.Fatalf("wrong output for input %v, width %d: want %q, got %q",
|
||||||
test.input, test.maxlen, test.output, out)
|
test.input, test.width, test.output, out)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue