Skip to content

Commit

Permalink
Fix table layout for multi-line values (#52)
Browse files Browse the repository at this point in the history
Truncate multiline entries to the first line
  • Loading branch information
gairadzi authored Mar 13, 2022
1 parent 47d0cf2 commit 4071c81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 15 additions & 1 deletion table/strlimit.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package table

import "github.com/mattn/go-runewidth"
import (
"strings"

"github.com/mattn/go-runewidth"
)

func limitStr(str string, maxLen int) string {
if maxLen == 0 {
return ""
}

newLineIndex := strings.Index(str, "\n")
if newLineIndex > -1 {
str = str[:newLineIndex]
}

if runewidth.StringWidth(str) > maxLen {
return runewidth.Truncate(str, maxLen-1, "") + "…"
}

if newLineIndex > -1 {
return str + "…"
}

return str
}
6 changes: 6 additions & 0 deletions table/strlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func TestLimitStr(t *testing.T) {
max: 5,
expected: "直立…",
},
{
name: "multiline truncated",
input: "hi\nall",
max: 5,
expected: "hi…",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 4071c81

Please sign in to comment.