package terminal import ( "fmt" "os" "golang.org/x/term" ) const ClearScreen = "2J" const ResetCursor = "H" // 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!") } } // 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) } } func ANSICode (code string) { fmt.Printf("\x1b[%s", code) } func Size () (width int, height int) { width, height, err := term.GetSize(int(os.Stdin.Fd())) if err != nil { width = 80 height = 24 } return width, height } // Print a formatted string to stdout, with CRLF line endings for proper terminal formatting func OutLn(format string, a ...interface{}) { formatted := fmt.Sprintf(format, a...) fmt.Printf("%s\r\n", formatted) }