-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_model.go
197 lines (167 loc) · 3.88 KB
/
process_model.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
package main
import (
"strconv"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type ProcesstModel struct {
table table.Model
help help.Model
keys keyMap
}
type updateProcessesMsg struct {
process []Process
}
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
func NewProcessModel() *ProcesstModel {
pm := ProcesstModel{
keys: keys,
help: help.New(),
}
processes := GetProcesses()
columns := []table.Column{
{Title: "S/N", Width: 8},
{Title: "Port", Width: 60},
{Title: "PID", Width: 10},
{Title: "Application", Width: 40},
}
rows := []table.Row{}
for i, p := range processes {
n := strconv.Itoa(i + 1)
rows = append(rows, []string{n, p.Port, p.PID, p.Application})
}
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(20),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(s)
pm.table = t
return &pm
}
func (pm ProcesstModel) Init() tea.Cmd {
return nil
}
func (pm ProcesstModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case updateProcessesMsg:
r := []table.Row{}
for i, p := range msg.process {
n := strconv.Itoa(i + 1)
r = append(r, []string{n, p.Port, p.PID, p.Application})
}
pm.table.SetRows(r)
return pm, nil
case tea.KeyMsg:
switch {
case key.Matches(msg, pm.keys.Quit):
return pm, tea.Quit
case key.Matches(msg, pm.keys.Escape):
if pm.table.Focused() {
pm.table.Blur()
} else {
pm.table.Focus()
}
case key.Matches(msg, pm.keys.Kill):
r := pm.table.SelectedRow()
p := Process{
Port: r[1],
PID: r[2],
Application: r[3],
}
p.kill()
return pm, tea.Batch(refetchProcesses)
case key.Matches(msg, pm.keys.Enter):
return pm, tea.Batch(
tea.Printf("Let's go to %s!", pm.table.SelectedRow()[1]),
)
}
}
pm.table, cmd = pm.table.Update(msg)
return pm, cmd
}
func refetchProcesses() tea.Msg {
newP := GetProcesses()
return updateProcessesMsg{
process: newP,
}
}
func (pm ProcesstModel) View() string {
helpView := pm.help.View(pm.keys)
return baseStyle.Render(pm.table.View()) + "\n" + helpView + "\n"
}
type keyMap struct {
Up key.Binding
Down key.Binding
Left key.Binding
Right key.Binding
Help key.Binding
Quit key.Binding
Kill key.Binding
Escape key.Binding
Enter key.Binding
}
var keys = keyMap{
Up: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "move down"),
),
Left: key.NewBinding(
key.WithKeys("left", "h"),
key.WithHelp("←/h", "move left"),
),
Right: key.NewBinding(
key.WithKeys("right", "l"),
key.WithHelp("→/l", "move right"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "toggle help"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
),
Kill: key.NewBinding(
key.WithKeys("k"),
key.WithHelp("k", "kill process"),
),
Escape: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "toggle focus"),
),
Enter: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select process"),
),
}
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Up, k.Down, k.Kill, k.Help, k.Quit}
}
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Up, k.Down, k.Kill}, // first column
{k.Help, k.Quit}, // second column
}
}