feat(textutil): add width-aware text wrapping and clipping
Wrap, Clip, and Pad plain text to a fixed rune width for the terminal panes.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package textutil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestClip(t *testing.T) {
|
||||
if got := Clip("abcdef", 4); got != "abc…" {
|
||||
t.Fatalf("clip: %q", got)
|
||||
}
|
||||
if got := Clip("ab", 4); got != "ab" {
|
||||
t.Fatalf("short: %q", got)
|
||||
}
|
||||
if got := Clip("a\nb", 5); got != "a b" {
|
||||
t.Fatalf("newline: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrap(t *testing.T) {
|
||||
got := Wrap("one two three", 7)
|
||||
want := []string{"one two", "three"}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("wrap %#v", got)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("line %d: %q", i, got[i])
|
||||
}
|
||||
}
|
||||
blank := Wrap("a\n\nb", 10)
|
||||
if len(blank) != 3 || blank[1] != "" {
|
||||
t.Fatalf("blank lines: %#v", blank)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user