| 1 |
package cellbuf |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
var wrapCases = []struct { |
| 9 |
name string |
| 10 |
input string |
| 11 |
expected string |
| 12 |
width int |
| 13 |
}{ |
| 14 |
{ |
| 15 |
name: "simple", |
| 16 |
input: "I really \x1B[38;2;249;38;114mlove the\x1B[0m Go language!", |
| 17 |
expected: "I really \x1B[38;2;249;38;114mlove\x1b[m\n\x1B[38;2;249;38;114mthe\x1B[0m Go\nlanguage!", |
| 18 |
width: 14, |
| 19 |
}, |
| 20 |
{ |
| 21 |
name: "passthrough", |
| 22 |
input: "hello world", |
| 23 |
expected: "hello world", |
| 24 |
width: 11, |
| 25 |
}, |
| 26 |
{ |
| 27 |
name: "asian", |
| 28 |
input: "こんにち", |
| 29 |
expected: "こんに\nち", |
| 30 |
width: 7, |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "emoji", |
| 34 |
input: "😃👰🏻♀️🫧", |
| 35 |
expected: "😃\n👰🏻♀️\n🫧", |
| 36 |
width: 2, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "long style", |
| 40 |
input: "\x1B[38;2;249;38;114ma really long string\x1B[0m", |
| 41 |
expected: "\x1B[38;2;249;38;114ma really\x1b[m\n\x1B[38;2;249;38;114mlong\x1b[m\n\x1B[38;2;249;38;114mstring\x1B[0m", |
| 42 |
width: 10, |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "long style nbsp", |
| 46 |
input: "\x1B[38;2;249;38;114ma really\u00a0long string\x1B[0m", |
| 47 |
expected: "\x1b[38;2;249;38;114ma\x1b[m\n\x1b[38;2;249;38;114mreally\u00a0lon\x1b[m\n\x1b[38;2;249;38;114mg string\x1b[0m", |
| 48 |
width: 10, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "longer", |
| 52 |
input: "the quick brown foxxxxxxxxxxxxxxxx jumped over the lazy dog.", |
| 53 |
expected: "the quick brown\nfoxxxxxxxxxxxxxx\nxx jumped over\nthe lazy dog.", |
| 54 |
width: 16, |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "longer asian", |
| 58 |
input: "猴 猴 猴猴 猴猴猴猴猴猴猴猴猴 猴猴猴 猴猴 猴’ 猴猴 猴.", |
| 59 |
expected: "猴 猴 猴猴\n猴猴猴猴猴猴猴猴\n猴 猴猴猴 猴猴\n猴’ 猴猴 猴.", |
| 60 |
width: 16, |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "long input", |
| 64 |
input: "Rotated keys for a-good-offensive-cheat-code-incorporated/animal-like-law-on-the-rocks.", |
| 65 |
expected: "Rotated keys for a-good-offensive-cheat-code-incorporated/animal-like-law-\non-the-rocks.", |
| 66 |
width: 76, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "long input2", |
| 70 |
input: "Rotated keys for a-good-offensive-cheat-code-incorporated/crypto-line-operating-system.", |
| 71 |
expected: "Rotated keys for a-good-offensive-cheat-code-incorporated/crypto-line-\noperating-system.", |
| 72 |
width: 76, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "hyphen breakpoint", |
| 76 |
input: "a-good-offensive-cheat-code", |
| 77 |
expected: "a-good-\noffensive-\ncheat-code", |
| 78 |
width: 10, |
| 79 |
}, |
| 80 |
{ |
| 81 |
name: "exact", |
| 82 |
input: "\x1b[91mfoo\x1b[0m", |
| 83 |
expected: "\x1b[91mfoo\x1b[0m", |
| 84 |
width: 3, |
| 85 |
}, |
| 86 |
{ |
| 87 |
// XXX: Should we preserve spaces on text wrapping? |
| 88 |
name: "extra space", |
| 89 |
input: "foo ", |
| 90 |
expected: "foo", |
| 91 |
width: 3, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "extra space style", |
| 95 |
input: "\x1b[mfoo \x1b[m", |
| 96 |
expected: "\x1b[mfoo\x1b[m", |
| 97 |
width: 3, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "paragraph with styles", |
| 101 |
input: "Lorem ipsum dolor \x1b[1msit\x1b[m amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \x1b[31mUt enim\x1b[m ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \x1b[38;5;200mcommodo consequat\x1b[m. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. \x1b[1;2;33mExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\x1b[m", |
| 102 |
expected: "Lorem ipsum dolor \x1b[1msit\x1b[m amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor\nincididunt ut labore et dolore\nmagna aliqua. \x1b[31mUt enim\x1b[m ad minim\nveniam, quis nostrud\nexercitation ullamco laboris\nnisi ut aliquip ex ea \x1b[38;5;200mcommodo\x1b[m\n\x1b[38;5;200mconsequat\x1b[m. Duis aute irure\ndolor in reprehenderit in\nvoluptate velit esse cillum\ndolore eu fugiat nulla\npariatur. \x1b[1;2;33mExcepteur sint\x1b[m\n\x1b[1;2;33moccaecat cupidatat non\x1b[m\n\x1b[1;2;33mproident, sunt in culpa qui\x1b[m\n\x1b[1;2;33mofficia deserunt mollit anim\x1b[m\n\x1b[1;2;33mid est laborum.\x1b[m", |
| 103 |
width: 30, |
| 104 |
}, |
| 105 |
{"hyphen break", "foo-bar", "foo-\nbar", 5}, |
| 106 |
{"double space", "f bar foobaz", "f bar\nfoobaz", 6}, |
| 107 |
{"passthrough", "foobar\n ", "foobar\n ", 0}, |
| 108 |
{"pass", "foo", "foo", 3}, |
| 109 |
{"toolong", "foobarfoo", "foob\narfo\no", 4}, |
| 110 |
{"white space", "foo bar foo", "foo\nbar\nfoo", 4}, |
| 111 |
{"broken_at_spaces", "foo bars foobars", "foo\nbars\nfoob\nars", 4}, |
| 112 |
{"hyphen", "foob-foobar", "foob\n-foo\nbar", 4}, |
| 113 |
{"wide_emoji_breakpoint", "foo🫧 foobar", "foo\n🫧\nfoob\nar", 4}, |
| 114 |
{"space_breakpoint", "foo --bar", "foo --bar", 9}, |
| 115 |
{"simple", "foo bars foobars", "foo\nbars\nfoob\nars", 4}, |
| 116 |
{"limit", "foo bar", "foo\nbar", 5}, |
| 117 |
{"remove white spaces", "foo \nb ar ", "foo\nb\nar", 4}, |
| 118 |
{"white space trail width", "foo\nb\t a\n bar", "foo\nb\t a\n bar", 4}, |
| 119 |
{"explicit_line_break", "foo bar foo\n", "foo\nbar\nfoo\n", 4}, |
| 120 |
{"explicit_breaks", "\nfoo bar\n\n\nfoo\n", "\nfoo\nbar\n\n\nfoo\n", 4}, |
| 121 |
{"example", " This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* foo \nbar ", " This\nis a\nlist: \n\n\t* foo\n\t* bar\n\n\n\t* foo\nbar", 6}, |
| 122 |
{"style_code_dont_affect_length", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", 7}, |
| 123 |
{"style_code_dont_get_wrapped", "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m", "\x1b[38;2;249;38;114m(\x1b[0m\x1b[38;2;248;248;242mjust\x1b[m\n\x1b[38;2;248;248;242manother\x1b[m\n\x1b[38;2;248;248;242mtest\x1b[38;2;249;38;114m)\x1b[0m", 7}, |
| 124 |
{"osc8_wrap", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\ สวัสดีสวัสดี\x1b]8;;\x1b\\", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\\x1b]8;;\x07\n\x1b]8;;https://example.com\x07สวัสดีสวัสดี\x1b]8;;\x1b\\", 8}, |
| 125 |
{"tab", "foo\tbar", "foo\nbar", 3}, |
| 126 |
{"wrapped styles example", "", "", 10}, |
| 127 |
{ |
| 128 |
name: "punctuation after formatted word with space", |
| 129 |
input: "\x1b[38;5;203;48;5;236m arm64 \x1b[0m, \x1b[38;5;203;48;5;236m amd64 \x1b[0m, \x1b[38;5;203;48;5;236m i386 \x1b[0m", |
| 130 |
expected: "\x1b[38;5;203;48;5;236m arm64 \x1b[0m,\n\x1b[38;5;203;48;5;236m amd64 \x1b[0m, \x1b[38;5;203;48;5;236m i386 \x1b[0m", |
| 131 |
width: 15, |
| 132 |
}, |
| 133 |
} |
| 134 |
|
| 135 |
func TestWrap(t *testing.T) { |
| 136 |
for i, tc := range wrapCases { |
| 137 |
t.Run(tc.name, func(t *testing.T) { |
| 138 |
output := Wrap(tc.input, tc.width, "") |
| 139 |
if output != tc.expected { |
| 140 |
t.Errorf("case %d, input:\n%q\nexpected:\n%q\n%s\n\ngot:\n%q\n%s", i+1, tc.input, tc.expected, tc.expected, output, output) |
| 141 |
} |
| 142 |
}) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func ExampleWrap() { |
| 147 |
fmt.Println(Wrap("The quick brown fox jumped over the lazy dog.", 20, "")) |
| 148 |
// Output: |
| 149 |
// The quick brown fox |
| 150 |
// jumped over the lazy |
| 151 |
// dog. |
| 152 |
} |