-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.lua
More file actions
100 lines (91 loc) · 3.54 KB
/
Copy pathcpu.lua
File metadata and controls
100 lines (91 loc) · 3.54 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
cpuMenu = hs.menubar.new()
local cpuTimer
local cpuWatchdog
local caffeinateWatcher
local fastPolling = false
local inactive = false
function getTopCPUProcess()
local cmd = [[ps -c -axo pcpu,comm -r | awk 'NR==2 {printf "%.1f|%s\n", $1, $2}']]
local output = hs.execute(cmd)
if not output or output == "" then
hs.printf("[CPU] No output from command")
return nil, nil
end
output = output:gsub("%s+$", "")
local cpuStr, name = output:match("^(%d+%.?%d*)|(.*)$")
if cpuStr and name then return name, tonumber(cpuStr) end
return nil, nil
end
function updateCPUMenu()
if inactive then return end
if hs.caffeinate.get("systemIdle") or hs.caffeinate.get("displayIdle") then return end
local name, cpu = getTopCPUProcess()
if not name or not cpu then return end
if cpu > 100 then
if not cpuMenu:isInMenuBar() then cpuMenu:returnToMenuBar() end
cpuMenu:setTitle("🔥")
cpuMenu:setTooltip(string.format("%.1f%% CPU — %s", cpu, name))
hs.printf("[CPU] 🔥 %.1f%% > 100%% CPU (%s)", cpu, name)
else
if cpuMenu:isInMenuBar() then cpuMenu:removeFromMenuBar() end
-- hs.printf("[CPU] 🧊 %.1f%% ≤ 100%% CPU", cpu)
end
if cpu > 100 and not fastPolling then
fastPolling = true
hs.printf("[CPU] Entering fast polling (every 5 s)")
if cpuTimer and cpuTimer:running() then cpuTimer:stop() end
cpuTimer = hs.timer.doEvery(5, updateCPUMenu)
elseif cpu <= 100 and fastPolling then
fastPolling = false
hs.printf("[CPU] Returning to normal polling (every 60 s)")
if cpuTimer and cpuTimer:running() then cpuTimer:stop() end
cpuTimer = hs.timer.doEvery(60, updateCPUMenu)
end
end
function startCPUTimer()
if cpuTimer and cpuTimer:running() then cpuTimer:stop() end
cpuTimer = hs.timer.doEvery(60, updateCPUMenu)
fastPolling = false
updateCPUMenu()
hs.printf("[CPU] Started CPU timer")
end
local function startCaffeinateWatcher()
if caffeinateWatcher then caffeinateWatcher:stop() end
caffeinateWatcher = hs.caffeinate.watcher.new(function(event)
if event == hs.caffeinate.watcher.screensDidSleep
or event == hs.caffeinate.watcher.screensDidLock
or event == hs.caffeinate.watcher.systemWillSleep then
if not inactive then
inactive = true
if cpuTimer then cpuTimer:stop() end
if cpuWatchdog then cpuWatchdog:stop() end
hs.printf("[CPU] Screen inactive — timers stopped")
end
elseif event == hs.caffeinate.watcher.screensDidUnlock
or event == hs.caffeinate.watcher.screensDidWake then
if inactive then
inactive = false
hs.printf("[CPU] Screen unlocked — restarting in 5s")
hs.timer.doAfter(5, function()
startCPUTimer()
if cpuWatchdog then cpuWatchdog:start() end
end)
end
end
end)
caffeinateWatcher:start()
hs.printf("[CPU] Caffeinate watcher started")
end
cpuWatchdog = hs.timer.doEvery(60, function()
if inactive then return end
if not cpuTimer or not cpuTimer:running() then
hs.printf("[CPU] Watchdog detected timer stopped — restarting")
startCPUTimer()
end
if not caffeinateWatcher or not caffeinateWatcher:start() then
hs.printf("[CPU] Watchdog detected dead watcher — restarting it")
startCaffeinateWatcher()
end
end)
startCaffeinateWatcher()
startCPUTimer()