-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0803d4
commit 4d7e5a0
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//go:build aix | ||
|
||
package termutil | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
var ( | ||
tty *os.File | ||
|
||
unlockSignals = []os.Signal{ | ||
os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL, | ||
} | ||
) | ||
|
||
func init() { | ||
var err error | ||
tty, err = os.Open("/dev/tty") | ||
if err != nil { | ||
tty = os.Stdin | ||
} | ||
} | ||
|
||
// TerminalWidth returns width of the terminal. | ||
func TerminalWidth() (int, error) { | ||
_, width, err := TerminalSize() | ||
return width, err | ||
} | ||
|
||
// TerminalSize returns size of the terminal. | ||
func TerminalSize() (int, int, error) { | ||
w, err := unix.IoctlGetWinsize(int(tty.Fd()), syscall.TIOCGWINSZ) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
return int(w.Row), int(w.Col), nil | ||
} | ||
|
||
var oldState unix.Termios | ||
|
||
func lockEcho() error { | ||
fd := int(tty.Fd()) | ||
currentState, err := unix.IoctlGetTermios(fd, unix.TCGETS) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
oldState = *currentState | ||
newState := oldState | ||
newState.Lflag &^= syscall.ECHO | ||
newState.Lflag |= syscall.ICANON | syscall.ISIG | ||
newState.Iflag |= syscall.ICRNL | ||
if err := unix.IoctlSetTermios(fd, unix.TCSETS, &newState); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func unlockEcho() (err error) { | ||
fd := int(tty.Fd()) | ||
if err := unix.IoctlSetTermios(fd, unix.TCSETS, &oldState); err != nil { | ||
return err | ||
} | ||
return | ||
} |