Skip to content

Commit

Permalink
chore(ansi): if there's nothing to Cut, return the empty string (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm authored Jan 9, 2025
1 parent 18c3144 commit 9d5df1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ansi/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
// wide-characters (such as East-Asian characters and emojis). Note that the
// [left] parameter is inclusive, while [right] isn't.
func Cut(s string, left, right int) string {
if right <= left {
return ""
}

if left == 0 {
return Truncate(s, right, "")
}
Expand Down
11 changes: 8 additions & 3 deletions ansi/truncate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,17 @@ func TestCut(t *testing.T) {
{
"right is 0",
"\x1b[7mHello\x1b[m", 3, 0,
"\x1b[7m\x1b[m",
"",
},
{
"right is less than left",
"\x1b[7mHello\x1b[m", 3, 2,
"",
},
{
"cut size is 0",
"\x1b[7mHello\x1b[m", 2, 2,
"\x1b[7m\x1b[m",
"",
},
{
"maintains open ansi",
Expand All @@ -343,7 +348,7 @@ func TestCut(t *testing.T) {
t.Run(c.input, func(t *testing.T) {
got := Cut(c.input, c.left, c.right)
if got != c.expect {
t.Errorf("%s (%d):\nexpected: %q\ngot: %q", c.desc, i+1, c.expect, got)
t.Errorf("%s (#%d):\nexpected: %q\ngot: %q", c.desc, i+1, c.expect, got)
}
})
}
Expand Down

0 comments on commit 9d5df1d

Please sign in to comment.