-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
113 lines (105 loc) · 1.85 KB
/
cli.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"io"
"os"
"os/exec"
"strings"
"unicode"
)
const (
OutputBold = "\033[1m"
OutputNormal = "\033[0m"
OutputBell = "\a"
OutputReset = "\x1b[2K\r"
InputBackspace = "\x7f"
InputEnter = "\n"
InputUp = "\027[A"
InputDown = "\027[B"
)
func readStdin(out chan string, kill chan bool) {
//no buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
//no visible output
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
var b []byte = make([]byte, 2)
for {
select {
case <-kill:
return
default:
c, err := os.Stdin.Read(b)
if err != io.EOF && err != nil {
panic(err)
}
if c > 0 {
out <- string(b[:c])
}
}
}
}
func print(s string) {
os.Stdout.Write([]byte(s))
}
func cliFindAsYouType(db database) error {
var q, r string
offset := 0
defer func() {
exec.Command("stty", "-f", "/dev/tty", "echo").Run()
os.Stderr.WriteString(r)
}()
stdin := make(chan string, 1)
kill := make(chan bool, 1)
print("> ")
go readStdin(stdin, kill)
for {
c := <-stdin
if c == InputEnter {
return nil
} else if c == InputBackspace {
if len(q) > 0 {
q = q[:len(q)-1]
} else {
print(OutputBell)
}
} else if c == InputUp {
offset += 1
} else if c == InputDown {
if offset > 0 {
offset -= 1
}
} else {
printable := true
for _, char := range c {
if !unicode.IsPrint(char) {
printable = false
break
}
}
if printable {
q += string(c)
}
}
rs, e := db.Query(query{
Cmd: &q,
Limit: 1,
Offset: offset,
})
if e != nil {
return e
}
if len(rs) == 0 {
r = ""
print(OutputReset + "> " + q + OutputBell)
} else {
r = rs[0].Cmd
i := strings.Index(r, q)
print(OutputReset +
"> " +
r[:i] +
OutputBold +
r[i:i+len(q)] +
OutputNormal +
r[i+len(q):])
}
}
}