Codebase list golang-github-vbauerster-mpb / 1cff8e0
separate Flush method Vladimir Bauer 3 years ago
3 changed file(s) with 36 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
1919 // Writer is a buffered the writer that updates the terminal. The
2020 // contents of writer will be flushed when Flush is called.
2121 type Writer struct {
22 *bytes.Buffer
2223 out io.Writer
23 buf bytes.Buffer
2424 fd int
2525 terminal bool
2626 termSize func(int) (int, int, error)
2929 // New returns a new Writer with defaults.
3030 func New(out io.Writer) *Writer {
3131 w := &Writer{
32 out: out,
32 Buffer: new(bytes.Buffer),
33 out: out,
3334 termSize: func(_ int) (int, int, error) {
3435 return -1, -1, ErrNotTTY
3536 },
4647 return w
4748 }
4849
49 // Flush flushes the underlying buffer.
50 func (w *Writer) Flush(lines int) error {
51 _, err := w.buf.WriteTo(w.out)
52 // some terminals interpret 'cursor up 0' as 'cursor up 1'
53 if err == nil && lines > 0 {
54 err = w.clearLines(lines)
55 }
56 return err
57 }
58
59 // Write appends the contents of p to the underlying buffer.
60 func (w *Writer) Write(p []byte) (n int, err error) {
61 return w.buf.Write(p)
62 }
63
64 // WriteString writes string to the underlying buffer.
65 func (w *Writer) WriteString(s string) (n int, err error) {
66 return w.buf.WriteString(s)
67 }
68
69 // ReadFrom reads from the provided io.Reader and writes to the
70 // underlying buffer.
71 func (w *Writer) ReadFrom(r io.Reader) (n int64, err error) {
72 return w.buf.ReadFrom(r)
73 }
74
7550 // GetTermSize returns WxH of underlying terminal.
7651 func (w *Writer) GetTermSize() (width, height int, err error) {
7752 return w.termSize(w.fd)
7853 }
7954
80 func (w *Writer) ansiCuuAndEd(n int) error {
55 func ansiCuuAndEd(out io.Writer, n int) error {
8156 buf := make([]byte, 8)
8257 buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(n), 10)
83 _, err := w.buf.Write(append(buf, cuuAndEd...))
58 _, err := out.Write(append(buf, cuuAndEd...))
8459 return err
8560 }
55 "golang.org/x/sys/unix"
66 )
77
8 func (w *Writer) clearLines(n int) error {
9 return w.ansiCuuAndEd(n)
8 // Flush flushes the underlying buffer.
9 func (w *Writer) Flush(lines int) error {
10 _, err := w.WriteTo(w.out)
11 // some terminals interpret 'cursor up 0' as 'cursor up 1'
12 if err == nil && lines > 0 {
13 err = ansiCuuAndEd(w, lines)
14 }
15 return err
1016 }
1117
1218 // GetSize returns the dimensions of the given terminal.
1414 procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
1515 )
1616
17 var linesCleaner = makeLinesCleaner()
18
19 func makeLinesCleaner() func(*Writer, int) error {
20 var lastLines int
21 return func(w *Writer, n int) (err error) {
22 if lastLines > 0 {
23 err = w.clearLines(lastLines)
24 }
25 lastLines = n
26 return err
27 }
28 }
29
30 // Flush flushes the underlying buffer.
31 func (w *Writer) Flush(lines int) error {
32 err := linesCleaner(w, lines)
33 if err == nil {
34 _, err = w.WriteTo(w.out)
35 }
36 return err
37 }
38
1739 func (w *Writer) clearLines(n int) error {
1840 if !w.terminal {
1941 // hope it's cygwin or similar
20 return w.ansiCuuAndEd()
42 return ansiCuuAndEd(w.out, n)
2143 }
2244
2345 var info windows.ConsoleScreenBufferInfo
5173 }
5274
5375 // GetSize returns the visible dimensions of the given terminal.
54 //
5576 // These dimensions don't include any scrollback buffer height.
5677 func GetSize(fd int) (width, height int, err error) {
5778 var info windows.ConsoleScreenBufferInfo