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

Add TruncatePrefix #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions runewidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,38 @@ func (c *Condition) TruncateLeft(s string, w int, prefix string) string {
return prefix + s[pos:]
}

// Truncate return string truncated with w cells from prefix part (left part)
func (c *Condition) TruncatePrefix(s string, w int, prefix string) string {
if c.StringWidth(prefix) >= w {
return prefix
}

sw := c.StringWidth(s)
if sw <= w {
return s
}
w -= c.StringWidth(prefix)
var width int
var pos int
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 sw-(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 @@ -333,6 +365,11 @@ func TruncateLeft(s string, w int, prefix string) string {
return DefaultCondition.TruncateLeft(s, w, prefix)
}

// Truncate return string truncated with w cells from prefix part (left part)
func TruncatePrefix(s string, w int, prefix string) string {
return DefaultCondition.TruncatePrefix(s, w, prefix)
}

// Wrap return string wrapped with w cells
func Wrap(s string, w int) string {
return DefaultCondition.Wrap(s, w)
Expand Down
34 changes: 34 additions & 0 deletions runewidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,40 @@ func TestTruncateLeft(t *testing.T) {
}
}

var truncateprefixtests = []struct {
s string
w int
prefix string
out string
}{
{"source", 4, "*", "*rce"},
{"source", 6, "*", "source"},
{"あいうえお", 4, "*", "*お"},
{"あいうえお", 10, "*", "あいうえお"},
{"Aあいうえお", 5, "*", "*えお"},

{"source", 4, "", "urce"},
{"source", 4, "...", "...e"},
{"あいうえお", 6, "", "うえお"},
{"あいうえお", 6, "...", "...お"},
{"あいうえお", 10, "", "あいうえお"},
{"あいうえお", 10, "...", "あいうえお"},
{"あいうえお", 5, "", "えお"},

{"source", 1, "*", "*"},
{"source", 0, "*", "*"}, // same as Truncate
}

func TestTruncatePrefix(t *testing.T) {
t.Parallel()

for _, tt := range truncateprefixtests {
if out := TruncatePrefix(tt.s, tt.w, tt.prefix); out != tt.out {
t.Errorf("TruncatePrefix(%q) = %q, want %q", tt.s, out, tt.out)
}
}
}

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