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:
Florian Thoma
2024-06-05 09:33:15 +02:00
parent 660679c2f6
commit e9de9684f4
4 changed files with 51 additions and 8 deletions
+18
View File
@@ -84,3 +84,21 @@ func TestParseBytesInvalid(t *testing.T) {
test.Equals(t, int64(0), v)
}
}
func TestTerminalDisplayWidth(t *testing.T) {
for _, c := range []struct {
input string
want int
}{
{"foo", 3},
{"aéb", 3},
{"ab", 3},
{"ab", 3},
{"aあb", 4},
} {
if got := TerminalDisplayWidth(c.input); got != c.want {
t.Errorf("wrong display width for '%s', want %d, got %d", c.input, c.want, got)
}
}
}