-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbox.go
More file actions
164 lines (142 loc) · 3.69 KB
/
checkbox.go
File metadata and controls
164 lines (142 loc) · 3.69 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package input
import (
"atomicgo.dev/keyboard/keys"
"fmt"
"github.com/liuuner/go-cli-input/colors"
"github.com/liuuner/go-cli-input/cursor"
"strings"
)
type CheckboxItem[T any] struct {
value T
checked bool
}
type CheckboxState[T any] struct {
items []CheckboxItem[T]
GetName func(T) string
GetColor func(T) colors.Formatter
cursorPos int
}
func NewCheckbox[T any](prompt string, items []T, getName func(T) string) Input[CheckboxState[T]] {
i := newInput[CheckboxState[T]]()
checkboxItems := make([]CheckboxItem[T], len(items))
for i, item := range items {
checkboxItems[i] = CheckboxItem[T]{value: item, checked: false}
}
state := CheckboxState[T]{
items: checkboxItems,
cursorPos: 0,
GetName: getName,
}
return Input[CheckboxState[T]]{
render: renderCheckbox[T],
handleInput: handleCheckbox[T],
close: closeCheckbox[T],
userPrompt: prompt,
inputPrompt: "› - Use arrow-keys. Return to submit.",
hasPrompt: i.hasPrompt,
hasSummary: i.hasSummary,
failedString: i.failedString,
completedString: i.completedString,
promptString: i.promptString,
state: state,
}
}
func renderCheckbox[T any](s *CheckboxState[T], rerender bool) {
if rerender {
// Move cursor to top
cursor.UpN(len(s.items) - 1)
} else {
cursor.Hide()
}
for index, item := range s.items {
var newline = "\n"
if index == len(s.items)-1 {
// Adding a new line on the last option will move the cursor position out of range
// For out redrawing
newline = ""
}
menuItemText := s.GetName(item.value)
if s.GetColor != nil {
menuItemText = s.GetColor(item.value)(menuItemText)
}
checkboxString := "[ ]"
if index == s.cursorPos { // for color or other effects
checkboxString = fmt.Sprintf("[%s]", col.Gray("X"))
menuItemText = col.Underline(menuItemText)
}
if item.checked {
checkboxString = "[X]"
}
fmt.Printf("\r %s %s%s", checkboxString, menuItemText, newline)
}
}
func handleCheckbox[T any](s *CheckboxState[T], key keys.Key) (stop bool, err error) {
switch key.Code {
case keys.Up:
s.cursorPos--
s.keepPosInBoundaries()
case keys.Down:
s.cursorPos++
s.keepPosInBoundaries()
case keys.Left:
//select none
s.setAllCheckedState(false)
case keys.Right:
//select all
s.setAllCheckedState(true)
case keys.Space:
//check/uncheck current
s.items[s.cursorPos].checked = !s.items[s.cursorPos].checked
case keys.Enter:
stop, err = true, nil
}
return
}
func closeCheckbox[T any](s *CheckboxState[T], err error) (summary string) {
cursor.ClearLine()
for i := 0; i < len(s.items)-1; i++ {
cursor.Up()
cursor.ClearLine()
}
checkedItems := s.getCheckedItems()
if len(checkedItems) == 0 {
summary = "none"
} else {
names := make([]string, len(checkedItems))
for i, item := range checkedItems {
names[i] = s.GetName(item.value)
}
summary = strings.Join(names, ", ")
}
if err != nil {
summary = err.Error()
}
cursor.Show()
return
}
func (s *CheckboxState[T]) Resolve() []T {
return s.toItems(s.getCheckedItems())
}
func (s *CheckboxState[T]) setAllCheckedState(checked bool) {
for i := range s.items {
s.items[i].checked = checked
}
}
func (s *CheckboxState[T]) getCheckedItems() (checkedItems []CheckboxItem[T]) {
for _, item := range s.items {
if item.checked {
checkedItems = append(checkedItems, item)
}
}
return checkedItems
}
func (s *CheckboxState[T]) toItems(items []CheckboxItem[T]) []T {
nonCheckboxItems := make([]T, len(items))
for i, checkboxItem := range items {
nonCheckboxItems[i] = checkboxItem.value
}
return nonCheckboxItems
}
func (s *CheckboxState[T]) keepPosInBoundaries() {
s.cursorPos = (s.cursorPos + len(s.items)) % len(s.items)
}