This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathidleWatcher.lua
213 lines (196 loc) · 7.16 KB
/
idleWatcher.lua
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
--- CPU Usage compare
local module = {}
module.newStyle = true
local canvas = require "hs.canvas"
local host = require "hs.host"
local timer = require "hs.timer"
local task = require "hs.task"
local fnutils = require "hs.fnutils"
local screen = require "hs.screen"
local stext = require "hs.styledtext"
local timestamp = function(date)
date = date or timer.secondsSinceEpoch()
return os.date("%F %T" .. string.format("%-5s", ((tostring(date):match("(%.%d+)$")) or "")), math.floor(date))
end
local logMessage = function(...) print(timestamp(), ...) end
local localSize = { h = 60, w = 200 }
local localFrame = {
x = screen.primaryScreen():frame().x + (screen.primaryScreen():frame().w - localSize.w) / 2,
y = screen.primaryScreen():frame().y + 5,
w = localSize.w,
h = localSize.h,
}
-- trickery to force an auto-update when it changes
module._frame = setmetatable({}, {
__index = function(self, key)
return localFrame[key]
end,
__newindex = function(self, key, value)
if localFrame[key] and type(value) == "number" then
localFrame[key] = value
-- only force update if the canvas already exists
if module._display then module._display:frame(localFrame) end
else
logMessage("++ invalid field or value type for display frame")
end
end,
__pairs = function(self)
return function(_, k)
local v
k, v = next(_, k)
return k, v
end, localFrame, nil
end
})
module._activeStyle = {
font = {
name = "Menlo-Italic",
size = 10,
},
color = { red = 1, green = .8, blue = .8 },
paragraphStyle = {
alignment = "center",
linebreak = "clip",
},
}
module._idleStyle = {
font = {
name = "Menlo-Italic",
size = 10,
},
color = { red = .8, green = 1, blue = .8 },
paragraphStyle = {
alignment = "center",
linebreak = "clip",
},
}
module._display = canvas.new(module._frame):appendElements(
{
id = "background",
type = "rectangle",
roundedRectRadii = { xRadius = 5.0, yRadius = 5.0 },
fillColor = { white = .50, alpha = .5 },
strokeColor = { white = .25, alpha = .5 },
strokeWidth = 2,
}, {
id = "topBox",
type = "rectangle",
frame = { x = "5%", y = "5%", w = "40%", h = "70%" },
roundedRectRadii = { xRadius = 5.0, yRadius = 5.0 },
fillColor = { white = .50, alpha = .5 },
strokeColor = { white = .25, alpha = .5 },
strokeWidth = 1,
}, {
id = "hostBox",
type = "rectangle",
frame = { x = "55%", y = "5%", w = "40%", h = "70%" },
roundedRectRadii = { xRadius = 5.0, yRadius = 5.0 },
fillColor = { white = .50, alpha = .5 },
strokeColor = { white = .25, alpha = .5 },
strokeWidth = 1,
}, {
id = "topLabel",
type = "text",
text = "/usr/bin/top",
textColor = { white = 0 },
textAlignment = "center",
textLineBreak = "clip",
textFont = "Menlo",
textSize = 8,
frame = { x = "5%", y = "5%", w = "40%", h = "20%" }
}, {
id = "hostLabel",
type = "text",
textColor = { white = 0 },
text = "hs.host.cpuUsage",
textAlignment = "center",
textLineBreak = "clip",
textFont = "Menlo",
textSize = 8,
frame = { x = "55%", y = "5%", w = "40%", h = "20%" }
}, {
id = "topValue",
type = "text",
frame = { x = "5%", y = "25%", w = "40%", h = "50%" }
}, {
id = "hostValue",
type = "text",
frame = { x = "55%", y = "25%", w = "40%", h = "50%" }
}, {
id = "timeLabel",
type = "text",
textColor = { white = 0 },
text = timestamp(),
textAlignment = "right",
textLineBreak = "clip",
textFont = "Menlo",
textSize = 8,
frame = { x = "5%", y = "75%", w = "90%", h = "20%" }
}
):behavior("canJoinAllSpaces")
module._timer = timer.new(5, function()
if module._topTask and module._topTask:isRunning() then
logMessage("+++ top task taking too long; killing it!")
module._topTask:terminate()
end
if module.newStyle then
module._cpuTime = host.cpuUsage(1, function(result)
module._display.hostValue.text =
stext.new(string.format("%.2f%%\n", result.overall.active), module._activeStyle) ..
stext.new(string.format("%.2f%%", result.overall.idle), module._idleStyle)
module._cpuTime = nil
end)
end
module._topTask = task.new("/usr/bin/top", function(c, o, e)
local idleStat, activeStat = nil, nil
if c == 0 then
-- we want the second line because top's first CPU data is not valid since it
-- calculates based on deltas
local count = 0
for i, v in ipairs(fnutils.split(o, "\n")) do
if v:match("CPU usage") then
count = count + 1
if count == 2 then
local userStat, sysStat
userStat, sysStat, idleStat = v:match("([%d%.]+)%% user, ([%d%.]+)%% sys, ([%d%.]+%%) idle")
if userStat and sysStat then
activeStat = tostring(tonumber(userStat) + tonumber(sysStat)) .. "%"
end
break
end
end
end
if not idleStat then
logMessage("+++ unable to get CPU usage number from top!")
end
else
logMessage("+++ unexpected return code for top!")
end
module._display.topValue.text = stext.new((activeStat or "XXX") .. "\n", module._activeStyle) ..
stext.new((idleStat or "XXX"), module._idleStyle)
if not module.newStyle then
local overallUsage = host.cpuUsage().overall
module._display.hostValue.text =
stext.new(string.format("%.2f%%\n", overallUsage.active), module._activeStyle) ..
stext.new(string.format("%.2f%%", overallUsage.idle), module._idleStyle)
end
module._display.timeLabel.text = timestamp()
end, {"-l", "2", "-n", "0"}):start()
end)
module.show = function()
module._display:show()
if not module._timer:running() then module._timer:start() end
end
module.hide = function()
module._display:hide()
if module._timer:running() then module._timer:stop() end
end
module.toggle = function()
if module._display:isShowing() then
module.hide()
else
module.show()
end
end
module.show()
return module