2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-10 04:42:21 +00:00

termstatus: Use io.WriteString to output messages.

The previous implementation was repeating the implementation that is
found inside of io.WriteString. Simplify by making use of the stdlib's
implementation.
This commit is contained in:
Brian Atkinson 2020-03-15 14:54:16 -07:00
parent 2b5a6d255a
commit b8da7b1f4d

View File

@ -89,10 +89,6 @@ func (t *Terminal) Run(ctx context.Context) {
t.runWithoutStatus(ctx) t.runWithoutStatus(ctx)
} }
type stringWriter interface {
WriteString(string) (int, error)
}
// run listens on the channels and updates the terminal screen. // run listens on the channels and updates the terminal screen.
func (t *Terminal) run(ctx context.Context) { func (t *Terminal) run(ctx context.Context) {
var status []string var status []string
@ -128,22 +124,14 @@ func (t *Terminal) run(ctx context.Context) {
dst = t.wr dst = t.wr
} }
var err error if _, err := io.WriteString(dst, msg.line); err != nil {
if w, ok := dst.(stringWriter); ok {
_, err = w.WriteString(msg.line)
} else {
_, err = dst.Write([]byte(msg.line))
}
if err != nil {
fmt.Fprintf(os.Stderr, "write failed: %v\n", err) fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
continue continue
} }
t.writeStatus(status) t.writeStatus(status)
err = t.wr.Flush() if err := t.wr.Flush(); err != nil {
if err != nil {
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err) fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
} }
@ -194,7 +182,6 @@ func (t *Terminal) runWithoutStatus(ctx context.Context) {
case <-ctx.Done(): case <-ctx.Done():
return return
case msg := <-t.msg: case msg := <-t.msg:
var err error
var flush func() error var flush func() error
var dst io.Writer var dst io.Writer
@ -205,13 +192,7 @@ func (t *Terminal) runWithoutStatus(ctx context.Context) {
flush = t.wr.Flush flush = t.wr.Flush
} }
if w, ok := dst.(stringWriter); ok { if _, err := io.WriteString(dst, msg.line); err != nil {
_, err = w.WriteString(msg.line)
} else {
_, err = dst.Write([]byte(msg.line))
}
if err != nil {
fmt.Fprintf(os.Stderr, "write failed: %v\n", err) fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
} }
@ -219,8 +200,7 @@ func (t *Terminal) runWithoutStatus(ctx context.Context) {
continue continue
} }
err = flush() if err := flush(); err != nil {
if err != nil {
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err) fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
} }