bombadillo/termios/termios.go

43 lines
822 B
Go

// +build cgo
package termios
/*
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
// NOTE: ioctl() is variadic so cgo requires a wrapper
int getWinsize(struct winsize* ws) {
return ioctl(STDOUT_FILENO, TIOCGWINSZ, ws);
}
*/
import "C"
func GetWindowSize() (int, int) {
var value C.struct_winsize
C.getWinsize(&value)
return int(value.ws_col), int(value.ws_row)
}
func getTermios() C.struct_termios {
var termios C.struct_termios
C.tcgetattr(C.STDIN_FILENO, &termios)
return termios
}
func setTermios(termios C.struct_termios) {
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &termios)
}
func SetCharMode() {
var t = getTermios()
t.c_lflag = t.c_lflag &^ (C.ICANON | C.ECHO)
setTermios(t)
}
func SetLineMode() {
var t = getTermios()
t.c_lflag = t.c_lflag | (C.ICANON | C.ECHO)
setTermios(t)
}