From 7c0b6a82db524b7e2e6711a7cee190ec532a2ca8 Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Sat, 17 Oct 2020 20:15:30 +0200 Subject: [PATCH 1/3] Remove unused public method ui.linesWriter.Flush --- internal/ui/stdio_wrapper.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/internal/ui/stdio_wrapper.go b/internal/ui/stdio_wrapper.go index eccaefb7b..a3b5c178f 100644 --- a/internal/ui/stdio_wrapper.go +++ b/internal/ui/stdio_wrapper.go @@ -74,13 +74,9 @@ func (w *lineWriter) Write(data []byte) (n int, err error) { return n, err } -func (w *lineWriter) Flush() error { +func (w *lineWriter) Close() error { if w.buf.Len() > 0 { w.print(string(append(w.buf.Bytes(), '\n'))) } return nil } - -func (w *lineWriter) Close() error { - return w.Flush() -} From 863a590a81b1edcb1487d7766e5c20f0f3ccb370 Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Sat, 17 Oct 2020 20:20:18 +0200 Subject: [PATCH 2/3] Refactor termstatus.Term.{Print,Error} methods --- internal/ui/termstatus/status.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/internal/ui/termstatus/status.go b/internal/ui/termstatus/status.go index c1648206b..38f49b0a0 100644 --- a/internal/ui/termstatus/status.go +++ b/internal/ui/termstatus/status.go @@ -234,19 +234,23 @@ func (t *Terminal) undoStatus(lines int) { } } -// Print writes a line to the terminal. -func (t *Terminal) Print(line string) { +func (t *Terminal) print(line string, isErr bool) { // make sure the line ends with a line break if line[len(line)-1] != '\n' { line += "\n" } select { - case t.msg <- message{line: line}: + case t.msg <- message{line: line, err: isErr}: case <-t.closed: } } +// Print writes a line to the terminal. +func (t *Terminal) Print(line string) { + t.print(line, false) +} + // Printf uses fmt.Sprintf to write a line to the terminal. func (t *Terminal) Printf(msg string, args ...interface{}) { s := fmt.Sprintf(msg, args...) @@ -255,15 +259,7 @@ func (t *Terminal) Printf(msg string, args ...interface{}) { // Error writes an error to the terminal. func (t *Terminal) Error(line string) { - // make sure the line ends with a line break - if line[len(line)-1] != '\n' { - line += "\n" - } - - select { - case t.msg <- message{line: line, err: true}: - case <-t.closed: - } + t.print(line, true) } // Errorf uses fmt.Sprintf to write an error line to the terminal. From 35419de232f89b02645a8844ae0f4853ba46be2e Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Sat, 17 Oct 2020 20:23:36 +0200 Subject: [PATCH 3/3] Simplify ui.StdioWrapper.Write Instead of looping to find line breaks, make it look for the last one. --- internal/ui/stdio_wrapper.go | 18 ++++---------- internal/ui/stdio_wrapper_test.go | 39 +++++++++++-------------------- 2 files changed, 18 insertions(+), 39 deletions(-) diff --git a/internal/ui/stdio_wrapper.go b/internal/ui/stdio_wrapper.go index a3b5c178f..42f4cc545 100644 --- a/internal/ui/stdio_wrapper.go +++ b/internal/ui/stdio_wrapper.go @@ -55,22 +55,12 @@ func (w *lineWriter) Write(data []byte) (n int, err error) { // look for line breaks buf := w.buf.Bytes() - skip := 0 - for i := 0; i < len(buf); { - if buf[i] == '\n' { - // found line - w.print(string(buf[:i+1])) - buf = buf[i+1:] - skip += i + 1 - i = 0 - continue - } - - i++ + i := bytes.LastIndexByte(buf, '\n') + if i != -1 { + w.print(string(buf[:i+1])) + w.buf.Next(i + 1) } - _ = w.buf.Next(skip) - return n, err } diff --git a/internal/ui/stdio_wrapper_test.go b/internal/ui/stdio_wrapper_test.go index fc071f992..b95d9180d 100644 --- a/internal/ui/stdio_wrapper_test.go +++ b/internal/ui/stdio_wrapper_test.go @@ -1,6 +1,7 @@ package ui import ( + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -8,16 +9,14 @@ import ( func TestStdioWrapper(t *testing.T) { var tests = []struct { - inputs [][]byte - outputs []string + inputs [][]byte + output string }{ { inputs: [][]byte{ []byte("foo"), }, - outputs: []string{ - "foo\n", - }, + output: "foo\n", }, { inputs: [][]byte{ @@ -26,10 +25,8 @@ func TestStdioWrapper(t *testing.T) { []byte("\n"), []byte("baz"), }, - outputs: []string{ - "foobar\n", + output: "foobar\n" + "baz\n", - }, }, { inputs: [][]byte{ @@ -37,11 +34,9 @@ func TestStdioWrapper(t *testing.T) { []byte("bar\nbaz\n"), []byte("bump\n"), }, - outputs: []string{ - "foobar\n", - "baz\n", + output: "foobar\n" + + "baz\n" + "bump\n", - }, }, { inputs: [][]byte{ @@ -53,23 +48,17 @@ func TestStdioWrapper(t *testing.T) { []byte("x"), []byte("z"), }, - outputs: []string{ - "foobar\n", - "baz\n", - "bump\n", + output: "foobar\n" + + "baz\n" + + "bump\n" + "xxxz\n", - }, }, } for _, test := range tests { t.Run("", func(t *testing.T) { - var lines []string - print := func(s string) { - lines = append(lines, s) - } - - w := newLineWriter(print) + var output strings.Builder + w := newLineWriter(func(s string) { output.WriteString(s) }) for _, data := range test.inputs { n, err := w.Write(data) @@ -87,8 +76,8 @@ func TestStdioWrapper(t *testing.T) { t.Fatal(err) } - if !cmp.Equal(test.outputs, lines) { - t.Error(cmp.Diff(test.outputs, lines)) + if outstr := output.String(); outstr != test.output { + t.Error(cmp.Diff(test.output, outstr)) } }) }