Created
January 30, 2018 15:31
-
-
Save rafaelsq/cb7a5a04b5cdaef22e09b594f0240b49 to your computer and use it in GitHub Desktop.
Hide terminal input
This file contains 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" | |
"os/exec" | |
) | |
func raw(start bool) error { | |
r := "raw" | |
if !start { | |
r = "-raw" | |
} | |
rawMode := exec.Command("stty", r) | |
rawMode.Stdin = os.Stdin | |
err := rawMode.Run() | |
if err != nil { | |
return err | |
} | |
return rawMode.Wait() | |
} | |
// http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html | |
func main() { | |
var rs []rune | |
raw(true) | |
for { | |
inp := bufio.NewReader(os.Stdin) | |
r, _, err := inp.ReadRune() | |
if err != nil { | |
raw(false) | |
panic(err) | |
} | |
if r == '\x03' { // ctrl+c | |
break | |
} else if r == '\r' { // enter | |
fmt.Print(string(rs), "\n\r") | |
rs = []rune{} | |
continue | |
} else if r == '\u007f' { // backspace | |
fmt.Printf("\033[1D\033[K") | |
continue | |
} | |
rs = append(rs, r) | |
} | |
raw(false) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment