Add terminal output helper
This commit is contained in:
parent
1161119269
commit
27271a61b3
35
gilo.go
35
gilo.go
@ -23,20 +23,26 @@ func isCtrl(char rune) bool {
|
||||
return ord == 0x7f || ord < 0x20
|
||||
}
|
||||
|
||||
func goRaw() (*term.State, error) {
|
||||
state, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||
|
||||
return state, err
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Go to proper raw mode
|
||||
oldState, err := goRaw()
|
||||
// Put the terminal in
|
||||
func goRaw() *term.State {
|
||||
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Restore canonical mode at exit
|
||||
return oldState
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Go to proper raw mode, but restore canonical mode at extit
|
||||
oldState := goRaw()
|
||||
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
@ -47,16 +53,15 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Ugliest syntax structure ever?
|
||||
switch {
|
||||
case char == 'q':
|
||||
fmt.Println("bye!\r")
|
||||
outLn("bye!")
|
||||
return
|
||||
case isCtrl(char):
|
||||
fmt.Printf("%d\n", char)
|
||||
outLn("%d", char)
|
||||
default:
|
||||
fmt.Printf("%d ('%c')\n", char, char)
|
||||
outLn("%d ('%c')", char, char)
|
||||
}
|
||||
|
||||
_ = os.Stdout.Sync()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user