-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
170 lines (141 loc) · 3.88 KB
/
input.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
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
168
169
170
package prompts
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type (
errMsg error
inputModel struct {
required bool
validator func(value string) error
label string
err error
input textinput.Model
value string
secure bool
terminated bool
}
InputOptions struct {
Secure bool
Label string
Placeholder string
Required bool
Validator func(value string) error
}
)
// prompt user to fill an input, it returns the user input string
//
// example :
//
// prompts.InputBox(prompts.InputOptions{
// Secure: false, // hides the user input, very common for passwords
// Label: "what is your name?",
// Placeholder: "what is your name",
// Required: true,
// Validator: func(value string) error { // will be called when user submit, and returned error will be displayed to the user below the input
// if len(value) < 3 {
// return fmt.Errorf("minimum len is 3")
// }
// return nil
// },
// })
func InputBox(options InputOptions) (string, error) {
input := getInput()
input.Placeholder = strings.TrimSpace(options.Placeholder)
result := tea.NewProgram(inputModel{
label: strings.TrimSpace(options.Label),
required: options.Required,
validator: options.Validator,
input: input,
secure: options.Secure,
})
model, err := result.Run()
m := model.(inputModel)
return m.value, err
}
func (model inputModel) Init() tea.Cmd {
return textinput.Blink
}
func (model inputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
model.value = model.input.Value()
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
if model.validate() {
model.terminated = true
return model, tea.Quit
}
return model, nil
case "ctrl+c", "esc":
return model, tea.Quit
}
case errMsg:
model.err = msg
return model, nil
}
var cmd tea.Cmd
model.input, cmd = model.input.Update(msg)
return model, cmd
}
func (model inputModel) View() string {
if model.secure && len(model.input.Value()) > 0 {
model.input.SetValue(strings.Repeat("⬩", len(model.input.Value())))
}
v := model.input.View()
m := "\n"
m += style().Margin(0, 0, 0, 1).Foreground(color("#495867")).Render("┌─")
if len(model.label) > 0 {
model.label = " " + model.label + " "
}
m += style().Foreground(color("#07beb8")).Render(model.label)
m += style().Foreground(color("#495867")).Render(strings.Repeat("─", getTrmW()-charWidth(model.label)-5))
m += style().Foreground(color("#495867")).Render("┐")
m += "\n"
m += style().Foreground(color("#495867")).Render(" │")
m += v
ph := model.input.Placeholder
if len(ph) > 0 {
if len(model.value) > 0 {
m += style().Foreground(color("#495867")).Render(" │")
} else {
m += strings.Repeat(" ", getTrmW()-6-len(ph))
m += style().Foreground(color("#495867")).Render(" │")
}
} else {
m += style().Foreground(color("#495867")).Render(" │")
}
m += "\n"
m += style().Foreground(color("#495867")).Render(" └")
m += style().Foreground(color("#495867")).Render(strings.Repeat("─", getTrmW()-4))
m += style().Foreground(color("#495867")).Render("┘")
m += "\n"
if model.err != nil {
m += style().Foreground(color("#fb8500")).Italic(true).Render(" ⚠ " + model.err.Error())
} else if !model.terminated {
m += style().Foreground(color("#495867")).Render(" Press enter to quit.")
}
m += "\n\n"
return m
}
func getInput() textinput.Model {
textInput := textinput.New()
textInput.Focus()
textInput.Prompt = " "
textInput.CharLimit = 150
textInput.Width = getTrmW() - 7
return textInput
}
func (model *inputModel) validate() bool {
if model.required && model.value == "" {
model.err = fmt.Errorf("this field is required")
return false
}
if model.validator != nil {
model.err = model.validator(model.value)
return model.err == nil
}
return true
}