Skip to content

Commit 1f3ddf7

Browse files
authored
Merge pull request #30 from raghav2005/feature/help-menu
feature/help-menu
2 parents ca959f9 + 2bdc27a commit 1f3ddf7

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ Options are 'green', 'red', 'blue', 'cyan', 'magenta', 'yellow', and 'white'. (-
9595
Use the following keys to interact with the application while its running:
9696
- `q`: Quit the application.
9797
- `r`: Refresh the UI data manually.
98-
- `l`: Toggle the current layout.
98+
- `l`: Toggle the main display's layout.
99+
- `h`: Toggle the help menu.
99100

100101
## Example Theme (Green) Screenshot (sudo mactop -c green)
101102

main.go

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,17 @@ func (e *EventThrottler) Notify() {
7878
}
7979

8080
var (
81-
cpu1Gauge, cpu2Gauge, gpuGauge, aneGauge *w.Gauge
82-
TotalPowerChart *w.BarChart
83-
memoryGauge *w.Gauge
84-
modelText, PowerChart, NetworkInfo, ProcessInfo *w.Paragraph
85-
grid *ui.Grid
86-
powerValues []float64
87-
lastUpdateTime time.Time
88-
stderrLogger = log.New(os.Stderr, "", 0)
89-
currentGridLayout = "default"
90-
updateInterval = 1000
81+
cpu1Gauge, cpu2Gauge, gpuGauge, aneGauge *w.Gauge
82+
TotalPowerChart *w.BarChart
83+
memoryGauge *w.Gauge
84+
modelText, PowerChart, NetworkInfo, ProcessInfo, helpText *w.Paragraph
85+
grid *ui.Grid
86+
powerValues []float64
87+
lastUpdateTime time.Time
88+
stderrLogger = log.New(os.Stderr, "", 0)
89+
currentGridLayout = "default"
90+
showHelp = false
91+
updateInterval = 1000
9192
)
9293

9394
var (
@@ -104,8 +105,9 @@ var (
104105

105106
func setupUI() {
106107
appleSiliconModel := getSOCInfo()
107-
modelText = w.NewParagraph()
108+
modelText, helpText = w.NewParagraph(), w.NewParagraph()
108109
modelText.Title = "Apple Silicon"
110+
helpText.Title = "Help Menu"
109111
modelName, ok := appleSiliconModel["name"].(string)
110112
if !ok {
111113
modelName = "Unknown Model"
@@ -129,41 +131,28 @@ func setupUI() {
129131
pCoreCount,
130132
gpuCoreCount,
131133
)
134+
helpText.Text = "- r: Refresh the UI data manually\n- l: Toggle the main display's layout\n- h: Toggle this help menu\n- ?: Toggle this help menu\n- q: Quit the application\n- <C-c>: Quit the application"
132135
stderrLogger.Printf("Model: %s\nE-Core Count: %d\nP-Core Count: %d\nGPU Core Count: %s",
133136
modelName,
134137
eCoreCount,
135138
pCoreCount,
136139
gpuCoreCount,
137140
)
138141

139-
cpu1Gauge = w.NewGauge()
140-
cpu1Gauge.Title = "E-CPU Usage"
141-
cpu1Gauge.Percent = 0
142-
cpu1Gauge.BarColor = ui.ColorGreen
143-
144-
cpu2Gauge = w.NewGauge()
145-
cpu2Gauge.Title = "P-CPU Usage"
146-
cpu2Gauge.Percent = 0
147-
cpu2Gauge.BarColor = ui.ColorYellow
148-
149-
gpuGauge = w.NewGauge()
150-
gpuGauge.Title = "GPU Usage"
151-
gpuGauge.Percent = 0
152-
gpuGauge.BarColor = ui.ColorMagenta
153-
154-
aneGauge = w.NewGauge()
155-
aneGauge.Title = "ANE"
156-
aneGauge.Percent = 0
157-
aneGauge.BarColor = ui.ColorBlue
158-
159-
PowerChart = w.NewParagraph()
160-
PowerChart.Title = "Power Usage"
161-
162-
NetworkInfo = w.NewParagraph()
163-
NetworkInfo.Title = "Network & Disk Info"
142+
gauges := []*w.Gauge{
143+
w.NewGauge(), w.NewGauge(), w.NewGauge(), w.NewGauge(), w.NewGauge(),
144+
}
145+
titles := []string{"E-CPU Usage", "P-CPU Usage", "GPU Usage", "ANE", "Memory Usage"}
146+
colors := []ui.Color{ui.ColorGreen, ui.ColorYellow, ui.ColorMagenta, ui.ColorBlue, ui.ColorCyan}
147+
for i, gauge := range gauges {
148+
gauge.Percent = 0
149+
gauge.Title = titles[i]
150+
gauge.BarColor = colors[i]
151+
}
152+
cpu1Gauge, cpu2Gauge, gpuGauge, aneGauge, memoryGauge = gauges[0], gauges[1], gauges[2], gauges[3], gauges[4]
164153

165-
ProcessInfo = w.NewParagraph()
166-
ProcessInfo.Title = "Process Info"
154+
PowerChart, NetworkInfo, ProcessInfo = w.NewParagraph(), w.NewParagraph(), w.NewParagraph()
155+
PowerChart.Title, NetworkInfo.Title, ProcessInfo.Title = "Power Usage", "Network & Disk Info", "Process Info"
167156

168157
TotalPowerChart = w.NewBarChart()
169158
TotalPowerChart.Title = "~ W Total Power"
@@ -175,10 +164,6 @@ func setupUI() {
175164
TotalPowerChart.NumFormatter = func(num float64) string {
176165
return ""
177166
}
178-
memoryGauge = w.NewGauge()
179-
memoryGauge.Title = "Memory Usage"
180-
memoryGauge.Percent = 0
181-
memoryGauge.BarColor = ui.ColorCyan
182167
}
183168

184169
func setupGrid() {
@@ -248,6 +233,31 @@ func switchGridLayout() {
248233
}
249234
}
250235

236+
func toggleHelpMenu() {
237+
showHelp = !showHelp
238+
if showHelp {
239+
newGrid := ui.NewGrid()
240+
newGrid.Set(
241+
ui.NewRow(1.0,
242+
ui.NewCol(1.0, helpText),
243+
),
244+
)
245+
termWidth, termHeight := ui.TerminalDimensions()
246+
helpTextGridWidth := termWidth / 3
247+
helpTextGridHeight := termHeight / 2
248+
x := (termWidth - helpTextGridWidth) / 2
249+
y := (termHeight - helpTextGridHeight) / 2
250+
newGrid.SetRect(x, y, x+helpTextGridWidth, y+helpTextGridHeight)
251+
grid = newGrid
252+
} else {
253+
currentGridLayout = map[bool]string{
254+
true: "alternative",
255+
false: "default",
256+
}[currentGridLayout == "default"]
257+
switchGridLayout()
258+
}
259+
}
260+
251261
func StderrToLogfile(logfile *os.File) {
252262
syscall.Dup2(int(logfile.Fd()), 2)
253263
}
@@ -431,6 +441,12 @@ func main() {
431441
ui.Clear()
432442
switchGridLayout()
433443
ui.Render(grid)
444+
case "h", "?": // "h" or "?" to open help menu
445+
termWidth, termHeight := ui.TerminalDimensions()
446+
grid.SetRect(0, 0, termWidth, termHeight)
447+
ui.Clear()
448+
toggleHelpMenu()
449+
ui.Render(grid)
434450
}
435451
case <-done:
436452
ui.Close()

0 commit comments

Comments
 (0)