Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion columnize.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ func elementsFromLine(config *Config, line string) []interface{} {
return elements
}

// runeLen calculates the number of visible "characters" in a string
func runeLen(s string) int {
l := 0
for _ = range s {
l++
}
return l
}

// widthsFromLines examines a list of strings and determines how wide each
// column should be considering all of the elements that need to be printed
// within it.
Expand All @@ -106,7 +115,7 @@ func widthsFromLines(config *Config, lines []string) []int {
for _, line := range lines {
elems := elementsFromLine(config, line)
for i := 0; i < len(elems); i++ {
l := len(elems[i].(string))
l := runeLen(elems[i].(string))
if len(widths) <= i {
widths = append(widths, l)
} else if widths[i] < l {
Expand Down
25 changes: 24 additions & 1 deletion columnize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,30 @@ func TestColumnWidthCalculator(t *testing.T) {
expected += "short short short"

if output != expected {
t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output)
printableProof := fmt.Sprintf("\nGot: %+q", output)
printableProof += fmt.Sprintf("\nExpected: %+q", expected)
t.Fatalf("\n%s", printableProof)
}
}

func TestColumnWidthCalculatorNonASCII(t *testing.T) {
input := []string{
"Column A | Column B | Column C",
"⌘⌘⌘⌘⌘⌘⌘⌘ | Longer than B | Longer than C",
"short | short | short",
}

config := DefaultConfig()
output := Format(input, config)

expected := "Column A Column B Column C\n"
expected += "⌘⌘⌘⌘⌘⌘⌘⌘ Longer than B Longer than C\n"
expected += "short short short"

if output != expected {
printableProof := fmt.Sprintf("\nGot: %+q", output)
printableProof += fmt.Sprintf("\nExpected: %+q", expected)
t.Fatalf("\n%s", printableProof)
}
}

Expand Down