Codebase list golang-github-vbauerster-mpb / a672843
add func IsTerminal Vladimir Bauer 5 years ago
6 changed file(s) with 44 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
0 // +build darwin dragonfly freebsd netbsd openbsd
1
2 package cwriter
3
4 import "golang.org/x/sys/unix"
5
6 const ioctlReadTermios = unix.TIOCGETA
0 // +build aix linux
1
2 package cwriter
3
4 import "golang.org/x/sys/unix"
5
6 const ioctlReadTermios = unix.TCGETS
0 // +build solaris
1
2 package cwriter
3
4 import "golang.org/x/sys/unix"
5
6 const ioctlReadTermios = unix.TCGETA
55 "io"
66 "os"
77 "strconv"
8
9 "github.com/mattn/go-isatty"
108 )
119
1210 // NotATTY not a TeleTYpewriter error.
2422 out io.Writer
2523 buf bytes.Buffer
2624 lineCount int
27 fd uintptr
25 fd int
2826 isTerminal bool
2927 }
3028
3230 func New(out io.Writer) *Writer {
3331 w := &Writer{out: out}
3432 if f, ok := out.(*os.File); ok {
35 w.fd = f.Fd()
36 w.isTerminal = isatty.IsTerminal(w.fd)
33 w.fd = int(f.Fd())
34 w.isTerminal = IsTerminal(w.fd)
3735 }
3836 return w
3937 }
1010 }
1111
1212 // GetSize returns the dimensions of the given terminal.
13 func GetSize(fd uintptr) (width, height int, err error) {
14 ws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ)
13 func GetSize(fd int) (width, height int, err error) {
14 ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
1515 if err != nil {
1616 return -1, -1, err
1717 }
1818 return int(ws.Col), int(ws.Row), nil
1919 }
20
21 // IsTerminal returns whether the given file descriptor is a terminal.
22 func IsTerminal(fd int) bool {
23 _, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
24 return err == nil
25 }
44 import (
55 "unsafe"
66
7 "github.com/mattn/go-isatty"
87 "golang.org/x/sys/windows"
98 )
109
1615 )
1716
1817 func (w *Writer) clearLines() error {
19 if !w.isTerminal && isatty.IsCygwinTerminal(w.fd) {
18 if !w.isTerminal {
19 // hope it's cygwin or similar
2020 return w.ansiCuuAndEd()
2121 }
2222
3030 info.CursorPosition.Y = 0
3131 }
3232 _, _, _ = procSetConsoleCursorPosition.Call(
33 w.fd,
33 uintptr(w.fd),
3434 uintptr(uint32(uint16(info.CursorPosition.Y))<<16|uint32(uint16(info.CursorPosition.X))),
3535 )
3636
4141 }
4242 count := uint32(info.Size.X) * uint32(w.lineCount)
4343 _, _, _ = procFillConsoleOutputCharacter.Call(
44 w.fd,
44 uintptr(w.fd),
4545 uintptr(' '),
4646 uintptr(count),
4747 *(*uintptr)(unsafe.Pointer(cursor)),
5353 // GetSize returns the visible dimensions of the given terminal.
5454 //
5555 // These dimensions don't include any scrollback buffer height.
56 func GetSize(fd uintptr) (width, height int, err error) {
56 func GetSize(fd int) (width, height int, err error) {
5757 var info windows.ConsoleScreenBufferInfo
5858 if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil {
5959 return 0, 0, err
6363 // but looks like this is a root cause of issue #66, so removing both "+ 1" have fixed it.
6464 return int(info.Window.Right - info.Window.Left), int(info.Window.Bottom - info.Window.Top), nil
6565 }
66
67 // IsTerminal returns whether the given file descriptor is a terminal.
68 func IsTerminal(fd int) bool {
69 var st uint32
70 err := windows.GetConsoleMode(windows.Handle(fd), &st)
71 return err == nil
72 }