Codebase list golang-github-vbauerster-mpb / be3aa76
cwriter fix for windows Vladimir Bauer 3 years ago
2 changed file(s) with 18 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
2222 out io.Writer
2323 buf bytes.Buffer
2424 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
25 fd int
26 terminal bool
27 termSize func(int) (int, int, error)
4128 }
4229
4330 // New returns a new Writer with defaults.
4431 func New(out io.Writer) *Writer {
4532 w := &Writer{
46 out: out,
47 termSize: prepareTermSizeFunc(out),
33 out: out,
34 termSize: func(_ int) (int, int, error) {
35 return -1, -1, ErrNotTTY
36 },
37 }
38 if f, ok := out.(*os.File); ok {
39 w.fd = int(f.Fd())
40 if IsTerminal(w.fd) {
41 w.terminal = true
42 w.termSize = func(fd int) (int, int, error) {
43 return GetSize(fd)
44 }
45 }
4846 }
4947 return w
5048 }
8179
8280 // GetTermSize returns WxH of underlying terminal.
8381 func (w *Writer) GetTermSize() (width, height int, err error) {
84 return w.termSize()
82 return w.termSize(w.fd)
8583 }
8684
8785 func (w *Writer) ansiCuuAndEd() error {
1515 )
1616
1717 func (w *Writer) clearLines() error {
18 if !w.isTerminal {
18 if !w.terminal {
1919 // hope it's cygwin or similar
2020 return w.ansiCuuAndEd()
2121 }