Skip to content

Commit

Permalink
Fix edge case for multiline truncation on exact max length (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras authored Mar 13, 2022
1 parent 4071c81 commit 534c809
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 1 addition & 5 deletions table/strlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ func limitStr(str string, maxLen int) string {

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

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

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

return str
}
8 changes: 7 additions & 1 deletion table/strlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@ func TestLimitStr(t *testing.T) {
expected: "直立…",
},
{
name: "multiline truncated",
name: "Multiline truncated",
input: "hi\nall",
max: 5,
expected: "hi…",
},
{
name: "Multiline with exact max width",
input: "hello\nall",
max: 5,
expected: "hell…",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 534c809

Please sign in to comment.