Skip to content

Commit

Permalink
Fix output alignment with Unicode characters (#324)
Browse files Browse the repository at this point in the history
Resolves #322.
  • Loading branch information
jotaen authored Aug 2, 2024
1 parent 634b355 commit 3ade1d8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
13 changes: 13 additions & 0 deletions klog/app/cli/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ Was #sick, need to compensate later
`, state.printBuffer)
}

func TestPrintTagsWithUnicodeCharacters(t *testing.T) {
state, err := NewTestingContext()._SetRecords(`
1995-03-17
1h #ascii
2h #üñïčödę
`)._Run((&Tags{}).Run)
require.Nil(t, err)
assert.Equal(t, `
#ascii 1h
#üñïčödę 2h
`, state.printBuffer)
}

func TestPrintTagsWithCount(t *testing.T) {
state, err := NewTestingContext()._SetRecords(`
1995-03-17
Expand Down
7 changes: 5 additions & 2 deletions klog/app/cli/terminalformat/table.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package terminalformat

import "strings"
import (
"strings"
"unicode/utf8"
)

type Options struct {
fill bool
Expand Down Expand Up @@ -45,7 +48,7 @@ func (t *Table) Cell(text string, opts Options) *Table {
c := cell{
Options: opts,
value: text,
len: len(StripAllAnsiSequences(text)),
len: utf8.RuneCountInString(StripAllAnsiSequences(text)),
}
t.cells = append(t.cells, c)
if c.len > t.longestCell[t.currentColumn] {
Expand Down
16 changes: 16 additions & 0 deletions klog/app/cli/terminalformat/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ long-text `+"\x1b[0m\x1b[4m"+`asdf`+"\x1b[0m"+` -----
foo
`, result)
}

func TestPrintTableWithUnicode(t *testing.T) {
result := ""
table := NewTable(3, " ")
table.
Cell("FIRST", Options{align: ALIGN_LEFT}).
Cell("SECOND", Options{align: ALIGN_LEFT}).
Cell("THIRD", Options{align: ALIGN_LEFT}).
CellL("first").
CellR("șëčøñd").
CellR("third")
table.Collect(func(x string) { result += x })
assert.Equal(t, `FIRST SECOND THIRD
first șëčøñd third
`, result)
}

0 comments on commit 3ade1d8

Please sign in to comment.