-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.go
More file actions
executable file
·438 lines (390 loc) · 11.6 KB
/
ui.go
File metadata and controls
executable file
·438 lines (390 loc) · 11.6 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package main
import (
"fmt"
"strings"
"time"
"unicode"
tea "github.com/charmbracelet/bubbletea"
)
func getSubMenuContent() []string {
return []string{
fmt.Sprintf("%s(s)%s %sssh%s", yellow, reset, BOLD, reset),
fmt.Sprintf("%s(w)%s Open in Browser", yellow, reset),
fmt.Sprintf("%s(o)%s sftp (os native)", yellow, reset),
fmt.Sprintf("%s(t)%s %ssftp (text UI)%s", yellow, reset, BOLD, reset),
fmt.Sprintf("%s(i)%s %sping%s", yellow, reset, BOLD, reset),
fmt.Sprintf("%s(c)%s %stcping%s", yellow, reset, BOLD, reset),
fmt.Sprintf("%s(k)%s ssh-copy-id", yellow, reset),
fmt.Sprintf("%s(d)%s %sDuplicate/Edit Profile%s", yellow, reset, BOLD, reset),
fmt.Sprintf("%s(p)%s %sSet Password%s", yellow, reset, BOLD, reset),
fmt.Sprintf("%s(h)%s Set http proxy", yellow, reset),
fmt.Sprintf("%s(x)%s Set SSH Tunnel", yellow, reset),
fmt.Sprintf("%s(5)%s Set Socks Tunnel", yellow, reset),
fmt.Sprintf("%s(u)%s Set URL", yellow, reset),
fmt.Sprintf("%s(f)%s Set Folder", yellow, reset),
fmt.Sprintf("%s(8)%s Set sshkey passphrase", yellow, reset),
fmt.Sprintf("%s(n)%s %sNotes%s", yellow, reset, BOLD, reset),
fmt.Sprintf("%s(r)%s Reveal Password", yellow, reset),
fmt.Sprintf("%s(y)%s Reveal sshkey passphrase", yellow, reset),
fmt.Sprintf("%s(X)%s Remove SSH Tunnel", yellow, reset),
fmt.Sprintf("%s(%%)%s Remove Socks Tunnel", yellow, reset),
fmt.Sprintf("%s(H)%s Remove http proxy", yellow, reset),
fmt.Sprintf("%s(*)%s Remove sshkey passphrase", yellow, reset),
fmt.Sprintf("%s(R)%s Remove Profile", yellow, reset),
goback,
}
}
// Common filter function
func (m *baseModel) filterChoices() {
if m.searchQuery == "" || !m.inSearchMode {
m.choices = make([]string, len(m.allChoices))
copy(m.choices, m.allChoices)
return
}
m.choices = nil
query := strings.ToLower(m.searchQuery)
for _, choice := range m.allChoices {
cleanChoice := strings.ReplaceAll(strings.ReplaceAll(choice, yellow, ""), reset, "")
if strings.Contains(strings.ToLower(cleanChoice), query) {
m.choices = append(m.choices, choice)
}
}
}
// SSH shortcut handlers
func (m *baseModel) handleSSHShortcuts(key string) (tea.Model, tea.Cmd) {
shortcuts := map[string]string{
"b": goback,
"s": fmt.Sprintf("%s(s)%s ssh", yellow, reset),
"w": fmt.Sprintf("%s(w)%s Open in Browser", yellow, reset),
"n": fmt.Sprintf("%s(n)%s Notes", yellow, reset),
"p": fmt.Sprintf("%s(p)%s Set Password", yellow, reset),
"t": fmt.Sprintf("%s(t)%s sftp (text UI)", yellow, reset),
"o": fmt.Sprintf("%s(o)%s sftp (os native)", yellow, reset),
"i": fmt.Sprintf("%s(i)%s ping", yellow, reset),
"c": fmt.Sprintf("%s(c)%s tcping", yellow, reset),
"d": fmt.Sprintf("%s(d)%s Duplicate/Edit Profile", yellow, reset),
"h": fmt.Sprintf("%s(h)%s Set http proxy", yellow, reset),
"5": fmt.Sprintf("%s(5)%s Set Socks Tunnel", yellow, reset),
"%": fmt.Sprintf("%s(%%)%s Remove Socks Tunnel", yellow, reset),
"H": fmt.Sprintf("%s(H)%s Remove http proxy", yellow, reset),
"R": fmt.Sprintf("%s(R)%s Remove Profile", yellow, reset),
"x": fmt.Sprintf("%s(x)%s Set SSH Tunnel", yellow, reset),
"u": fmt.Sprintf("%s(u)%s Set URL", yellow, reset),
"X": fmt.Sprintf("%s(X)%s Remove SSH Tunnel", yellow, reset),
"f": fmt.Sprintf("%s(f)%s Set Folder", yellow, reset),
"k": fmt.Sprintf("%s(k)%s ssh-copy-id", yellow, reset),
"8": fmt.Sprintf("%s(8)%s Set sshkey passphrase", yellow, reset),
"*": fmt.Sprintf("%s(*)%s Remove sshkey passphrase", yellow, reset),
"r": fmt.Sprintf("%s(r)%s Reveal Password", yellow, reset),
"y": fmt.Sprintf("%s(y)%s Reveal sshkey passphrase", yellow, reset),
}
if choice, exists := shortcuts[key]; exists {
m.choice = choice
m.searchQuery = ""
m.inSearchMode = false
m.filterChoices()
return ssh_model{*m}, tea.Quit
}
return ssh_model{*m}, nil
}
func (m *baseModel) handleProfileShortcuts(key string) (tea.Model, tea.Cmd) {
shortcuts := map[string]string{
"b": goback,
}
if choice, exists := shortcuts[key]; exists {
m.choice = choice
m.searchQuery = ""
m.inSearchMode = false
m.filterChoices()
return main_model{*m}, tea.Quit
}
return main_model{*m}, nil
}
// Common update logic
func (m *baseModel) updateBase(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "/":
if !m.inSearchMode {
m.inSearchMode = true
m.searchQuery = ""
m.filterChoices()
if m.cursor >= len(m.choices) {
m.cursor = max(0, len(m.choices)-1)
}
}
if m.isSSHContext {
return ssh_model{*m}, nil
}
return main_model{*m}, nil
case "ctrl+c", "q":
m.choice = ""
m.searchQuery = ""
m.inSearchMode = false
m.filterChoices()
if m.isSSHContext {
return ssh_model{*m}, tea.Quit
}
return main_model{*m}, tea.Quit
case "esc":
if m.inSearchMode {
m.inSearchMode = false
m.searchQuery = ""
m.filterChoices()
if m.cursor >= len(m.choices) {
m.cursor = max(0, len(m.choices)-1)
}
} else {
m.choice = ""
if m.isSSHContext {
return ssh_model{*m}, tea.Quit
}
return main_model{*m}, tea.Quit
}
if m.isSSHContext {
return ssh_model{*m}, nil
}
return main_model{*m}, nil
case "up":
if m.cursor > 0 {
m.cursor--
}
case "down":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case "enter", " ":
if len(m.choices) > 0 {
m.choice = m.choices[m.cursor]
m.searchQuery = ""
m.inSearchMode = false
m.filterChoices()
if m.isSSHContext {
return ssh_model{*m}, tea.Quit
}
return main_model{*m}, tea.Quit
}
}
if m.inSearchMode {
switch msg.Type {
case tea.KeyBackspace:
if len(m.searchQuery) > 0 {
m.searchQuery = m.searchQuery[:len(m.searchQuery)-1]
m.filterChoices()
if m.cursor >= len(m.choices) {
m.cursor = max(0, len(m.choices)-1)
}
}
case tea.KeyRunes:
if len(msg.Runes) == 1 && (unicode.IsLetter(msg.Runes[0]) || unicode.IsDigit(msg.Runes[0])) {
m.searchQuery += string(msg.Runes)
m.filterChoices()
if m.cursor >= len(m.choices) {
m.cursor = max(0, len(m.choices)-1)
}
}
}
} else if m.isSSHContext {
// Handle SSH keyboard shortcuts only when not in search mode
return m.handleSSHShortcuts(msg.String())
} else if !m.isSSHContext {
return m.handleProfileShortcuts(msg.String())
}
}
// Handle mouse clicks (detect double-click)
switch msg := msg.(type) {
case tea.MouseMsg:
// Only handle left button *release* clicks to avoid treating
// the press+release of a single click as a double-click.
if msg.Button == tea.MouseButtonLeft && msg.Type == tea.MouseRelease {
now := time.Now()
// If second click within threshold => treat as Enter
if now.Sub(m.lastClick) <= 200*time.Millisecond {
if len(m.choices) > 0 {
m.choice = m.choices[m.cursor]
m.searchQuery = ""
m.inSearchMode = false
m.filterChoices()
if m.isSSHContext {
return ssh_model{*m}, tea.Quit
}
return main_model{*m}, tea.Quit
}
} else {
// Single click: update cursor based on clicked row (best-effort)
// Map terminal Y coordinate to displayed choice index.
// This is a simple heuristic; adjust if your header/message layout differs.
// compute number of lines before choices start
headerLines := -1
if m.isSSHContext {
// leading blank + instruction + blank line
headerLines += 4
}
if m.inSearchMode {
// "Search mode: <query>" + blank line
headerLines += 3
} else if !m.isSSHContext {
// account for message lines (if any)
if m.message != "" {
headerLines += len(strings.Split(m.message, "\n"))
}
}
clicked := msg.Y - headerLines
if clicked >= 0 && clicked < len(m.choices) {
m.cursor = clicked
} else {
// clamp cursor
if m.cursor >= len(m.choices) {
m.cursor = max(0, len(m.choices)-1)
}
}
}
m.lastClick = now
}
}
if m.isSSHContext {
return ssh_model{*m}, nil
}
return main_model{*m}, nil
}
// Common view logic
func (m *baseModel) viewBase() string {
var s strings.Builder
if m.isSSHContext {
s.WriteString(fmt.Sprintf("\n%sPress shortcut key, / to search, arrows+Enter to select, or q to quit.%s\n\n", yellow, reset))
}
if m.inSearchMode {
s.WriteString(fmt.Sprintf("Search mode: %s\n\n", m.searchQuery))
} else if !m.isSSHContext {
s.WriteString(m.message)
}
if len(m.choices) == 0 {
s.WriteString("No matches found.\n")
} else {
for i, choice := range m.choices {
cursor := " "
if m.cursor == i {
cursor = fmt.Sprintf(" %s>%s", green, reset)
}
s.WriteString(fmt.Sprintf("%s %s\n", cursor, choice))
}
}
if !m.isSSHContext {
s.WriteString(fmt.Sprintf("\n%sPress shortcut key, / to search, arrows+Enter to select, or q to quit.%s\n", yellow, reset))
}
return s.String()
}
// Interface methods for ssh_model
func (m ssh_model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.baseModel.updateBase(msg)
}
// Interface methods for main_model
func (m main_model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.baseModel.updateBase(msg)
}
func (m ssh_model) View() string {
return m.baseModel.viewBase()
}
func (m main_model) View() string {
return m.baseModel.viewBase()
}
func (m ssh_model) Init() tea.Cmd {
return nil
}
func (m main_model) Init() tea.Cmd {
return nil
}
func main_ui(items []string, message string, isSshContextMenu bool) (string, error) {
var p *tea.Program
var secureItems []string
if isSecure {
maxlen := -1
for _, chosen := range items {
chosen = cleanTheString(chosen, "onlyColors")
if strings.Contains(chosen, sshIcon) {
if !strings.Contains(chosen, "New SSH Profile") {
chosenParts := strings.Split(chosen, " ")
if len(chosenParts[1]) > maxlen {
maxlen = len(chosenParts[1])
}
}
}
}
for _, chosen := range items {
newItem := ""
chosen = cleanTheString(chosen, "onlyColors")
if strings.Contains(chosen, sshIcon) {
if !strings.Contains(chosen, "New SSH Profile") {
chosenParts := strings.Split(chosen, " ")
newItem = private + " " + chosenParts[1] + strings.Repeat(" ", maxlen-len(chosenParts[1])) + " " + chosen[strings.Index(chosen, "("):]
secureItems = append(secureItems, newItem)
} else {
secureItems = append(secureItems, chosen)
}
} else {
secureItems = append(secureItems, chosen)
}
}
}
if isSshContextMenu {
p = tea.NewProgram(ssh_model{
baseModel: baseModel{
allChoices: getSubMenuContent(),
choices: getSubMenuContent(),
selected: make(map[int]string),
isSSHContext: true,
},
}, tea.WithAltScreen(), tea.WithMouseAllMotion())
} else {
if isSecure {
p = tea.NewProgram(main_model{
baseModel: baseModel{
allChoices: secureItems,
choices: secureItems,
selected: make(map[int]string),
message: message,
isSSHContext: false,
},
}, tea.WithAltScreen(), tea.WithMouseAllMotion())
} else {
p = tea.NewProgram(main_model{
baseModel: baseModel{
allChoices: items,
choices: items,
selected: make(map[int]string),
message: message,
isSSHContext: false,
},
}, tea.WithAltScreen(), tea.WithMouseAllMotion())
}
}
finalModel, err := p.Run()
if err != nil {
return "", err
}
if isSshContextMenu {
if m, ok := finalModel.(ssh_model); ok && m.choice != "" {
return m.choice, nil
}
} else {
if m, ok := finalModel.(main_model); ok && m.choice != "" {
if isSecure {
itemIndex := 0
for i, j := range secureItems {
if j == m.choice {
itemIndex = i
}
}
return items[itemIndex], nil
} else {
return m.choice, nil
}
}
}
return "", fmt.Errorf(`
(o o)
--oOO--(_)--OOo--
Have a nice day!
`)
}