Path: blob/main/vendor/github.com/chzyer/readline/utils_unix.go
2875 views
// +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd os400 solaris12package readline34import (5"io"6"os"7"os/signal"8"sync"9"syscall"10)1112type winsize struct {13Row uint1614Col uint1615Xpixel uint1616Ypixel uint1617}1819// SuspendMe use to send suspend signal to myself, when we in the raw mode.20// For OSX it need to send to parent's pid21// For Linux it need to send to myself22func SuspendMe() {23p, _ := os.FindProcess(os.Getppid())24p.Signal(syscall.SIGTSTP)25p, _ = os.FindProcess(os.Getpid())26p.Signal(syscall.SIGTSTP)27}2829// get width of the terminal30func getWidth(stdoutFd int) int {31cols, _, err := GetSize(stdoutFd)32if err != nil {33return -134}35return cols36}3738func GetScreenWidth() int {39w := getWidth(syscall.Stdout)40if w < 0 {41w = getWidth(syscall.Stderr)42}43return w44}4546// ClearScreen clears the console screen47func ClearScreen(w io.Writer) (int, error) {48return w.Write([]byte("\033[H"))49}5051func DefaultIsTerminal() bool {52return IsTerminal(syscall.Stdin) && (IsTerminal(syscall.Stdout) || IsTerminal(syscall.Stderr))53}5455func GetStdin() int {56return syscall.Stdin57}5859// -----------------------------------------------------------------------------6061var (62widthChange sync.Once63widthChangeCallback func()64)6566func DefaultOnWidthChanged(f func()) {67widthChangeCallback = f68widthChange.Do(func() {69ch := make(chan os.Signal, 1)70signal.Notify(ch, syscall.SIGWINCH)7172go func() {73for {74_, ok := <-ch75if !ok {76break77}78widthChangeCallback()79}80}()81})82}838485