Last active
August 29, 2015 14:00
-
-
Save mrnugget/11040208 to your computer and use it in GitHub Desktop.
Changing and resetting stty settings
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 ( | |
"bytes" | |
"fmt" | |
"log" | |
"os/exec" | |
"os" | |
"strings" | |
) | |
func setSttyState(f *os.File, state string) { | |
outputbuf := new(bytes.Buffer) | |
cmd := exec.Command("stty", state) | |
cmd.Stdin = f | |
cmd.Stdout = outputbuf | |
cmd.Stderr = os.Stderr | |
cmd.Run() | |
} | |
func getSttyState(f *os.File) (state string) { | |
outputbuf := new(bytes.Buffer) | |
cmd := exec.Command("stty", "-g") | |
cmd.Stdin = f | |
cmd.Stdout = outputbuf | |
cmd.Run() | |
return strings.TrimSuffix(outputbuf.String(), "\n") | |
} | |
func main() { | |
fh, err := os.OpenFile("/dev/tty", os.O_RDWR, 0666) | |
if err != nil { | |
log.Fatal(err) | |
} | |
before := getSttyState(fh) | |
fmt.Printf("before:\t\t%s\n", before) | |
// set new state: -echo (do not echo back typed in characters) | |
setSttyState(fh, "-echo") | |
fmt.Printf("new:\t\t%s\n", getSttyState(fh)) | |
setSttyState(fh, before) | |
fmt.Printf("reset:\t\t%s\n", getSttyState(fh)) | |
} |
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
#!/usr/bin/env bash | |
old_state=$(stty -g < /dev/tty) | |
printf "before:\t %s\n" $old_state | |
stty -echo < /dev/tty | |
printf "new:\t %s\n" $(stty -g < /dev/tty) | |
stty $old_state < /dev/tty | |
printf "reset:\t %s\n" $(stty -g </dev/tty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment