mirror of
https://github.com/octoleo/restic.git
synced 2024-11-30 00:33:57 +00:00
Merge pull request #3026 from greatroar/refactor-ui
internal/ui refactoring
This commit is contained in:
commit
ad3a52e6f0
@ -55,32 +55,18 @@ 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
|
||||
i := bytes.LastIndexByte(buf, '\n')
|
||||
if i != -1 {
|
||||
w.print(string(buf[:i+1]))
|
||||
buf = buf[i+1:]
|
||||
skip += i + 1
|
||||
i = 0
|
||||
continue
|
||||
w.buf.Next(i + 1)
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
_ = w.buf.Next(skip)
|
||||
|
||||
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()
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
@ -9,15 +10,13 @@ import (
|
||||
func TestStdioWrapper(t *testing.T) {
|
||||
var tests = []struct {
|
||||
inputs [][]byte
|
||||
outputs []string
|
||||
output string
|
||||
}{
|
||||
{
|
||||
inputs: [][]byte{
|
||||
[]byte("foo"),
|
||||
},
|
||||
outputs: []string{
|
||||
"foo\n",
|
||||
},
|
||||
output: "foo\n",
|
||||
},
|
||||
{
|
||||
inputs: [][]byte{
|
||||
@ -26,23 +25,19 @@ func TestStdioWrapper(t *testing.T) {
|
||||
[]byte("\n"),
|
||||
[]byte("baz"),
|
||||
},
|
||||
outputs: []string{
|
||||
"foobar\n",
|
||||
output: "foobar\n" +
|
||||
"baz\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
inputs: [][]byte{
|
||||
[]byte("foo"),
|
||||
[]byte("bar\nbaz\n"),
|
||||
[]byte("bump\n"),
|
||||
},
|
||||
outputs: []string{
|
||||
"foobar\n",
|
||||
"baz\n",
|
||||
output: "foobar\n" +
|
||||
"baz\n" +
|
||||
"bump\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
inputs: [][]byte{
|
||||
[]byte("foo"),
|
||||
@ -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))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user