-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
327 lines (280 loc) · 7.5 KB
/
main.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
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
package main
import (
"fmt"
"strings"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
"github.com/termkit/skeleton"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// -----------------------------------------------------------------------------
// CPU Model
type cpuModel struct {
skeleton *skeleton.Skeleton
usage float64
lastUpdate time.Time
color string // tab color
borderColor string // border color
}
func newCPUModel(s *skeleton.Skeleton) *cpuModel {
return &cpuModel{
skeleton: s,
usage: 0,
lastUpdate: time.Now(),
color: "39", // bright blue
borderColor: "27", // darker blue
}
}
func (m *cpuModel) Init() tea.Cmd {
return nil
}
func (m *cpuModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case skeleton.UpdateMsg:
if time.Since(m.lastUpdate) >= time.Second {
if percent, err := cpu.Percent(0, false); err == nil && len(percent) > 0 {
m.usage = percent[0]
}
m.lastUpdate = time.Now()
}
case skeleton.IAMActivePage:
m.skeleton.SetActiveTabBorderColor(m.color)
m.skeleton.SetBorderColor(m.borderColor)
}
return m, nil
}
func (m *cpuModel) View() string {
mainStyle := lipgloss.NewStyle().
Align(lipgloss.Center).
Padding(1)
bar, valueColor := createProgressBar(progressBarOptions{
value: m.usage,
width: 50,
barColor: "205",
warningAt: 60,
criticalAt: 80,
})
// CPU usage value
usageValue := lipgloss.NewStyle().
Foreground(lipgloss.Color(valueColor)).
Bold(true).
Render(fmt.Sprintf("%.1f%%", m.usage))
info := []string{
"CPU Usage",
"",
bar,
"",
fmt.Sprintf("Current Usage: %s", usageValue),
"",
"Press Ctrl+Left/Right to switch tabs",
}
return mainStyle.Render(strings.Join(info, "\n"))
}
// -----------------------------------------------------------------------------
// Memory Model
type memoryModel struct {
skeleton *skeleton.Skeleton
used uint64
total uint64
lastUpdate time.Time
color string // tab color
borderColor string // border color
}
func newMemoryModel(s *skeleton.Skeleton) *memoryModel {
return &memoryModel{
skeleton: s,
lastUpdate: time.Now(),
color: "162", // bright purple
borderColor: "126", // darker purple
}
}
func (m *memoryModel) Init() tea.Cmd {
return nil
}
func (m *memoryModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case skeleton.UpdateMsg:
if time.Since(m.lastUpdate) >= time.Second {
if v, err := mem.VirtualMemory(); err == nil {
m.used = v.Used
m.total = v.Total
}
m.lastUpdate = time.Now()
}
case skeleton.IAMActivePage:
m.skeleton.SetActiveTabBorderColor(m.color)
m.skeleton.SetBorderColor(m.borderColor)
}
return m, nil
}
func (m *memoryModel) View() string {
mainStyle := lipgloss.NewStyle().
Align(lipgloss.Center).
Padding(1)
// Calculate memory values
usedGB := float64(m.used) / 1024 / 1024 / 1024
totalGB := float64(m.total) / 1024 / 1024 / 1024
usagePercent := float64(0)
if totalGB > 0 {
usagePercent = (usedGB / totalGB) * 100
}
bar, valueColor := createProgressBar(progressBarOptions{
value: usagePercent,
width: 50,
barColor: "205",
warningAt: 60,
criticalAt: 80,
})
// Memory values
memoryStats := lipgloss.NewStyle().
Foreground(lipgloss.Color(valueColor)).
Bold(true).
Render(fmt.Sprintf("%.1f GB / %.1f GB (%.1f%%)", usedGB, totalGB, usagePercent))
info := []string{
"Memory Usage",
"",
bar,
"",
fmt.Sprintf("Used Memory: %s", memoryStats),
"",
"Press Ctrl+Left/Right to switch tabs",
}
return mainStyle.Render(strings.Join(info, "\n"))
}
// -----------------------------------------------------------------------------
// Disk Model
type diskModel struct {
skeleton *skeleton.Skeleton
used uint64
total uint64
lastUpdate time.Time
color string // tab color
borderColor string // border color
}
func newDiskModel(s *skeleton.Skeleton) *diskModel {
return &diskModel{
skeleton: s,
lastUpdate: time.Now(),
color: "136", // bright gold
borderColor: "94", // darker gold
}
}
func (m *diskModel) Init() tea.Cmd {
return nil
}
func (m *diskModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case skeleton.UpdateMsg:
if time.Since(m.lastUpdate) >= time.Second {
if usage, err := disk.Usage("/"); err == nil {
m.used = usage.Used
m.total = usage.Total
}
m.lastUpdate = time.Now()
}
case skeleton.IAMActivePage:
m.skeleton.SetActiveTabBorderColor(m.color)
m.skeleton.SetBorderColor(m.borderColor)
}
return m, nil
}
func (m *diskModel) View() string {
mainStyle := lipgloss.NewStyle().
Align(lipgloss.Center).
Padding(1)
// Calculate disk values
usedGB := float64(m.used) / 1024 / 1024 / 1024
totalGB := float64(m.total) / 1024 / 1024 / 1024
usagePercent := float64(0)
if totalGB > 0 {
usagePercent = (usedGB / totalGB) * 100
}
bar, valueColor := createProgressBar(progressBarOptions{
value: usagePercent,
width: 50,
barColor: "205",
warningAt: 70,
criticalAt: 90,
})
// Disk values
diskStats := lipgloss.NewStyle().
Foreground(lipgloss.Color(valueColor)).
Bold(true).
Render(fmt.Sprintf("%.1f GB / %.1f GB (%.1f%%)", usedGB, totalGB, usagePercent))
info := []string{
"Disk Usage",
"",
bar,
"",
fmt.Sprintf("Used Space: %s", diskStats),
fmt.Sprintf("Free Space: %.1f GB", totalGB-usedGB),
"",
"Press Ctrl+Left/Right to switch tabs",
}
return mainStyle.Render(strings.Join(info, "\n"))
}
// -----------------------------------------------------------------------------
// Helper Functions
// progressBar creates a styled progress bar with given percentage
type progressBarOptions struct {
value float64 // percentage value (0-100)
width int // total width of the bar
barColor string // color of the progress bar
warningAt float64 // warning threshold (orange)
criticalAt float64 // critical threshold (red)
}
func createProgressBar(opts progressBarOptions) (string, string) {
// Ensure value is within bounds
if opts.value < 0 {
opts.value = 0
} else if opts.value > 100 {
opts.value = 100
}
// Calculate used width
usedWidth := int(float64(opts.width) * opts.value / 100)
if usedWidth < 0 {
usedWidth = 0
} else if usedWidth > opts.width {
usedWidth = opts.width
}
// Create progress bar
barStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(opts.barColor))
bar := "█" + strings.Repeat("█", usedWidth) +
strings.Repeat("░", opts.width-usedWidth)
// Select color based on thresholds
valueColor := "39" // blue for normal
if opts.value > opts.criticalAt {
valueColor = "196" // red for critical
} else if opts.value > opts.warningAt {
valueColor = "208" // orange for warning
}
return barStyle.Render(bar), valueColor
}
// -----------------------------------------------------------------------------
// Main Program
func main() {
s := skeleton.NewSkeleton()
// Add tabs (pages)
s.AddPage("cpu", "CPU", newCPUModel(s))
s.AddPage("memory", "Memory", newMemoryModel(s))
s.AddPage("disk", "Disk", newDiskModel(s))
s.SetActiveTabBorderColor("39") // dark blue
// Add widgets
s.AddWidget("app", "System Monitor")
s.AddWidget("time", time.Now().Format("15:04:05"))
// Update system stats every second
go func() {
for {
time.Sleep(time.Second)
s.TriggerUpdate()
s.UpdateWidgetValue("time", time.Now().Format("15:04:05"))
}
}()
p := tea.NewProgram(s)
if err := p.Start(); err != nil {
panic(err)
}
}