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