mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 16:49:41 +00:00
feat: display macOS battery capacity and cached memory, and refine status view presentation
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -18,16 +19,62 @@ func collectMemory() (MemoryStatus, error) {
|
||||
swap, _ := mem.SwapMemory()
|
||||
pressure := getMemoryPressure()
|
||||
|
||||
// On macOS, vm.Cached is 0, so we calculate from file-backed pages.
|
||||
cached := vm.Cached
|
||||
if runtime.GOOS == "darwin" && cached == 0 {
|
||||
cached = getFileBackedMemory()
|
||||
}
|
||||
|
||||
return MemoryStatus{
|
||||
Used: vm.Used,
|
||||
Total: vm.Total,
|
||||
UsedPercent: vm.UsedPercent,
|
||||
SwapUsed: swap.Used,
|
||||
SwapTotal: swap.Total,
|
||||
Cached: cached,
|
||||
Pressure: pressure,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getFileBackedMemory() uint64 {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||
defer cancel()
|
||||
out, err := runCmd(ctx, "vm_stat")
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Parse page size from first line: "Mach Virtual Memory Statistics: (page size of 16384 bytes)"
|
||||
var pageSize uint64 = 4096 // Default
|
||||
lines := strings.Split(out, "\n")
|
||||
if len(lines) > 0 {
|
||||
firstLine := lines[0]
|
||||
if strings.Contains(firstLine, "page size of") {
|
||||
if _, after, found := strings.Cut(firstLine, "page size of "); found {
|
||||
if before, _, found := strings.Cut(after, " bytes"); found {
|
||||
if size, err := strconv.ParseUint(strings.TrimSpace(before), 10, 64); err == nil {
|
||||
pageSize = size
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse "File-backed pages: 388975."
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, "File-backed pages:") {
|
||||
if _, after, found := strings.Cut(line, ":"); found {
|
||||
numStr := strings.TrimSpace(after)
|
||||
numStr = strings.TrimSuffix(numStr, ".")
|
||||
if pages, err := strconv.ParseUint(numStr, 10, 64); err == nil {
|
||||
return pages * pageSize
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func getMemoryPressure() string {
|
||||
if runtime.GOOS != "darwin" {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user