Merge pull request #2648 from nairb774/iowritestring

termstatus: Use io.WriteString to output messages.
This commit is contained in:
MichaelEischer 2020-04-18 13:48:42 +02:00 committed by GitHub
commit 7910ff4c0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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)
} }