mirror of
https://github.com/restic/restic.git
synced 2026-08-15 18:13:17 +00:00
Use character display width for table padding
Using len(...) for table cell padding produced wrong results for unicode chracters leading to misaligned tables. Implementation changed to take the actual terminal display width into consideration.
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"math/bits"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"golang.org/x/text/width"
|
||||
)
|
||||
|
||||
func FormatBytes(c uint64) string {
|
||||
@@ -105,3 +107,24 @@ func ToJSONString(status interface{}) string {
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// TerminalDisplayWidth returns the number of terminal cells needed to display s
|
||||
func TerminalDisplayWidth(s string) int {
|
||||
width := 0
|
||||
for _, r := range s {
|
||||
width += terminalDisplayRuneWidth(r)
|
||||
}
|
||||
|
||||
return width
|
||||
}
|
||||
|
||||
func terminalDisplayRuneWidth(r rune) int {
|
||||
switch width.LookupRune(r).Kind() {
|
||||
case width.EastAsianWide, width.EastAsianFullwidth:
|
||||
return 2
|
||||
case width.EastAsianNarrow, width.EastAsianHalfwidth, width.EastAsianAmbiguous, width.Neutral:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user