forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (39 loc) · 794 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// +build !windows
package main
import (
"fmt"
"syscall"
prompt "github.com/c-bata/go-prompt"
"github.com/c-bata/go-prompt/internal/term"
)
func main() {
if err := term.SetRaw(syscall.Stdin); err != nil {
fmt.Println(err)
return
}
defer term.Restore()
bufCh := make(chan []byte, 128)
go readBuffer(bufCh)
fmt.Print("> ")
for {
b := <-bufCh
if key := prompt.GetKey(b); key == prompt.NotDefined {
fmt.Printf("Key '%s' data:'%#v'\n", string(b), b)
} else {
if key == prompt.ControlC {
fmt.Println("exit.")
return
}
fmt.Printf("Key '%s' data:'%#v'\n", key, b)
}
fmt.Print("> ")
}
}
func readBuffer(bufCh chan []byte) {
buf := make([]byte, 1024)
for {
if n, err := syscall.Read(syscall.Stdin, buf); err == nil {
bufCh <- buf[:n]
}
}
}