forked from cmsj/hammerspoon-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statuslets.lua
144 lines (117 loc) · 5.83 KB
/
statuslets.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
print("Loading statuslets")
local obj = {}
obj.__index = obj
obj.timer = nil
obj.fwText = nil
obj.fwDot = nil
obj.cccText = nil
obj.cccDot = nil
obj.arqText = nil
obj.arqDot = nil
hs.canvas.drawingWrapper(true)
function obj:render()
-- Destroy existing Statuslets
if self.fwText then self.fwText:delete() end
if self.fwDot then self.fwDot:delete() end
if self.cccText then self.cccText:delete() end
if self.cccDot then self.cccDot:delete() end
if self.arqText then self.arqText:delete() end
if self.arqDot then self.arqDot:delete() end
-- Defines for statuslets - little coloured dots in the corner of my screen that give me status info, see:
-- https://www.dropbox.com/s/3v2vyhi1beyujtj/Screenshot%202015-03-11%2016.13.25.png?dl=0
local initialScreenFrame = hs.screen.allScreens()[1]:fullFrame()
-- Start off by declaring the size of the text/circle objects and some anchor positions for them on screen
local statusDotWidth = 10
local statusTextWidth = 30
local statusTextHeight = 15
local statusText_x = initialScreenFrame.x + initialScreenFrame.w - statusDotWidth - statusTextWidth
local statusText_y = initialScreenFrame.y + initialScreenFrame.h - statusTextHeight
local statusDot_x = initialScreenFrame.x + initialScreenFrame.w - statusDotWidth
local statusDot_y = statusText_y
-- Now create the text/circle objects using the sizes/positions we just declared (plus a little fudging to make it all align properly)
self.fwText = hs.drawing.text(hs.geometry.rect(statusText_x + 5,
statusText_y - (statusTextHeight*2) + 2,
statusTextWidth,
statusTextHeight), "FW:")
self.cccText = hs.drawing.text(hs.geometry.rect(statusText_x,
statusText_y - statusTextHeight + 1,
statusTextWidth,
statusTextHeight), "CCC:")
self.arqText = hs.drawing.text(hs.geometry.rect(statusText_x + 4,
statusText_y,
statusTextWidth,
statusTextHeight), "Arq:")
self.fwDot = hs.drawing.circle(hs.geometry.rect(statusDot_x,
statusDot_y - (statusTextHeight*2) + 4,
statusDotWidth,
statusDotWidth))
self.cccDot = hs.drawing.circle(hs.geometry.rect(statusDot_x,
statusDot_y - statusTextHeight + 3,
statusDotWidth,
statusDotWidth))
self.arqDot = hs.drawing.circle(hs.geometry.rect(statusDot_x,
statusDot_y + 2,
statusDotWidth,
statusDotWidth))
-- Finally, configure the rendering style of the text/circle objects, clamp them to the desktop, and show them
self.fwText:setBehaviorByLabels({"canJoinAllSpaces", "stationary"}):setTextSize(11):sendToBack():show(0.5)
self.cccText:setBehaviorByLabels({"canJoinAllSpaces", "stationary"}):setTextSize(11):sendToBack():show(0.5)
self.arqText:setBehaviorByLabels({"canJoinAllSpaces", "stationary"}):setTextSize(11):sendToBack():show(0.5)
self.fwDot:setBehaviorByLabels({"canJoinAllSpaces", "stationary"}):setFillColor(hs.drawing.color.osx_yellow):setStroke(false):sendToBack():show(0.5)
self.cccDot:setBehaviorByLabels({"canJoinAllSpaces", "stationary"}):setFillColor(hs.drawing.color.osx_yellow):setStroke(false):sendToBack():show(0.5)
self.arqDot:setBehaviorByLabels({"canJoinAllSpaces", "stationary"}):setFillColor(hs.drawing.color.osx_yellow):setStroke(false):sendToBack():show(0.5)
return self
end
function obj.statusletCallbackFirewall(code, stdout, stderr)
local color
if string.find(stdout, "block all non-essential") then
color = hs.drawing.color.osx_green
else
color = hs.drawing.color.osx_red
end
obj.fwDot:setFillColor(color)
end
function obj.statusletCallbackCCC(code, stdout, stderr)
local color
if code == 0 then
color = hs.drawing.color.osx_green
else
color = hs.drawing.color.osx_red
end
obj.cccDot:setFillColor(color)
end
function obj.statusletCallbackArq(code, stdout, stderr)
local color
if code == 0 then
color = hs.drawing.color.osx_green
else
color = hs.drawing.color.osx_red
end
obj.arqDot:setFillColor(color)
end
function obj:update()
print("statuslets:update()")
hs.task.new("/usr/bin/sudo", self.statusletCallbackFirewall, {"/usr/libexec/ApplicationFirewall/socketfilterfw", "--getblockall"}):start()
hs.task.new("/usr/bin/grep", self.statusletCallbackCCC, {"-q", os.date("%d/%m/%Y"), os.getenv("HOME").."/.cccLast"}):start()
hs.task.new("/usr/bin/grep", self.statusletCallbackArq, {"-q", "Arq.*finished backup", "/var/log/system.log"}):start()
return self
end
-- Render our statuslets, trigger a timer to update them regularly, and do an initial update
function obj:start()
self:render()
self.timer = hs.timer.new(hs.timer.minutes(5), function(obj) obj:update() end)
self.timer:start()
self:update()
return self
end
function obj:stop()
self.timer:stop()
self.cccDot:delete()
self.cccText:delete()
self.arqDot:delete()
self.arqText:delete()
self.fwDot:delete()
self.fwText:delete()
return self
end
return obj