Dont remove leading and trailing spaces when wrapping

Without this we lose some spaces and also there was a case where the
line could end up longer than the specified width.
This commit is contained in:
Kovid Goyal
2023-03-29 20:41:05 +05:30
parent 7169a89591
commit cb99fbd83c
2 changed files with 27 additions and 50 deletions

View File

@@ -8,29 +8,38 @@ import (
)
func TestFormatWithIndent(t *testing.T) {
indent := "__"
screen_width := 11
screen_width, indent := 0, ""
tx := func(text string, expected ...string) {
q := indent + strings.Join(expected, "")
actual := WrapText(text, indent, screen_width)
if actual != q {
t.Fatalf("%#v\nexpected: %#v\nactual: %#v", text, q, actual)
t.Fatalf("\nFailed for: %#v\nexpected: %#v\nactual: %#v", text, q, actual)
}
}
tx("testing\n\ntwo", "testing\n\n__two")
tx("testing\n \ntwo", "testing\n\n__two")
a := strings.Repeat("a", screen_width-len(indent)-1)
tx(a+" b", a+"\n__b")
indent = ""
screen_width = 4
tx("one two", "one \ntwo")
tx("a b", "a b")
screen_width = 3
tx("one tw", "one\n tw")
tx("123456 \x1b[31m789a", "123456\n__\x1b[31m789a")
tx("12 \x1b[31m789 abcd", "12 \x1b[31m789\n\x1b[39m__\x1b[31mabcd")
tx("bb \x1b]8;;http://xyz.com\x1b\\text\x1b]8;;\x1b\\ two", "bb \x1b]8;;http://xyz.com\x1b\\text\x1b]8;;\x1b\\\n__two")
tx("\x1b[31maaaaaa \x1b[39mbbbbbb", "\x1b[31maaaaaa\n\x1b[39m__\x1b[31m\x1b[39mbbbbbb")
indent = "__"
screen_width = 11
tx("testing\n\ntwo", "testing\n\n__two")
tx("testing\n \ntwo", "testing\n__ \n__two")
a := strings.Repeat("a", screen_width-len(indent)-1)
tx(a+" b", a+" \n__b")
tx("123456 \x1b[31m789a", "123456 \n__\x1b[31m789a")
tx("12 \x1b[31m789 abcd", "12 \x1b[31m789 \n\x1b[39m__\x1b[31mabcd")
tx("bb \x1b]8;;http://xyz.com\x1b\\text\x1b]8;;\x1b\\ two", "bb \x1b]8;;http://xyz.com\x1b\\text\x1b]8;;\x1b\\ \n__two")
tx("\x1b[31maaaaaa \x1b[39mbbbbbb", "\x1b[31maaaaaa \n\x1b[39m__\x1b[31m\x1b[39mbbbbbb")
tx(
"\x1b[31;4:3m\x1b]8;;XXX\x1b\\combined using\x1b]8;;\x1b\\ operators",
"\x1b[31;4:3m\x1b]8;;XXX\x1b\\combined\n\x1b[4:0;39m\x1b]8;;\x1b\\__\x1b[4:3;31m\x1b]8;;XXX\x1b\\using\x1b]8;;\x1b\\\n\x1b[4:0;39m__\x1b[4:3;31moperators")
"\x1b[31;4:3m\x1b]8;;XXX\x1b\\combined \n\x1b[4:0;39m\x1b]8;;\x1b\\__\x1b[4:3;31m\x1b]8;;XXX\x1b\\using\x1b]8;;\x1b\\ \n\x1b[4:0;39m__\x1b[4:3;31moperators")
indent = ""
screen_width = 3
tx("one", "one")
@@ -41,6 +50,6 @@ func TestFormatWithIndent(t *testing.T) {
screen_width = 8
tx(
"\x1b[1mbold\x1b[221m no more bold",
"\x1b[1mbold\x1b[221m no\nmore\nbold",
"\x1b[1mbold\x1b[221m no \nmore \nbold",
)
}