Codebase list golang-github-vbauerster-mpb / 9c633d5
refactoring: GetTermSize Vladimir Bauer 3 years ago
1 changed file(s) with 25 addition(s) and 16 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 out io.Writer
23 buf bytes.Buffer
24 lines int
25 fd int
26 isTerminal bool
22 out io.Writer
23 buf bytes.Buffer
24 lines int // how much lines to clear before flushing new ones
25 termSize func() (int, int, error)
26 }
27
28 func prepareTermSizeFunc(out io.Writer) func() (int, int, error) {
29 fn := func() (int, int, error) {
30 return -1, -1, ErrNotTTY
31 }
32 if f, ok := out.(*os.File); ok {
33 fd := int(f.Fd())
34 if IsTerminal(fd) {
35 fn = func() (int, int, error) {
36 return GetSize(fd)
37 }
38 }
39 }
40 return fn
2741 }
2842
2943 // New returns a new Writer with defaults.
3044 func New(out io.Writer) *Writer {
31 w := &Writer{out: out}
32 if f, ok := out.(*os.File); ok {
33 w.fd = int(f.Fd())
34 w.isTerminal = IsTerminal(w.fd)
45 w := &Writer{
46 out: out,
47 termSize: prepareTermSizeFunc(out),
3548 }
3649 return w
3750 }
6679 return w.buf.ReadFrom(r)
6780 }
6881
69 // GetWidth returns width of underlying terminal.
70 func (w *Writer) GetWidth() (int, error) {
71 if !w.isTerminal {
72 return -1, ErrNotTTY
73 }
74 tw, _, err := GetSize(w.fd)
75 return tw, err
82 // GetTermSize returns WxH of underlying terminal.
83 func (w *Writer) GetTermSize() (width, height int, err error) {
84 return w.termSize()
7685 }
7786
7887 func (w *Writer) ansiCuuAndEd() error {