-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean.go
More file actions
167 lines (145 loc) · 3.83 KB
/
boolean.go
File metadata and controls
167 lines (145 loc) · 3.83 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
165
166
167
package input
import (
"atomicgo.dev/keyboard/keys"
"errors"
"fmt"
"github.com/liuuner/go-cli-input/cursor"
"slices"
"strings"
)
type BooleanState struct {
text []rune // The text being written
defaultBoolean int
acceptStrings []string
declineStrings []string
position int // Cursor position within the text
}
func NewBoolean(prompt string, defaultBoolean int) Input[BooleanState] {
i := newInput[BooleanState]()
state := BooleanState{
defaultBoolean: defaultBoolean,
text: []rune{},
acceptStrings: []string{"y", "ye", "yes"},
declineStrings: []string{"n", "no"},
position: 0,
}
inputPrompt := "[y/n] "
if defaultBoolean == 0 {
inputPrompt = "[y/N] "
} else if defaultBoolean == 1 {
inputPrompt = "[Y/n] "
}
return Input[BooleanState]{
render: renderBoolean,
handleInput: handleBoolean,
close: closeBoolean,
userPrompt: prompt,
inputPrompt: inputPrompt,
hasPrompt: i.hasPrompt,
hasSummary: i.hasSummary,
failedString: i.failedString,
completedString: i.completedString,
promptString: i.promptString,
isLevelWithPrompt: true,
state: state,
}
}
// Render the text relative to the initial position
func renderBoolean(s *BooleanState, _ bool) {
// Move back to the start relative to the initial position
cursor.MoveHorizontally(-s.position)
// Clear the line from the current cursor to avoid overwriting
fmt.Printf("\033[K")
// Render the current text
fmt.Print(string(s.text))
// Move cursor to the current position within the text
cursor.MoveHorizontally(s.position - len(s.text))
}
func handleBoolean(s *BooleanState, key keys.Key) (stop bool, err error) {
switch key.Code {
case keys.Left:
if s.position > 0 {
s.position--
cursor.MoveHorizontally(-1)
}
case keys.Right:
if s.position < len(s.text) {
s.position++
cursor.MoveHorizontally(1)
}
case keys.Backspace:
if s.position > 0 {
s.text = append(s.text[:s.position-1], s.text[s.position:]...)
s.position--
cursor.MoveHorizontally(-1)
}
case keys.Delete:
if s.position < len(s.text) {
// Remove the character at the current position
s.text = append(s.text[:s.position], s.text[s.position+1:]...)
}
case keys.Space:
// no whitespaces at start of text
if len(s.text) == 0 {
break
}
s.text = append(s.text[:s.position], append([]rune{' '}, s.text[s.position:]...)...)
s.position += len(key.Runes)
cursor.MoveHorizontally(len(key.Runes))
case keys.RuneKey:
// Add the rune to the text at the cursor position
runes := key.Runes
// Replace newLines with whitespaces when being pasted
for i, r := range runes {
if r == '\r' {
runes[i] = ' '
}
}
s.text = append(s.text[:s.position], append(key.Runes, s.text[s.position:]...)...)
s.position += len(key.Runes)
cursor.MoveHorizontally(len(key.Runes))
case keys.Enter:
_, boolErr := s.getBoolean()
if boolErr != nil {
stop = false
} else {
stop = true
}
}
return
}
func closeBoolean(s *BooleanState, err error) (summary string) {
// Move back to the start relative to the initial position
cursor.MoveHorizontally(-s.position)
// Clear the line from the current cursor to avoid overwriting
fmt.Printf("\033[K")
b, _ := s.getBoolean()
if b {
summary = "yes"
} else {
summary = "no"
}
if err != nil {
summary = err.Error()
}
return
}
func (s *BooleanState) Resolve() bool {
b, _ := s.getBoolean()
return b
}
func (s *BooleanState) getBoolean() (b bool, err error) {
text := strings.ToLower(string(s.text))
if text == "" && s.defaultBoolean == 0 {
b = false
} else if text == "" && s.defaultBoolean == 1 {
b = true
} else if slices.Contains(s.acceptStrings, text) {
b = true
} else if slices.Contains(s.declineStrings, text) {
b = false
} else {
err = errors.New("invalid value")
}
return
}