From 4596dc39cefab197a077852b18cf9b22bcc5faba Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 17 Aug 2022 13:58:01 +0530 Subject: [PATCH] Fix formatting of lines with only spaces --- tools/cli/infrastructure.go | 4 ++++ tools/cli/infrastructure_test.go | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tools/cli/infrastructure.go b/tools/cli/infrastructure.go index 313163bcd..b852798aa 100644 --- a/tools/cli/infrastructure.go +++ b/tools/cli/infrastructure.go @@ -81,6 +81,10 @@ var blue_fmt = color.New(color.FgBlue).SprintFunc() var green_fmt = color.New(color.FgGreen).SprintFunc() func format_line_with_indent(output io.Writer, text string, indent string, screen_width int) { + if strings.TrimSpace(text) == "" { + fmt.Fprintln(output, indent) + return + } x := len(indent) fmt.Fprint(output, indent) in_escape := 0 diff --git a/tools/cli/infrastructure_test.go b/tools/cli/infrastructure_test.go index 656f15598..ec1340f43 100644 --- a/tools/cli/infrastructure_test.go +++ b/tools/cli/infrastructure_test.go @@ -5,14 +5,20 @@ import ( "testing" ) -func TestFormatLineWithIndent(t *testing.T) { +func TestFormatWithIndent(t *testing.T) { var output strings.Builder + indent := "__" + screen_width := 11 - output.Reset() - indent := " " - format_line_with_indent(&output, "testing \x1b[31mstyled\x1b[m", indent, 11) - expected := indent + "testing \n" + indent + "\x1b[31mstyled\x1b[m\n" - if output.String() != expected { - t.Fatalf("%#v != %#v", expected, output.String()) + run := func(text string, expected ...string) { + output.Reset() + q := indent + strings.Join(expected, "\n"+indent) + "\n" + format_with_indent(&output, text, indent, screen_width) + if output.String() != q { + t.Fatalf("expected != actual: %#v != %#v", q, output.String()) + } } + run("testing \x1b[31mstyled\x1b[m", "testing ", "\x1b[31mstyled\x1b[m") + run("testing\n\ntwo", "testing", "", "two") + run("testing\n \ntwo", "testing", "", "two") }