split src/devices/input into src/devices/input/{raw,wrap}

This commit is contained in:
bel
2023-03-24 19:51:38 -06:00
parent ab673a81f0
commit 38b00e55b0
16 changed files with 161 additions and 159 deletions

View File

@@ -0,0 +1,57 @@
package raw
import (
"io"
"log"
"os"
"os/exec"
"runtime"
)
type Keyboard struct {
}
func NewKeyboard() Keyboard {
switch runtime.GOOS {
case "linux":
if err := exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run(); err != nil {
panic(err)
} else if err := exec.Command("stty", "-F", "/dev/tty", "-echo").Run(); err != nil {
panic(err)
}
case "darwin":
if err := exec.Command("stty", "-f", "/dev/tty", "cbreak", "min", "1").Run(); err != nil {
panic(err)
} else if err := exec.Command("stty", "-f", "/dev/tty", "-echo").Run(); err != nil {
panic(err)
}
default:
panic(runtime.GOOS)
}
return Keyboard{}
}
func (kb Keyboard) Close() {
switch runtime.GOOS {
case "linux":
if err := exec.Command("stty", "-F", "/dev/tty", "echo").Run(); err != nil {
panic(err)
}
case "darwin":
if err := exec.Command("stty", "-f", "/dev/tty", "echo").Run(); err != nil {
panic(err)
}
}
}
func (kb Keyboard) Read() []byte {
b := make([]byte, 5)
n, err := os.Stdin.Read(b)
if err != nil && err != io.EOF {
panic(err)
}
if os.Getenv("DEBUG") == "true" {
log.Printf("raw.Keyboard.Read() %s", b[:n])
}
return b[:n]
}