-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecklist.go
134 lines (124 loc) · 3.51 KB
/
checklist.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package prompt
import (
"fmt"
"reflect"
)
func getChecked(dst, options reflect.Value) ([]bool, error) {
checked := make([]bool, options.Len())
if dst.Type().Elem() == options.Type().Elem() {
for j := 0; j < dst.Len(); j++ {
for i := 0; i < len(checked); i++ {
if options.Index(i).Equal(dst.Index(j)) {
checked[i] = true
break
}
}
}
} else if k := dst.Elem().Kind(); k == reflect.Bool {
for j := 0; j < dst.Len(); j++ {
checked[j] = dst.Index(j).Bool()
}
} else if k == reflect.Int || k == reflect.Int8 || k == reflect.Int16 || k == reflect.Int32 || k == reflect.Int64 {
for j := 0; j < dst.Len(); j++ {
i := int(dst.Index(j).Int())
checked[i] = true
}
} else if k == reflect.Uint || k == reflect.Uint8 || k == reflect.Uint16 || k == reflect.Uint32 || k == reflect.Uint64 {
for j := 0; j < dst.Len(); j++ {
i := int(dst.Index(j).Uint())
checked[i] = true
}
} else {
return nil, fmt.Errorf("destination must be a boolean or integer type or a slice of %v", options.Type().Elem())
}
return checked, nil
}
func Checklist(idst interface{}, label string, ioptions interface{}) error {
dst := reflect.ValueOf(idst)
options := reflect.ValueOf(ioptions)
if dst.Kind() != reflect.Pointer || dst.Elem().Kind() != reflect.Slice {
return fmt.Errorf("destination must be a pointer to slice")
} else if options.Kind() != reflect.Slice {
return fmt.Errorf("options must be a slice")
} else if options.Len() == 0 {
return fmt.Errorf("no options")
}
dst = dst.Elem()
checked, err := getChecked(dst, options)
if err != nil {
return err
}
optionStrings := make([]string, options.Len())
for i := 0; i < options.Len(); i++ {
optionStrings[i] = fmt.Sprint(options.Index(i).Interface())
}
// set constants
selected := 0
maxLines := selectMaxLines
if _, rows, err := TerminalSize(); err != nil {
return err
} else if rows-1 < maxLines {
maxLines = rows - 1 // keep one for prompt row
}
scrollOffset := selectScrollOffset
withQuery := maxLines < options.Len() || 10 < options.Len()
exitEnter := false
err = terminalList(label, optionStrings, selected, maxLines, scrollOffset, withQuery, exitEnter, func(i, selected int) string {
s := "[ ] %v"
if checked[i] {
s = "[\u00D7] %v"
}
if i == selected {
s = escBold + s + escReset
}
return s
}, func(r rune, i int) {
if r == ' ' || r == '\n' || r == '\r' {
checked[i] = !checked[i]
}
})
fmt.Printf("%v: ", label)
if err != nil {
if err == keyInterrupt {
fmt.Printf("^C")
}
fmt.Printf("\n")
return err
}
first := true
for i := 0; i < len(optionStrings); i++ {
if checked[i] {
if !first {
fmt.Printf(", ")
}
fmt.Printf("%s", optionStrings[i])
first = false
}
}
fmt.Println()
value := reflect.MakeSlice(dst.Type(), 0, options.Len())
if dst.Type().Elem() == options.Type().Elem() {
for i := 0; i < options.Len(); i++ {
if checked[i] {
value = reflect.Append(value, options.Index(i))
}
}
} else {
switch kind := dst.Elem().Kind(); kind {
case reflect.Bool:
for i := 0; i < options.Len(); i++ {
value = reflect.Append(value, reflect.ValueOf(checked[i]))
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
for i := 0; i < options.Len(); i++ {
if checked[i] {
value = reflect.Append(value, reflect.ValueOf(i).Convert(dst.Type().Elem()))
}
}
default:
return fmt.Errorf("unsupported destination type: %v", kind)
}
}
dst.Set(value)
return nil
}