|
| 1 | +//go:build aix |
| 2 | + |
| 3 | +package termutil |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "syscall" |
| 8 | + |
| 9 | + "golang.org/x/sys/unix" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + tty *os.File |
| 14 | + |
| 15 | + unlockSignals = []os.Signal{ |
| 16 | + os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL, |
| 17 | + } |
| 18 | +) |
| 19 | + |
| 20 | +func init() { |
| 21 | + var err error |
| 22 | + tty, err = os.Open("/dev/tty") |
| 23 | + if err != nil { |
| 24 | + tty = os.Stdin |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// TerminalWidth returns width of the terminal. |
| 29 | +func TerminalWidth() (int, error) { |
| 30 | + _, width, err := TerminalSize() |
| 31 | + return width, err |
| 32 | +} |
| 33 | + |
| 34 | +// TerminalSize returns size of the terminal. |
| 35 | +func TerminalSize() (int, int, error) { |
| 36 | + w, err := unix.IoctlGetWinsize(int(tty.Fd()), syscall.TIOCGWINSZ) |
| 37 | + if err != nil { |
| 38 | + return 0, 0, err |
| 39 | + } |
| 40 | + return int(w.Row), int(w.Col), nil |
| 41 | +} |
| 42 | + |
| 43 | +var oldState unix.Termios |
| 44 | + |
| 45 | +func lockEcho() error { |
| 46 | + fd := int(tty.Fd()) |
| 47 | + currentState, err := unix.IoctlGetTermios(fd, unix.TCGETS) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + oldState = *currentState |
| 53 | + newState := oldState |
| 54 | + newState.Lflag &^= syscall.ECHO |
| 55 | + newState.Lflag |= syscall.ICANON | syscall.ISIG |
| 56 | + newState.Iflag |= syscall.ICRNL |
| 57 | + if err := unix.IoctlSetTermios(fd, unix.TCSETS, &newState); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func unlockEcho() (err error) { |
| 64 | + fd := int(tty.Fd()) |
| 65 | + if err := unix.IoctlSetTermios(fd, unix.TCSETS, &oldState); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + return |
| 69 | +} |
0 commit comments