...
1 package streams
2
3 import (
4 "io"
5 "os"
6
7 "github.com/moby/term"
8 "github.com/sirupsen/logrus"
9 )
10
11
12
13
14 type Out struct {
15 commonStream
16 out io.Writer
17 }
18
19 func (o *Out) Write(p []byte) (int, error) {
20 return o.out.Write(p)
21 }
22
23
24
25
26
27
28
29 func (o *Out) SetRawTerminal() (err error) {
30 if !o.isTerminal || os.Getenv("NORAW") != "" {
31 return nil
32 }
33 o.state, err = term.SetRawTerminalOutput(o.fd)
34 return err
35 }
36
37
38
39 func (o *Out) GetTtySize() (height uint, width uint) {
40 if !o.isTerminal {
41 return 0, 0
42 }
43 ws, err := term.GetWinsize(o.fd)
44 if err != nil {
45 logrus.WithError(err).Debug("Error getting TTY size")
46 if ws == nil {
47 return 0, 0
48 }
49 }
50 return uint(ws.Height), uint(ws.Width)
51 }
52
53
54 func NewOut(out io.Writer) *Out {
55 o := &Out{out: out}
56 o.fd, o.isTerminal = term.GetFdInfo(out)
57 return o
58 }
59
View as plain text