From 3d79eb5730d11fbbd9c4ccd67d7f7c7e385be5cd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 3 Sep 2022 14:40:49 +0530 Subject: [PATCH] more tests for indent and wrap --- tools/cli/infrastructure.go | 4 +++- tools/utils/style/indent-and-wrap.go | 10 ++++------ tools/utils/style/indent-and-wrap_test.go | 10 +++++++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tools/cli/infrastructure.go b/tools/cli/infrastructure.go index 5ded2f103..6cfac8628 100644 --- a/tools/cli/infrastructure.go +++ b/tools/cli/infrastructure.go @@ -232,7 +232,9 @@ func prettify(text string) string { } func format_with_indent(output io.Writer, text string, indent string, screen_width int) { - io.WriteString(output, style.WrapText(prettify(text), indent, screen_width, "#placeholder_for_formatting#")) + text = prettify(text) + indented := style.WrapText(text, indent, screen_width, "#placeholder_for_formatting#") + io.WriteString(output, indented) } func full_command_name(cmd *cobra.Command) string { diff --git a/tools/utils/style/indent-and-wrap.go b/tools/utils/style/indent-and-wrap.go index 77feccffc..90940cb3a 100644 --- a/tools/utils/style/indent-and-wrap.go +++ b/tools/utils/style/indent-and-wrap.go @@ -8,7 +8,6 @@ import ( "strings" "unicode" - "kitty/tools/utils" "kitty/tools/wcswidth" ) @@ -302,8 +301,7 @@ func (self *line_builder) add_escape_code2(prefix string, body []byte, suffix st } type escape_code_ struct { - prefix, suffix string - body []byte + prefix, body, suffix string } type word_builder struct { @@ -334,7 +332,7 @@ func (self *word_builder) width() int { } func (self *word_builder) add_escape_code(prefix string, body []byte, suffix string) { - e := escape_code_{prefix: prefix, body: body, suffix: suffix} + e := escape_code_{prefix: prefix, body: string(body), suffix: suffix} self.escape_codes = append(self.escape_codes, e) self.buf.WriteString(prefix) self.buf.Write(body) @@ -419,9 +417,9 @@ func (self *wrapper) print_word() { } for _, e := range self.current_word.escape_codes { if e.suffix != "" { - self.hyperlink.apply_osc(utils.UnsafeBytesToString(e.body)) + self.hyperlink.apply_osc(e.body) } else { - self.sgr.apply_csi(utils.UnsafeBytesToString(e.body)) + self.sgr.apply_csi(e.body) } } self.current_line.add_word(self.current_word.reset(), w) diff --git a/tools/utils/style/indent-and-wrap_test.go b/tools/utils/style/indent-and-wrap_test.go index c1e1fefbb..31022b92c 100644 --- a/tools/utils/style/indent-and-wrap_test.go +++ b/tools/utils/style/indent-and-wrap_test.go @@ -18,10 +18,18 @@ func TestFormatWithIndent(t *testing.T) { t.Fatalf("%#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") tx("123456 \x1b[31m789a", "123456\n__\x1b[31m789a") tx("12 \x1b[31m789 abcd", "12 \x1b[31m789\n\x1b[39m__\x1b[31mabcd") - tx("\x1b]8;;x\x1b\\text\x1b]8;;\x1b\\ two", "\x1b]8;;x\x1b\\text\x1b]8;;\x1b\\ two") + 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") + }