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
1 changed files with 4 additions and 24 deletions

View File

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