package main import ( "fmt" "git.kiefte.eu/lapingvino/clitris/tris" "github.com/pkg/term" "time" ) // GetKey() returns the key currently pressed. It always returns a 3 byte slice. Check first element for Escape for handling arrow keys // Because a defer would trigger too late and the Restore and Close are essential, separated in a function. func GetKey() []byte { t, _ := term.Open("/dev/tty") term.RawMode(t) key := make([]byte, 3) t.SetReadTimeout(time.Second / 60) t.Read(key) t.Restore() t.Close() return key } func main() { var f tris.Field fmt.Print("\033[2J") // Clear screen b := tris.NewBag() p := b.Pick() for { // fmt.Print("\033[2J") // Clear screen if p.Lock <= 0 { f = f.Add(p) p = b.Pick() } fmt.Print("\033[0;0H") // Position to 0,0 fmt.Println(f.Add(p).String()) if !p.Drop(f) { fmt.Println("Should drop one") p.Lock-- } key := GetKey() switch key[0] { case 27: // Escape, read the arrow key pressed switch key[2] { case 65: // Up case 66: // Down case 67: // Right case 68: // Left default: fmt.Println("...escape, escape!") return } case 'q': fmt.Println("...that was exciting!") return case 'Q': fmt.Println("...never let an engineer pick the name of your software?") return default: if key[0] != 0 { fmt.Print(string(key[0])) } } } }