mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-17 14:04:52 +02:00
Allow passing multiple options to control how wrapping is done
This commit is contained in:
@@ -468,9 +468,12 @@ func (self *wrapper) wrap_text(text string) []string {
|
||||
return self.lines
|
||||
}
|
||||
|
||||
func new_wrapper(indent string, width int) *wrapper {
|
||||
func new_wrapper(opts WrapOptions, width int) *wrapper {
|
||||
width = utils.Max(2, width)
|
||||
ans := wrapper{indent: indent, width: width, indent_width: wcswidth.Stringwidth(indent)}
|
||||
ans := wrapper{indent: opts.Indent, width: width, indent_width: wcswidth.Stringwidth(opts.Indent)}
|
||||
if opts.Ignore_lines_containing != "" {
|
||||
ans.ignore_lines_containing = utils.Splitlines(opts.Ignore_lines_containing)
|
||||
}
|
||||
ans.ep.HandleRune = ans.handle_rune
|
||||
ans.ep.HandleCSI = ans.handle_csi
|
||||
ans.ep.HandleOSC = ans.handle_osc
|
||||
@@ -480,12 +483,17 @@ func new_wrapper(indent string, width int) *wrapper {
|
||||
return &ans
|
||||
}
|
||||
|
||||
func WrapTextAsLines(text string, indent string, width int, ignore_lines_containing ...string) []string {
|
||||
w := new_wrapper(indent, width)
|
||||
w.ignore_lines_containing = ignore_lines_containing
|
||||
type WrapOptions struct {
|
||||
Ignore_lines_containing string
|
||||
Trim_whitespace bool // trim whitespace at the start and end of lines (start is after indent)
|
||||
Indent string
|
||||
}
|
||||
|
||||
func WrapTextAsLines(text string, width int, opts WrapOptions) []string {
|
||||
w := new_wrapper(opts, width)
|
||||
return w.wrap_text(text)
|
||||
}
|
||||
|
||||
func WrapText(text string, indent string, width int, ignore_lines_containing ...string) string {
|
||||
return strings.Join(WrapTextAsLines(text, indent, width, ignore_lines_containing...), "\n")
|
||||
func WrapText(text string, width int, opts WrapOptions) string {
|
||||
return strings.Join(WrapTextAsLines(text, width, opts), "\n")
|
||||
}
|
||||
|
||||
@@ -9,28 +9,29 @@ import (
|
||||
|
||||
func TestFormatWithIndent(t *testing.T) {
|
||||
|
||||
screen_width, indent := 0, ""
|
||||
screen_width := 0
|
||||
opts := WrapOptions{}
|
||||
|
||||
tx := func(text string, expected ...string) {
|
||||
q := indent + strings.Join(expected, "")
|
||||
actual := WrapText(text, indent, screen_width)
|
||||
q := opts.Indent + strings.Join(expected, "")
|
||||
actual := WrapText(text, screen_width, opts)
|
||||
if actual != q {
|
||||
t.Fatalf("\nFailed for: %#v\nexpected: %#v\nactual: %#v", text, q, actual)
|
||||
}
|
||||
}
|
||||
|
||||
indent = ""
|
||||
opts.Indent = ""
|
||||
screen_width = 4
|
||||
tx("one two", "one \ntwo")
|
||||
tx("a b", "a b")
|
||||
screen_width = 3
|
||||
tx("one tw", "one\n tw")
|
||||
|
||||
indent = "__"
|
||||
opts.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)
|
||||
a := strings.Repeat("a", screen_width-len(opts.Indent)-1)
|
||||
tx(a+" b", a+" \n__b")
|
||||
|
||||
tx("123456 \x1b[31m789a", "123456 \n__\x1b[31m789a")
|
||||
@@ -40,7 +41,8 @@ func TestFormatWithIndent(t *testing.T) {
|
||||
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")
|
||||
indent = ""
|
||||
|
||||
opts.Indent = ""
|
||||
screen_width = 3
|
||||
tx("one", "one")
|
||||
tx("four", "fou\nr")
|
||||
|
||||
Reference in New Issue
Block a user