handle unicode input correctly
diff --git a/colwriter/column.go b/colwriter/column.go
index c03f43a..7302ce9 100644
--- a/colwriter/column.go
+++ b/colwriter/column.go
@@ -8,6 +8,7 @@
import (
"bytes"
"io"
+ "unicode/utf8"
)
const (
@@ -107,8 +108,8 @@
}
maxwidth := 0
for _, wd := range words {
- if len(wd) > maxwidth {
- maxwidth = len(wd)
+ if n := utf8.RuneCount(wd); n > maxwidth {
+ maxwidth = n
}
}
maxwidth++ // space char
@@ -126,7 +127,7 @@
if err != nil {
return err
}
- col += len(words[j])
+ col += utf8.RuneCount(words[j])
if j+nlines < len(words) {
for col < endcol {
_, err := w.w.Write([]byte{' '})
diff --git a/colwriter/column_test.go b/colwriter/column_test.go
index 61bd12e..8d0bf8f 100644
--- a/colwriter/column_test.go
+++ b/colwriter/column_test.go
@@ -60,6 +60,15 @@
git.go linux.go plugin.go ssh.go unix.go windows.go
help.go ls.go run.go tail.go update.go
`[1:]},
+ {20, 0, `
+Hello
+Γειά σου
+안녕
+今日は
+`[1:], `
+Hello 안녕
+Γειά σου 今日は
+`[1:]},
}
func TestWriter(t *testing.T) {