-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
58 lines (48 loc) · 1.13 KB
/
list.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
package selector
import (
"fmt"
tty "github.com/mattn/go-tty"
)
func List(items []string, caption string) (int, error) {
tty, err := tty.Open()
if err != nil {
return -1, err
}
defer tty.Close()
out := tty.Output()
out.Write([]byte(hideCursor))
out.Write([]byte(colorize(Green, "? ") + caption + "\n"))
cursor := 0
for {
for i, item := range items {
str := colorize(Dim, " "+item)
if cursor == i {
str = colorize(Blue, "❯ "+item)
}
out.Write([]byte(str + "\n"))
}
out.Write([]byte(fmt.Sprintf(upCursor, len(items))))
r, err := tty.ReadRune()
if err != nil {
return -1, err
}
switch r {
case 'j', 0x0E:
if cursor < len(items)-1 {
cursor++
}
case 'k', 0x10:
if cursor > 0 {
cursor--
}
case 13:
out.Write([]byte(fmt.Sprintf(upCursor, 1) + colorize(Green, "? ") + caption + " " + colorize(Blue, items[cursor]) + "\n"))
out.Write([]byte(eraseDisplay + showCursor))
return cursor, nil
case 27:
out.Write([]byte(fmt.Sprintf(upCursor, 1) + colorize(Green, "? ") + caption + " " + "\n"))
out.Write([]byte(eraseDisplay + showCursor))
return -1, nil
}
}
}