Created
December 18, 2018 22:25
-
-
Save miguelmota/2af6302e7d517d6eaf21c87bf78a13b0 to your computer and use it in GitHub Desktop.
Golang terminal package example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "unicode" | |
| "golang.org/x/crypto/ssh/terminal" | |
| ) | |
| func main() { | |
| oldState, err := terminal.MakeRaw(0) | |
| if err != nil { | |
| panic(err) | |
| } | |
| defer terminal.Restore(0, oldState) | |
| reader := bufio.NewReader(os.Stdin) | |
| var c rune | |
| for err == nil { | |
| if c == 'q' { | |
| break | |
| } | |
| c, _, err = reader.ReadRune() | |
| if unicode.IsControl(c) { | |
| fmt.Printf("%d\r\n", c) | |
| } else { | |
| fmt.Printf("%d ('%c')\r\n", c, c) | |
| } | |
| } | |
| if err != nil { | |
| panic(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment