// TODO(bep): check different OSs func readTerm(done, read chan bool, wg *sync.WaitGroup) { defer wg.Done() t, _ := term.Open("/dev/tty") defer t.Close() defer t.Restore() term.RawMode(t) for { select { case <-done: return default: b := make([]byte, 3) i, err := t.Read(b) if err != nil { return } if i == 1 && b[0] == 3 { // CTRL-C done <- true return } read <- true } } }
// Returns either an ascii code, or (if input is an arrow) a Javascript key code. func getChar() (ascii int, keyCode int, err error) { t, _ := term.Open("/dev/tty") term.RawMode(t) bytes := make([]byte, 3) var numRead int numRead, err = t.Read(bytes) if err != nil { return } if numRead == 3 && bytes[0] == 27 && bytes[1] == 91 { // Three-character control sequence, beginning with "ESC-[". // Since there are no ASCII codes for arrow keys, we use // Javascript key codes. if bytes[2] == 65 { // Up keyCode = 38 } else if bytes[2] == 66 { // Down keyCode = 40 } else if bytes[2] == 67 { // Right keyCode = 39 } else if bytes[2] == 68 { // Left keyCode = 37 } } else if numRead == 1 { ascii = int(bytes[0]) } else { // Two characters read?? } if ascii == 3 { fmt.Printf("BREAK") t.Write([]byte("\r\n")) } term.CBreakMode(t) t.Restore() t.Close() return }
func RawMode(t *Term) error { return term.RawMode(t.Term) }