-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
276 lines (245 loc) · 7.34 KB
/
Copy pathmain.go
File metadata and controls
276 lines (245 loc) · 7.34 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
package main
import (
"fmt"
"log"
"math"
"math/rand"
"os"
"time"
ui "github.com/numbergroup/gotui"
"github.com/numbergroup/gotui/widgets"
)
func main() {
// 1. Header
p := widgets.NewParagraph()
p.Title = "gotui Dashboard"
p.Text = "PRESS q TO QUIT | Responsive Grid Layout Demo | TrueColor Support"
p.TextStyle.Fg = ui.ColorWhite
p.BorderStyle.Fg = ui.ColorLightCyan
p.TitleStyle = ui.NewStyle(ui.ColorLightCyan, ui.ColorClear, ui.ModifierBold)
p.TitleAlignment = ui.AlignCenter
p.TitleRight = "v5.0.0"
p.BorderRounded = false // Variety: Non-rounded border
// 2. Sparklines (CPU Usage)
slData := make([]float64, 200)
sl := widgets.NewSparkline()
sl.Data = slData
// 4. Bar Chart (Network Traffic)
bc := widgets.NewBarChart()
bc.Title = "Network Traffic"
bc.TitleBottom = "MB/s" // Bottom title demo
bc.TitleBottomAlignment = ui.AlignRight
bc.Labels = []string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
bc.BarColors = []ui.Color{ui.ColorBlue, ui.ColorLightCyan}
bc.NumStyles = []ui.Style{ui.NewStyle(ui.ColorWhite)}
bc.Data = []float64{3, 2, 5, 3, 9, 5}
bc.TitleStyle.Fg = ui.ColorBlue
bc.BorderStyle.Fg = ui.ColorBlue
bc.BorderRounded = true
bc.BarWidth = 0 // Auto-Width
bc.BarGap = 1
bc.MaxVal = 10 // Fix scaling to be consistent
sl.LineColor = ui.ColorGreen
sl.TitleStyle.Fg = ui.ColorWhite
sl.MaxVal = 100
sl2 := widgets.NewSparkline()
sl2.Data = slData
sl2.TitleStyle.Fg = ui.ColorWhite
sl2.LineColor = ui.ColorMagenta
sl2.MaxVal = 100
slg := widgets.NewSparklineGroup(sl, sl2)
slg.Title = "CPU Usage"
slg.TitleStyle.Fg = ui.ColorGreen
slg.BorderStyle.Fg = ui.ColorGreen
slg.TitleRight = "Core 0 & 1" // Right title demo
slg.BorderRounded = true
// 3. Line Gauges (Memory & Load)
lg1 := widgets.NewLineGauge()
lg1.Title = "Memory"
lg1.Percent = 45
lg1.BarRune = '■'
lg1.LineColor = ui.ColorYellow
lg1.TitleStyle.Fg = ui.ColorYellow
lg1.BorderRounded = true
lg2 := widgets.NewLineGauge()
lg2.Title = "Load"
lg2.Percent = 60
lg2.BarRune = '▰' // Future style
lg2.BarRuneEmpty = '▱'
lg2.LineColor = ui.ColorRed
lg2.TitleStyle.Fg = ui.ColorRed
lg2.BorderRounded = true
// 5. Pie Chart (Disk Space)
pc := widgets.NewPieChart()
pc.Title = "Disk Usage"
pc.Data = []float64{10, 20, 30, 40}
pc.Colors = []ui.Color{ui.ColorRed, ui.ColorYellow, ui.ColorGreen, ui.ColorBlue}
pc.LabelFormatter = func(i int, v float64) string {
return fmt.Sprintf("%.0f%%", v)
}
pc.TitleStyle.Fg = ui.ColorMagenta
pc.BorderStyle.Fg = ui.ColorMagenta
pc.BorderRounded = true
// 6. Plot (Response Time)
plotData := make([][]float64, 2)
plotData[0] = make([]float64, 50)
plotData[1] = make([]float64, 50)
plot := widgets.NewPlot()
plot.Title = "Response Time"
plot.TitleBottom = "(ms)"
plot.Data = plotData
plot.AxesColor = ui.ColorWhite
plot.LineColors[0] = ui.ColorGreen // Brighter color
plot.LineColors[1] = ui.ColorYellow // Brighter color
plot.Marker = widgets.MarkerDot // Clearer than Braille
plot.TitleStyle.Fg = ui.ColorLightCyan
plot.BorderStyle.Fg = ui.ColorLightCyan
plot.BorderRounded = true
// 7. List (System Logs)
l := widgets.NewList()
l.Title = "System Logs"
l.Rows = []string{
"[INFO] System started",
"[INFO] Service A initialized",
"[WARN] Connection timeout (retrying)",
"[INFO] Cache cleared",
"[ERROR] User authentication failed",
"[INFO] Worker pool scaled up",
"[INFO] Backup completed",
"[INFO] Health check passed",
"[WARN] High memory usage detected",
"[INFO] GC triggered",
"[INFO] New client connected (192.168.1.10)",
"[INFO] Request processed in 45ms",
"[ERROR] Database connection lost",
"[INFO] Reconnecting to DB...",
"[INFO] DB Connected",
"[INFO] Job #1234 completed",
"[WARN] Rate limit exceeded",
"[INFO] Retrying Job #1234",
"[INFO] Cache warm-up started",
"[INFO] Cache warm-up finished",
"[INFO] Service B initialized",
"[INFO] API Gateway ready",
"[INFO] Listening on port 8080",
"[WARN] Deprecated API usage /v1/users",
"[INFO] User A logged out",
"[INFO] User B logged in",
"[INFO] Metrics flushed to InfluxDB",
"[INFO] Rotating logs",
"[INFO] Running cleanup task",
"[INFO] Temp files deleted",
"[ERROR] Disk space low (<10%)",
"[INFO] Alert sent to admin",
}
l.TextStyle.Fg = ui.ColorYellow
l.SelectedStyle = ui.NewStyle(ui.ColorBlack, ui.ColorYellow)
l.TitleStyle.Fg = ui.ColorYellow
l.BorderStyle.Fg = ui.ColorYellow
l.TitleBottom = "Page 1/1"
l.TitleBottomAlignment = ui.AlignRight
l.BorderRounded = true
// 8. Gauge (Download)
g := widgets.NewGauge()
g.Title = "Download"
g.Percent = 50
g.BarColor = ui.ColorGreen
g.BorderStyle.Fg = ui.ColorGreen
g.TitleStyle.Fg = ui.ColorGreen
g.BorderRounded = true
// 9. Grid Layout
grid := ui.NewGrid()
//termWidth, termHeight := ui.TerminalDimensions()
//grid.SetRect(0, 0, termWidth, termHeight)
grid.Set(
ui.NewRow(1.0/10,
ui.NewCol(1.0, p), // Header
),
ui.NewRow(2.0/10,
ui.NewCol(1.0/2, slg), // Sparklines
ui.NewCol(1.0/2, // Gauges Stacked
ui.NewRow(1.0/2, lg1),
ui.NewRow(1.0/2, lg2),
),
),
ui.NewRow(3.5/10, // Increased Height
ui.NewCol(1.0/3, bc), // Bar Chart
ui.NewCol(1.0/3, pc), // Pie Chart
ui.NewCol(1.0/3, plot), // Plot
),
ui.NewRow(3.5/10,
ui.NewCol(2.0/3, l), // Logs
ui.NewCol(1.0/3, g), // Gauge
),
)
// Check for screenshot mode
if len(os.Args) > 1 && os.Args[1] == "-screenshot" {
termWidth, termHeight := 1024/7, 768/13 // Approximate cols/rows (e.g. 146x59)
// Or standard terminal size
termWidth, termHeight = 120, 40
grid.SetRect(0, 0, termWidth, termHeight)
if err := ui.SaveImage("screenshot.png", termWidth, termHeight, grid); err != nil {
log.Fatalf("failed to take screenshot: %v", err)
}
fmt.Println("Screenshot saved to screenshot.png")
return
}
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize gotui: %v", err)
}
defer ui.Close()
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
ui.Render(grid)
// Update Function
tickerCount := 0
uiEvents := ui.PollEvents()
ticker := time.NewTicker(100 * time.Millisecond).C // 10 FPS updates
for {
select {
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "<MouseWheelUp>":
l.ScrollUp()
ui.Render(grid)
case "<MouseWheelDown>":
l.ScrollDown()
ui.Render(grid)
case "<Resize>":
payload := e.Payload.(ui.Resize)
grid.SetRect(0, 0, payload.Width, payload.Height)
ui.Clear()
ui.Render(grid)
}
case <-ticker:
tickerCount++
// Update Sparklines
sl.Data = append(sl.Data[1:], float64(rand.Intn(100)))
sl2.Data = append(sl2.Data[1:], float64(rand.Intn(100)))
// Update Gauges
if tickerCount%5 == 0 {
lg1.Percent = (lg1.Percent + rand.Intn(5)) % 100
lg2.Percent = (lg2.Percent + rand.Intn(3)) % 100
}
// Update BarChart
if tickerCount%10 == 0 {
for i := range bc.Data {
bc.Data[i] = float64(rand.Intn(10))
}
}
// Update Gauge
g.Percent = (g.Percent + 2) % 100
// Update Plot
plotData[0] = append(plotData[0][1:], 20+10*math.Sin(float64(tickerCount)/10.0)+float64(rand.Intn(5)))
plotData[1] = append(plotData[1][1:], 40+20*math.Cos(float64(tickerCount)/15.0)+float64(rand.Intn(10)))
plot.Data = plotData
// Update Logs (Rotate selection)
// if tickerCount%20 == 0 {
// l.SelectedRow = (l.SelectedRow + 1) % len(l.Rows)
// }
ui.Render(grid)
}
}
}