mirror of
https://github.com/octoleo/restic.git
synced 2024-12-22 10:58:55 +00:00
Merge pull request #4291 from greatroar/widechars
ui/termstatus: Optimize Truncate
This commit is contained in:
commit
a06d927dce
@ -303,26 +303,35 @@ func Truncate(s string, w int) string {
|
||||
return s
|
||||
}
|
||||
|
||||
for i, r := range s {
|
||||
for i := uint(0); i < uint(len(s)); {
|
||||
utfsize := uint(1) // UTF-8 encoding size of first rune in s.
|
||||
w--
|
||||
if r > unicode.MaxASCII && wideRune(r) {
|
||||
|
||||
if s[i] > unicode.MaxASCII {
|
||||
var wide bool
|
||||
if wide, utfsize = wideRune(s[i:]); wide {
|
||||
w--
|
||||
}
|
||||
}
|
||||
|
||||
if w < 0 {
|
||||
return s[:i]
|
||||
}
|
||||
i += utfsize
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Guess whether r would occupy two terminal cells instead of one.
|
||||
// This cannot be determined exactly without knowing the terminal font,
|
||||
// so we treat all ambigous runes as full-width, i.e., two cells.
|
||||
func wideRune(r rune) bool {
|
||||
kind := width.LookupRune(r).Kind()
|
||||
return kind != width.Neutral && kind != width.EastAsianNarrow
|
||||
// Guess whether the first rune in s would occupy two terminal cells
|
||||
// instead of one. This cannot be determined exactly without knowing
|
||||
// the terminal font, so we treat all ambigous runes as full-width,
|
||||
// i.e., two cells.
|
||||
func wideRune(s string) (wide bool, utfsize uint) {
|
||||
prop, size := width.LookupString(s)
|
||||
kind := prop.Kind()
|
||||
wide = kind != width.Neutral && kind != width.EastAsianNarrow
|
||||
return wide, uint(size)
|
||||
}
|
||||
|
||||
// SetStatus updates the status lines.
|
||||
|
@ -79,11 +79,15 @@ func BenchmarkTruncateASCII(b *testing.B) {
|
||||
func BenchmarkTruncateUnicode(b *testing.B) {
|
||||
s := "Hello World or Καλημέρα κόσμε or こんにちは 世界"
|
||||
w := 0
|
||||
for _, r := range s {
|
||||
for i := 0; i < len(s); {
|
||||
w++
|
||||
if wideRune(r) {
|
||||
wide, utfsize := wideRune(s[i:])
|
||||
if wide {
|
||||
w++
|
||||
}
|
||||
i += int(utfsize)
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
benchmarkTruncate(b, s, w-1)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user