Merge pull request #1641 from WeiZhang555/fix-progress-bar

[progress] Fix wrong display of progress bar
This commit is contained in:
Phil Estes 2017-10-16 16:32:10 -04:00 committed by GitHub
commit ef5fe56c24

View File

@ -4,6 +4,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"strings"
"github.com/containerd/console"
) )
// Writer buffers writes until flush, at which time the last screen is cleared // Writer buffers writes until flush, at which time the last screen is cleared
@ -38,7 +41,15 @@ func (w *Writer) Flush() error {
return err return err
} }
w.lines = bytes.Count(w.buf.Bytes(), []byte("\n")) ws, err := console.Current().Size()
if err != nil {
return fmt.Errorf("failed to get terminal width: %v", err)
}
strlines := strings.Split(w.buf.String(), "\n")
w.lines = -1
for _, line := range strlines {
w.lines += len(line)/int(ws.Width) + 1
}
if _, err := w.w.Write(w.buf.Bytes()); err != nil { if _, err := w.w.Write(w.buf.Bytes()); err != nil {
return err return err
@ -53,7 +64,7 @@ func (w *Writer) Flush() error {
func (w *Writer) clear() error { func (w *Writer) clear() error {
for i := 0; i < w.lines; i++ { for i := 0; i < w.lines; i++ {
if _, err := fmt.Fprintf(w.w, "\x1b[0A\x1b[2K\r"); err != nil { if _, err := fmt.Fprintf(w.w, "\x1b[1A\x1b[2K\r"); err != nil {
return err return err
} }
} }