Don't use golang.org/x/term

This commit is contained in:
Anthony Wang 2022-03-27 18:09:22 -05:00
parent 2e0dac3994
commit 81fe341a26
Signed by: a
GPG key ID: BC96B00AEC5F2D76
3 changed files with 22 additions and 17 deletions

4
go.mod
View file

@ -1,7 +1,3 @@
module sd
go 1.18
require golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect

4
go.sum
View file

@ -1,4 +0,0 @@
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

31
sd.go
View file

@ -2,9 +2,9 @@ package main
import (
"flag"
"fmt"
"os"
"golang.org/x/term"
"os/exec"
)
var file = flag.String("f", "cards", "cards file")
@ -12,12 +12,25 @@ var file = flag.String("f", "cards", "cards file")
func main() {
flag.Parse()
// Put terminal into raw mode
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
// https://stackoverflow.com/questions/14094190/function-similar-to-getchar
// disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
// do not display entered characters on the screen
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
// restore the echoing state when exiting
defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()
for {
fmt.Println("hello world")
var b []byte = make([]byte, 1)
os.Stdin.Read(b)
if string(b) == 'y' {
} else if string(b) == 'n' {
} else {
}
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
}