-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdates.go
More file actions
297 lines (269 loc) · 6.59 KB
/
updates.go
File metadata and controls
297 lines (269 loc) · 6.59 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"fmt"
"strconv"
"time"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
)
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// you can also get this triggered when the m state is changed
switch msg := msg.(type) {
// for the pass display
case dataFetchedMsg:
m.table = table.New(
table.WithRows(msg),
table.WithFocused(true),
table.WithHeight(17), // Its perfect dont change
table.WithColumns(m.table.Columns()),
table.WithStyles(TableStyles),
)
return m, nil
case dataFetchedMsg2:
m.table2 = table.New(
table.WithRows(msg),
table.WithFocused(true),
table.WithHeight(13),
table.WithColumns(m.table2.Columns()),
table.WithStyles(TableStyles2),
)
return m, nil
case clearMsg:
m.dbOpMsg = ""
return m, nil
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
if m.page == -1 {
switch msg.String() {
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter":
m.page = m.cursor
if m.cursor == 0 {
return m, fetchDataCmd()
}
if m.cursor == 1 {
return m, fetchDataCmd2()
}
return m, nil
}
}
// show pass view
if m.page == 0 {
// fetching and displaying passwords
var cmd tea.Cmd
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "0":
m.page = -1
case "b":
m.page = -1
case "2":
m.page = 1
return m, fetchDataCmd2()
case "3":
m.page = 2
case "enter":
row := m.table.SelectedRow()
if len(row) > 1 {
m.value = fmt.Sprintf("%s: 👤 %s → 🔐 %s", row[1], row[2], row[3])
}
return m, nil
case "esc":
if m.table.Focused() {
m.table.Blur()
} else {
m.table.Focus()
}
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}
if m.page == 1 {
// manager
var cmd tea.Cmd
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "q":
if m.state != optionsView && m.option != "create" {
return m, tea.Quit
}
case "tab":
if m.state == tableView {
m.state = optionsView
return m, nil
}
if m.state == optionsView && m.option == "create" {
m.createIndex++
if m.createIndex > 2 {
m.createIndex = 0
}
switch m.createIndex {
case 0:
m.textInput.Focus()
m.textInput2.Blur()
m.textInput3.Blur()
case 1:
m.textInput.Blur()
m.textInput2.Focus()
m.textInput3.Blur()
case 2:
m.textInput.Blur()
m.textInput2.Blur()
m.textInput3.Focus()
}
return m, nil
} else {
m.state = tableView
return m, nil
}
case "0":
if m.state != optionsView && m.option != "create" {
m.page = -1
}
case "b":
if m.state != optionsView && m.option != "create" {
m.page = -1
}
case "1":
if m.state != optionsView && m.option != "create" {
m.page = 0
return m, fetchDataCmd()
}
case "3":
if m.state != optionsView && m.option != "create" {
m.page = 2
}
// for optionsView
case "up", "k":
if m.state == optionsView {
if m.cursor2 > 0 {
m.cursor2--
}
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.state == optionsView {
if m.cursor2 < len(m.choices2)-1 {
m.cursor2++
}
}
case "enter":
if m.state == optionsView && m.cursor2 == 0 && m.option == "" {
m.option = "create"
}
if m.state == optionsView && m.cursor2 == 1 && m.option == "" {
m.option = "delete"
m.textInput4.Focus()
}
if m.state == optionsView && m.option == "create" {
if len(m.textInput.Value()) != 0 && len(m.textInput2.Value()) != 0 && len(m.textInput3.Value()) != 0 {
m.formValue[0] = m.textInput.Value()
m.formValue[1] = m.textInput2.Value()
m.formValue[2] = m.textInput3.Value()
m.option = ""
m.textInput.SetValue("")
m.textInput2.SetValue("")
m.textInput3.SetValue("")
UpdateMsg := insertData(m.formValue[0], m.formValue[1], m.formValue[2])
m.dbOpMsg = UpdateMsg
return m, tea.Batch(
tea.Tick(2*time.Second, func(t time.Time) tea.Msg {
return clearMsg{}
}), fetchDataCmd2(),
)
}
}
if m.state == optionsView && m.option == "delete" {
parsedId, err := strconv.ParseInt(m.textInput4.Value(), 10, 64)
if len(m.textInput4.Value()) != 0 && err == nil {
m.option = ""
m.textInput4.SetValue("")
deleteMsg := deleteData(int(parsedId))
m.dbOpMsg = deleteMsg
return m, tea.Batch(tea.Tick(2*time.Second, func(t time.Time) tea.Msg {
return clearMsg{}
}), fetchDataCmd2(),
)
}
}
// when esc jey is pressed
case "esc":
if m.state == tableView {
if m.table2.Focused() {
m.table2.Blur()
} else {
m.table2.Focus()
}
}
if m.state == optionsView && m.option == "create" {
m.option = ""
m.textInput.SetValue("")
m.textInput2.SetValue("")
m.textInput3.SetValue("")
return m, nil
}
if m.state == optionsView && m.option == "delete" {
m.option = ""
m.textInput4.SetValue("")
return m, nil
}
}
// when other navigating keys are pressed.
if m.state == tableView {
m.table2, cmd = m.table2.Update(msg)
return m, cmd
}
if m.state == optionsView && m.option == "create" {
switch m.createIndex {
case 0:
m.textInput, cmd = m.textInput.Update(msg)
case 1:
m.textInput2, cmd = m.textInput2.Update(msg)
case 2:
m.textInput3, cmd = m.textInput3.Update(msg)
}
return m, cmd
}
if m.state == optionsView && m.option == "delete" {
m.textInput4, cmd = m.textInput4.Update(msg)
return m, cmd
}
}
switch msg.String() {
// These keys should exit the program.
case "ctrl+c":
return m, tea.Quit
case "q":
return m, tea.Quit
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "0":
m.page = -1
case "1":
m.page = 0
return m, fetchDataCmd()
case "2":
m.page = 1
return m, fetchDataCmd2()
case "3":
m.page = 2
}
}
// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, nil
}