Manually echo output

This commit is contained in:
Timothy Warren 2021-03-19 12:03:55 -04:00
parent b3344dbe89
commit 1161119269

26
gilo.go
View File

@ -7,6 +7,22 @@ import (
"os" "os"
) )
func isAscii(char rune) bool {
ord := int(char)
return ord < 0x80
}
func isCtrl(char rune) bool {
if ! isAscii(char) {
return false
}
ord:= int(char)
return ord == 0x7f || ord < 0x20
}
func goRaw() (*term.State, error) { func goRaw() (*term.State, error) {
state, err := term.MakeRaw(int(os.Stdin.Fd())) state, err := term.MakeRaw(int(os.Stdin.Fd()))
@ -14,11 +30,13 @@ func goRaw() (*term.State, error) {
} }
func main() { func main() {
// Go to proper raw mode
oldState, err := goRaw() oldState, err := goRaw()
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Restore canonical mode at exit
defer term.Restore(int(os.Stdin.Fd()), oldState) defer term.Restore(int(os.Stdin.Fd()), oldState)
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
@ -33,6 +51,12 @@ func main() {
case char == 'q': case char == 'q':
fmt.Println("bye!\r") fmt.Println("bye!\r")
return return
} case isCtrl(char):
fmt.Printf("%d\n", char)
default:
fmt.Printf("%d ('%c')\n", char, char)
}
_ = os.Stdout.Sync()
} }
} }