package term
import (
"golang.org/x/sys/unix"
)
type State struct {
termios unix .Termios
}
func IsTerminal (fd int ) bool {
_ , err := unix .IoctlGetTermios (fd , ioctlReadTermios )
return err == nil
}
func MakeRaw (fd int ) (*State , error ) {
termios , err := unix .IoctlGetTermios (fd , ioctlReadTermios )
if err != nil {
return nil , err
}
oldState := State {termios : *termios }
termios .Iflag &^= unix .IGNBRK | unix .BRKINT | unix .PARMRK | unix .ISTRIP | unix .INLCR | unix .IGNCR | unix .ICRNL | unix .IXON
termios .Lflag &^= unix .ECHO | unix .ECHONL | unix .ICANON | unix .ISIG | unix .IEXTEN
termios .Cflag &^= unix .CSIZE | unix .PARENB
termios .Cflag |= unix .CS8
termios .Cc [unix .VMIN ] = 1
termios .Cc [unix .VTIME ] = 0
if err := unix .IoctlSetTermios (fd , ioctlWriteTermios , termios ); err != nil {
return nil , err
}
return &oldState , nil
}
func GetState (fd int ) (*State , error ) {
termios , err := unix .IoctlGetTermios (fd , ioctlReadTermios )
if err != nil {
return nil , err
}
return &State {termios : *termios }, nil
}
func Restore (fd int , state *State ) error {
return unix .IoctlSetTermios (fd , ioctlWriteTermios , &state .termios )
}
func GetSize (fd int ) (width , height int , err error ) {
ws , err := unix .IoctlGetWinsize (fd , unix .TIOCGWINSZ )
if err != nil {
return -1 , -1 , err
}
return int (ws .Col ), int (ws .Row ), nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .