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
|
package tui
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"go.jolheiser.com/ugit/internal/git"
)
type repoForm struct {
inputs []textinput.Model
isPrivate bool
focusIndex int
width int
height int
done bool
save bool
selectedRepo *git.Repo
}
// newRepoForm creates a new repository editing form
func newRepoForm() repoForm {
var inputs []textinput.Model
nameInput := textinput.New()
nameInput.Placeholder = "Repository name"
nameInput.Focus()
nameInput.Width = 50
inputs = append(inputs, nameInput)
descInput := textinput.New()
descInput.Placeholder = "Repository description"
descInput.Width = 50
inputs = append(inputs, descInput)
tagsInput := textinput.New()
tagsInput.Placeholder = "Tags (comma separated)"
tagsInput.Width = 50
inputs = append(inputs, tagsInput)
return repoForm{
inputs: inputs,
focusIndex: 0,
}
}
// setValues sets the form values from the selected repo
func (f *repoForm) setValues(repo *git.Repo) {
f.inputs[0].SetValue(repo.Name())
f.inputs[1].SetValue(repo.Meta.Description)
f.inputs[2].SetValue(strings.Join(repo.Meta.Tags.Slice(), ", "))
f.isPrivate = repo.Meta.Private
f.inputs[0].Focus()
f.focusIndex = 0
}
// setSize sets the form dimensions
func (f *repoForm) setSize(width, height int) {
f.width = width
f.height = height
for i := range f.inputs {
f.inputs[i].Width = width - 10
}
}
// isPrivateToggleFocused returns true if the private toggle is focused
func (f *repoForm) isPrivateToggleFocused() bool {
return f.focusIndex == len(f.inputs)
}
// isSaveButtonFocused returns true if the save button is focused
func (f *repoForm) isSaveButtonFocused() bool {
return f.focusIndex == len(f.inputs)+1
}
// isCancelButtonFocused returns true if the cancel button is focused
func (f *repoForm) isCancelButtonFocused() bool {
return f.focusIndex == len(f.inputs)+2
}
// Update handles form updates
func (f repoForm) Update(msg tea.Msg) (repoForm, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "tab", "shift+tab", "up", "down":
if msg.String() == "up" || msg.String() == "shift+tab" {
f.focusIndex--
if f.focusIndex < 0 {
f.focusIndex = len(f.inputs) + 3 - 1
}
} else {
f.focusIndex++
if f.focusIndex >= len(f.inputs)+3 {
f.focusIndex = 0
}
}
for i := range f.inputs {
if i == f.focusIndex {
cmds = append(cmds, f.inputs[i].Focus())
} else {
f.inputs[i].Blur()
}
}
case "enter":
if f.isSaveButtonFocused() {
f.done = true
f.save = true
return f, nil
}
if f.isCancelButtonFocused() {
f.done = true
f.save = false
return f, nil
}
case "esc":
f.done = true
f.save = false
return f, nil
case " ":
if f.isPrivateToggleFocused() {
f.isPrivate = !f.isPrivate
}
if f.isSaveButtonFocused() {
f.done = true
f.save = true
return f, nil
}
if f.isCancelButtonFocused() {
f.done = true
f.save = false
return f, nil
}
}
}
for i := range f.inputs {
if i == f.focusIndex {
var cmd tea.Cmd
f.inputs[i], cmd = f.inputs[i].Update(msg)
cmds = append(cmds, cmd)
}
}
return f, tea.Batch(cmds...)
}
// View renders the form
func (f repoForm) View() string {
var b strings.Builder
formStyle := lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("170")).
Padding(1, 2)
titleStyle := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("170")).
MarginBottom(1)
b.WriteString(titleStyle.Render("Edit Repository"))
b.WriteString("\n\n")
b.WriteString("Repository Name:\n")
b.WriteString(f.inputs[0].View())
b.WriteString("\n\n")
b.WriteString("Description:\n")
b.WriteString(f.inputs[1].View())
b.WriteString("\n\n")
b.WriteString("Tags (comma separated):\n")
b.WriteString(f.inputs[2].View())
b.WriteString("\n\n")
toggleStyle := lipgloss.NewStyle()
if f.isPrivateToggleFocused() {
toggleStyle = toggleStyle.Foreground(lipgloss.Color("170")).Bold(true)
}
visibility := "Public 🔓"
if f.isPrivate {
visibility = "Private 🔒"
}
b.WriteString(toggleStyle.Render(fmt.Sprintf("[%s] %s", visibility, "Toggle with Space")))
b.WriteString("\n\n")
buttonStyle := lipgloss.NewStyle().
Padding(0, 3).
MarginRight(1)
focusedButtonStyle := buttonStyle.Copy().
Foreground(lipgloss.Color("0")).
Background(lipgloss.Color("170")).
Bold(true)
saveButton := buttonStyle.Render("[ Save ]")
cancelButton := buttonStyle.Render("[ Cancel ]")
if f.isSaveButtonFocused() {
saveButton = focusedButtonStyle.Render("[ Save ]")
}
if f.isCancelButtonFocused() {
cancelButton = focusedButtonStyle.Render("[ Cancel ]")
}
b.WriteString(saveButton + cancelButton)
b.WriteString("\n\n")
b.WriteString("\nTab: Next • Shift+Tab: Previous • Enter: Select • Esc: Cancel")
return formStyle.Width(f.width - 4).Render(b.String())
}
|