-
Notifications
You must be signed in to change notification settings - Fork 18
/
ui_commit_inputs.go
277 lines (234 loc) · 7.22 KB
/
ui_commit_inputs.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"errors"
"strings"
"time"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
inputsTitleLayout = lipgloss.NewStyle().
Padding(1, 0, 1, 2)
inputsTitleStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#333333", Dark: "#DDDDDD"}).
Background(lipgloss.AdaptiveColor{Light: "#7653FF", Dark: "#7653FF"}).
Bold(true).
Padding(0, 1, 0, 1)
inputsBlockLayout = lipgloss.NewStyle().
Padding(0, 0, 1, 0)
inputsCursorStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#25A065", Dark: "#25A065"})
inputsPromptFocusStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), false, false, false, true).
BorderForeground(lipgloss.AdaptiveColor{Light: "#9F72FF", Dark: "#AD58B4"}).
Foreground(lipgloss.AdaptiveColor{Light: "#9A4AFF", Dark: "#EE6FF8"}).
Bold(true).
Padding(0, 0, 0, 1)
inputsPromptNormalStyle = lipgloss.NewStyle().
Padding(0, 0, 0, 2)
inputsTextFocusStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#1A1A1A", Dark: "#FFFDF5"}).
Bold(true)
inputsTextNormalStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#1A1A1A", Dark: "#FFFDF5"})
inputsButtonLayout = lipgloss.NewStyle().
Padding(2, 0, 1, 2)
inputsButtonFocusStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#FFFDF5", Dark: "#FFFDF5"}).
Background(lipgloss.AdaptiveColor{Light: "#25A065", Dark: "#25A065"}).
Padding(0, 1, 0, 1).
Bold(true)
inputsButtonNormalStyle = inputsButtonFocusStyle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#626262", Dark: "#DDDDDD"}).
Background(lipgloss.AdaptiveColor{Light: "#DDDDDD", Dark: "#626262"})
inputsErrLayout = lipgloss.NewStyle().
Padding(0, 0, 0, 1)
inputsErrStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#FF6037")).
Padding(0, 1, 0, 1).
Bold(true)
spinnerMetaFrame1 = lipgloss.NewStyle().Foreground(lipgloss.Color("1")).Render("❯")
spinnerMetaFrame2 = lipgloss.NewStyle().Foreground(lipgloss.Color("3")).Render("❯")
spinnerMetaFrame3 = lipgloss.NewStyle().Foreground(lipgloss.Color("2")).Render("❯")
)
type inputWithCheck struct {
input textinput.Model
checker func(s string) error
}
type inputsModel struct {
focusIndex int
title string
inputs []inputWithCheck
err error
errSpinner spinner.Model
editMode bool
}
func (m inputsModel) Init() tea.Cmd {
return tea.Batch(textinput.Blink, spinner.Tick)
}
func (m inputsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.editMode {
return m, nil
}
switch msg := msg.(type) {
case tea.KeyMsg:
var renderCursor bool
switch msg.String() {
case "ctrl+c", "esc":
return m, tea.Quit
case "enter":
if m.focusIndex == len(m.inputs) {
for _, iwc := range m.inputs {
if iwc.checker != nil {
m.err = iwc.checker(iwc.input.Value())
if m.err != nil {
return m, spinner.Tick
}
}
}
return m, func() tea.Msg { return done{nextView: COMMIT} }
}
fallthrough
case "tab", "down":
m.focusIndex++
if m.focusIndex > len(m.inputs) {
m.focusIndex = 0
}
renderCursor = true
case "shift+tab", "up":
m.focusIndex--
if m.focusIndex < 0 {
m.focusIndex = len(m.inputs)
}
renderCursor = true
}
if renderCursor {
cmds := make([]tea.Cmd, len(m.inputs))
for i := 0; i <= len(m.inputs)-1; i++ {
if i == m.focusIndex {
// Set focused state
cmds[i] = m.inputs[i].input.Focus()
m.inputs[i].input.PromptStyle = inputsPromptFocusStyle
m.inputs[i].input.TextStyle = inputsTextFocusStyle
continue
}
// Remove focused state
m.inputs[i].input.Blur()
m.inputs[i].input.PromptStyle = inputsPromptNormalStyle
m.inputs[i].input.TextStyle = inputsTextNormalStyle
}
return m, tea.Batch(cmds...)
}
case string:
m.title = "✔ Commit Type: " + msg
return m, nil
case spinner.TickMsg:
var cmd tea.Cmd
m.errSpinner, cmd = m.errSpinner.Update(msg)
return m, cmd
}
// Handle character input and blinking
return m, m.updateInputs(msg)
}
func (m *inputsModel) updateInputs(msg tea.Msg) tea.Cmd {
var cmds = make([]tea.Cmd, len(m.inputs)+1)
// Only text inputs with Focus() set will respond, so it's safe to simply
// update all of them here without any further logic.
for i := range m.inputs {
m.inputs[i].input, cmds[i] = m.inputs[i].input.Update(msg)
}
return tea.Batch(cmds...)
}
func (m inputsModel) View() string {
var b strings.Builder
for i := range m.inputs {
b.WriteString(m.inputs[i].input.View())
if i < len(m.inputs)-1 {
b.WriteRune('\n')
}
}
button := inputsButtonNormalStyle.Render("➜ Submit")
if m.focusIndex == len(m.inputs) {
button = inputsButtonFocusStyle.Render("➜ Submit")
}
// check input value
for _, iwc := range m.inputs {
if iwc.checker != nil {
m.err = iwc.checker(iwc.input.Value())
if m.err != nil {
button += inputsErrLayout.Render(m.errSpinner.View() + " " + inputsErrStyle.Render(m.err.Error()))
break
}
}
}
b.WriteString(inputsButtonLayout.Render(button))
title := inputsTitleLayout.Render(inputsTitleStyle.Render(m.title))
inputs := inputsBlockLayout.Render(b.String())
return lipgloss.JoinVertical(lipgloss.Left, title, inputs)
}
func newInputsModel() inputsModel {
m := inputsModel{
inputs: make([]inputWithCheck, 4),
}
for i := range m.inputs {
var iwc inputWithCheck
iwc.input = textinput.NewModel()
iwc.input.CursorStyle = inputsCursorStyle
iwc.input.CharLimit = 128
switch i {
case 0:
iwc.input.Prompt = "1. SCOPE "
iwc.input.Placeholder = "Specifying place of the commit change."
iwc.input.PromptStyle = inputsPromptFocusStyle
iwc.input.TextStyle = inputsTextFocusStyle
iwc.input.Focus()
iwc.checker = func(s string) error {
if strings.TrimSpace(s) == "" {
return errors.New("Scope cannot be empty")
}
return nil
}
case 1:
iwc.input.Prompt = "2. SUBJECT "
iwc.input.PromptStyle = inputsPromptNormalStyle
iwc.input.Placeholder = "A very short description of the change."
iwc.checker = func(s string) error {
if strings.TrimSpace(s) == "" {
return errors.New("Subject cannot be empty")
}
return nil
}
case 2:
iwc.input.Prompt = "3. BODY "
iwc.input.PromptStyle = inputsPromptNormalStyle
iwc.input.Placeholder = "Motivation and contrasts for the change."
case 3:
iwc.input.Prompt = "4. FOOTER "
iwc.input.PromptStyle = inputsPromptNormalStyle
iwc.input.Placeholder = "Description of the change, justification and migration notes."
}
m.inputs[i] = iwc
}
m.errSpinner = spinner.NewModel()
m.errSpinner.Spinner = spinner.Spinner{
Frames: []string{
// "❯ "
spinnerMetaFrame1 + " ",
// "❯❯ "
spinnerMetaFrame1 + spinnerMetaFrame2 + " ",
// "❯❯❯ "
spinnerMetaFrame1 + spinnerMetaFrame2 + spinnerMetaFrame3 + " ",
// " ❯❯❯"
" " + spinnerMetaFrame1 + spinnerMetaFrame2 + spinnerMetaFrame3,
// " ❯❯"
" " + spinnerMetaFrame1 + spinnerMetaFrame2,
// " ❯"
" " + spinnerMetaFrame1,
},
FPS: time.Second / 10,
}
return m
}