35 lines
570 B
Go
35 lines
570 B
Go
package terminal
|
|
|
|
import (
|
|
"os"
|
|
|
|
"golang.org/x/term"
|
|
)
|
|
|
|
// Put the terminal in raw mode
|
|
func RawOn() *term.State {
|
|
check()
|
|
|
|
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return oldState
|
|
}
|
|
|
|
// Restore the terminal to canonical mode
|
|
func RawOff(oldState *term.State) {
|
|
err := term.Restore(int(os.Stdin.Fd()), oldState)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Is this a valid interactive terminal?
|
|
func check() {
|
|
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
|
panic("An interactive terminal is required to use a text editor!")
|
|
}
|
|
}
|