Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trim prefix #65

Merged
merged 7 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions runewidth.go
Expand Up @@ -214,6 +214,35 @@ func (c *Condition) Truncate(s string, w int, tail string) string {
return s[:pos] + tail
}

// TrimPrefix cuts w cells from the beginning of the `s`.
func (c *Condition) TrimPrefix(s string, w int, prefix string) string {
if c.StringWidth(s) <= w {
return prefix
}

var width int
pos := len(s)

g := uniseg.NewGraphemes(s)
for g.Next() {
var chWidth int
for _, r := range g.Runes() {
chWidth = c.RuneWidth(r)
if chWidth > 0 {
break // See StringWidth() for details.
}
}
if width+chWidth > w {
pos, _ = g.Positions()

break
}
width += chWidth
}

return prefix + s[pos:]
}

// Wrap return string wrapped with w cells
func (c *Condition) Wrap(s string, w int) string {
width := 0
Expand Down Expand Up @@ -291,6 +320,11 @@ func Truncate(s string, w int, tail string) string {
return DefaultCondition.Truncate(s, w, tail)
}

// TrimPrefix cuts w cells from the beginning of the `s`.
func TrimPrefix(s string, w int, prefix string) string {
return DefaultCondition.TrimPrefix(s, w, prefix)
}

// Wrap return string wrapped with w cells
func Wrap(s string, w int) string {
return DefaultCondition.Wrap(s, w)
Expand Down
70 changes: 70 additions & 0 deletions runewidth_test.go
Expand Up @@ -380,6 +380,76 @@ func TestTruncateNoNeeded(t *testing.T) {
}
}

func Test_TrimPrefix(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove _

t.Parallel()

t.Run("ascii", func(t *testing.T) {
t.Parallel()
s := "source"
expected := "ce"

out := TrimPrefix(s, 4, "")
if out != expected {
t.Errorf("TrimPrefix(%q) = %q, want %q", s, out, expected)
}
})

t.Run("ascii: with prefix", func(t *testing.T) {
t.Parallel()
s := "source"
expected := "...ce"

out := TrimPrefix(s, 4, "...")
if out != expected {
t.Errorf("TrimPrefix(%q) = %q, want %q", s, out, expected)
}
})

t.Run("non ascii", func(t *testing.T) {
t.Parallel()
s := "あいうえお"
expected := "えお"

out := TrimPrefix(s, 6, "")
if out != expected {
t.Errorf("TrimPrefix(%q) = %q, want %q", s, out, expected)
}
})

t.Run("non ascii: with prefix", func(t *testing.T) {
t.Parallel()
s := "あいうえお"
expected := "...えお"

out := TrimPrefix(s, 6, "...")
if out != expected {
t.Errorf("TrimPrefix(%q) = %q, want %q", s, out, expected)
}
})

t.Run("trim all", func(t *testing.T) {
t.Parallel()
s := "あいうえお"
expected := ""

out := TrimPrefix(s, 10, "")
if out != expected {
t.Errorf("TrimPrefix(%q) = %q, want %q", s, out, expected)
}
})

t.Run("trim all: with prefix", func(t *testing.T) {
t.Parallel()
s := "あいうえお"
expected := "..."

out := TrimPrefix(s, 10, "...")
if out != expected {
t.Errorf("TrimPrefix(%q) = %q, want %q", s, out, expected)
}
})
}

var isneutralwidthtests = []struct {
in rune
out bool
Expand Down