Skip to content

Commit 3509dee

Browse files
committed
Add --help/-h flag
1 parent ef4dde2 commit 3509dee

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
## Install via Homebrew
2727

28-
Help get us on the official Homebrew formulas by giving us a star! [mactop](https://github.com/context-labs/mactop)
28+
Help get us on the official Homebrew formulas by giving us a star and watching this repo! [mactop](https://github.com/context-labs/mactop)
2929

3030
```bash
3131
brew tap context-labs/mactop https://github.com/context-labs/mactop
@@ -84,6 +84,7 @@ sudo ./mactop
8484

8585
- `--interval` or `-i`: Set the powermetrics update interval in milliseconds. Default is 1000. (For low-end M chips, you may want to increase this value)
8686
- `--version` or `-v`: Print the version of mactop.
87+
- `--help` or `-h`: Show a help message about these flags and how to run mactop.
8788

8889
## mactop Commands
8990
Use the following keys to interact with the application while its running:
@@ -97,6 +98,8 @@ Use the following keys to interact with the application while its running:
9798
- M1 Max
9899
- M1 Ultra
99100

101+
(If you have a confirmed working M series chip that is not listed, please open an issue!)
102+
100103
## Contributing
101104

102105
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
@@ -124,6 +127,10 @@ Carsen Klock - [@carsenklock](https://twitter.com/carsenklock)
124127

125128
Project Link: [https://github.com/context-labs/mactop](https://github.com/context-labs/mactop)
126129

130+
## Disclaimer
131+
132+
This tool is not officially supported by Apple. It is provided as is, and may not work as expected. Use at your own risk.
133+
127134
## Acknowledgements
128135

129136
- [termui](https://github.com/gizak/termui) for the terminal UI framework.

main.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ func setupUI() {
9494
modelText = w.NewParagraph()
9595
modelText.Title = "Apple Silicon"
9696

97-
// Accessing map values with type assertion
9897
modelName, ok := appleSiliconModel["name"].(string)
9998
if !ok {
10099
modelName = "Unknown Model"
@@ -189,7 +188,6 @@ func setupGrid() {
189188
}
190189

191190
func switchGridLayout() {
192-
193191
if currentGridLayout == "default" {
194192
ui.Clear()
195193
newGrid := ui.NewGrid()
@@ -246,9 +244,18 @@ func StderrToLogfile(logfile *os.File) {
246244
syscall.Dup2(int(logfile.Fd()), 2)
247245
}
248246

249-
// mactop main function
250247
func main() {
251248

249+
if len(os.Args) > 1 && os.Args[1] == "--help" || len(os.Args) > 1 && os.Args[1] == "-h" {
250+
fmt.Println("Usage: mactop [--help] [--version] [--interval]")
251+
fmt.Println("--help: Show this help message")
252+
fmt.Println("--version: Show the version of mactop")
253+
fmt.Println("--interval: Set the powermetrics update interval in milliseconds. Default is 1000.")
254+
fmt.Println("You must use sudo to run mactop, as powermetrics requires root privileges.")
255+
fmt.Println("For more information, see https://github.com/context-labs/mactop")
256+
os.Exit(0)
257+
}
258+
252259
version := "v0.1.5"
253260
if len(os.Args) > 1 && os.Args[1] == "--version" || len(os.Args) > 1 && os.Args[1] == "-v" {
254261
fmt.Println("mactop version:", version)
@@ -489,16 +496,13 @@ func updateNetDiskUI(netdiskMetrics NetDiskMetrics) {
489496

490497
func updateProcessUI(processMetrics []ProcessMetrics) {
491498
ProcessInfo.Text = ""
492-
493499
sort.Slice(processMetrics, func(i, j int) bool {
494500
return processMetrics[i].CPUUsage > processMetrics[j].CPUUsage
495501
})
496-
497502
maxEntries := 15
498503
if len(processMetrics) > maxEntries {
499504
processMetrics = processMetrics[:maxEntries]
500505
}
501-
502506
for _, pm := range processMetrics {
503507
ProcessInfo.Text += fmt.Sprintf("%d - %s: %.2f ms/s\n", pm.ID, pm.Name, pm.CPUUsage)
504508
}
@@ -529,7 +533,6 @@ func parseProcessMetrics(powermetricsOutput string, processMetrics []ProcessMetr
529533
}
530534
}
531535

532-
// Sort by CPU ms/s in descending order
533536
sort.Slice(processMetrics, func(i, j int) bool {
534537
return processMetrics[i].CPUUsage > processMetrics[j].CPUUsage
535538
})
@@ -538,10 +541,8 @@ func parseProcessMetrics(powermetricsOutput string, processMetrics []ProcessMetr
538541
}
539542

540543
func parseActivityMetrics(powermetricsOutput string, netdiskMetrics NetDiskMetrics) NetDiskMetrics {
541-
542544
outRegex := regexp.MustCompile(`out:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
543545
inRegex := regexp.MustCompile(`in:\s*([\d.]+)\s*packets/s,\s*([\d.]+)\s*bytes/s`)
544-
545546
outMatches := outRegex.FindStringSubmatch(powermetricsOutput)
546547
inMatches := inRegex.FindStringSubmatch(powermetricsOutput)
547548

@@ -557,7 +558,6 @@ func parseActivityMetrics(powermetricsOutput string, netdiskMetrics NetDiskMetri
557558

558559
readRegex := regexp.MustCompile(`read:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
559560
writeRegex := regexp.MustCompile(`write:\s*([\d.]+)\s*ops/s\s*([\d.]+)\s*KBytes/s`)
560-
561561
readMatches := readRegex.FindStringSubmatch(powermetricsOutput)
562562
writeMatches := writeRegex.FindStringSubmatch(powermetricsOutput)
563563

@@ -587,9 +587,6 @@ func parseCPUMetrics(powermetricsOutput string, cpuMetrics CPUMetrics) CPUMetric
587587
residencyRe := regexp.MustCompile(`(\w+-Cluster)\s+HW active residency:\s+(\d+\.\d+)%`)
588588
frequencyRe := regexp.MustCompile(`(\w+-Cluster)\s+HW active frequency:\s+(\d+)\s+MHz`)
589589

590-
// const numReadings = 5
591-
// var pClusterReadings []int
592-
// var pClusterFreqReadings []int
593590
for _, line := range lines {
594591
residencyMatches := residencyRe.FindStringSubmatch(line)
595592
frequencyMatches := frequencyRe.FindStringSubmatch(line)

0 commit comments

Comments
 (0)