-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.go
More file actions
102 lines (88 loc) · 1.86 KB
/
ui.go
File metadata and controls
102 lines (88 loc) · 1.86 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
package main
import (
"fmt"
"log"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
titleStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.ANSIColor(5)).MarginBottom(1)
itemStyle = lipgloss.NewStyle().PaddingLeft(2)
selectedStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.ANSIColor(6)).Bold(true)
)
type entry struct {
name string
host string
command string
}
type model struct {
entries []entry
cursor int
chosen *entry
quitting bool
}
func newModel(hosts []Host) model {
entries := make([]entry, 0, len(hosts))
for _, h := range hosts {
cmd := fmt.Sprintf("ssh -q -t %s@%s", h.User, h.Address)
if h.Key != "" {
cmd = fmt.Sprintf("ssh -q -t -i %s %s@%s", h.Key, h.User, h.Address)
}
entries = append(entries, entry{
name: h.Name,
host: h.Address,
command: cmd,
})
}
if len(entries) == 0 {
log.Println("no entries found")
os.Exit(1)
}
return model{
entries: entries,
}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
m.quitting = true
return m, tea.Quit
case "up":
if m.cursor > 0 {
m.cursor--
}
case "down":
if m.cursor < len(m.entries)-1 {
m.cursor++
}
case "enter":
chosen := m.entries[m.cursor]
m.chosen = &chosen
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
if m.quitting {
return ""
}
var s strings.Builder
s.WriteString(titleStyle.Render("select a host") + "\n")
for i, e := range m.entries {
line := fmt.Sprintf("%s (%s)", e.name, e.host)
if i == m.cursor {
s.WriteString(selectedStyle.Render("> "+line) + "\n")
} else {
s.WriteString(itemStyle.Render(" "+line) + "\n")
}
}
return s.String()
}