-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpookfile
413 lines (360 loc) · 10.2 KB
/
Spookfile
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
-- vim: syntax=moon
fs = require 'fs'
moonscript = require 'moonscript'
package.path = "#{package.path};lib/?.lua;lib/?/init.lua"
package.moonpath = moonscript.create_moonpath(package.path)
insert: append = table
reload_spook = _G.reload_spook
log = require 'systemd-journal'
_G.log = log
gettimeofday = gettimeofday
round = math.round
S = require "syscall"
:execute, :spawn = require 'process'
:after = require 'moonbar_util'
:format = string
insert: append = table
command = require 'command'
desktop_session = os.getenv('DESKTOP_SESSION') or ''
on_sway_wm = desktop_session\match '^sway'
has_executable = (name) ->
paths = os.getenv("PATH")\split ":"
for path in *paths
if fs.is_dir path
for entry, attr in fs.dirtree(path)
return true if fs.basename(entry) == name
false
-- How much log output can you handle? (ERR, WARN, INFO, DEBUG)
log_level "INFO"
log.info "loading moonbar..."
restart_requested = false
request_restart = -> restart_requested = true
-- We don't want buffered io on stdout here
io.stdout\setvbuf 'no'
json = require "json"
normal_color = "#FFFFFF"
warning_color = "#e8e375"
critical_color = "#ee6874"
good_color = "#87ed91"
was_reloaded = -> os.getenv('__SPOOK_RELOADED') == 'yes'
is_reloaded = -> S.setenv('__SPOOK_RELOADED', 'yes', true)
is_initialized = -> was_reloaded! or _G.INIT
-- begin infinite i3 json stream
-- wrap it in a guard so that on spook
-- hot reload we don't output this again
unless is_initialized!
io.stdout\write '{"version":1,"click_events":true}', "\n"
io.stdout\write "[[]", "\n"
_G.INIT = true
:block, :blocklist, :named_blocks = require 'block'
write_blocks = ->
data = [ablock\to_table! for ablock in *blocklist]
io.stdout\write ",#{json.encode(data)}", "\n"
block 'IdleInhibit', ->
idle_inhibit_file = "#{os.getenv('HOME')}/.inhibit-idle"
text ' '
color normal_color
interval 5
on_update =>
if fs.is_present idle_inhibit_file
text ' '
color warning_color
else
text ' '
color normal_color
on_left_click =>
if fs.is_present idle_inhibit_file
os.remove idle_inhibit_file
else
io.open(idle_inhibit_file, "w")\close!
@update!
if api_key = os.getenv('SYNCTHING_API_KEY')
curl = "curl -H 'X-API-Key: #{api_key}' -s 'http://localhost:8384/rest/"
list_folders = curl .. "system/config' | jq -r '[.folders[] | {id: .id, path: .path}]'"
state_for = (folder) ->
folder = folder.id\gsub '%s', '%%20'
curl .. "db/status?folder=#{folder}' | jq -r '.state'"
block 'Syncthing', ->
label ' '
color normal_color
interval 5
on_update =>
folders = json.decode(command list_folders)
for folder in *folders
out = command state_for(folder)
return unless out
state = out\trim!
if state != 'idle'
color warning_color
text state
return
color normal_color
text 'idle'
on_left_click => @update!
net_throughput = require "net_throughput"
block 'Bandwidth', ->
wifi_network = ""
signal_strength = 50
iter = 9
net_if = net_throughput!
interval 1.0
on_update =>
iter += 1
status, err = net_if!
return unless status
unless net_if.up
color critical_color
text " net down"
return
if net_if.interface\match '^wlp'
if iter >= 10
iter = 0
db = command "iw wlp2s0 station dump | grep 'signal:' | awk '{print $2}'"
db = tonumber db\trim!
round = math.round
signal_strength = if db < -92
1
else if db > -21
100
else
round((-0.0154*db*db)-(0.3794*db)+98.182)
wifi_network = (command "iw wlp2s0 info | grep ssid | awk '{print $2}'")\trim!
label " #{wifi_network} #{signal_strength}% "
else
label " "
color normal_color
text " #{net_if.receive_rate}#{net_if.rx_unit} #{net_if.transmit_rate}#{net_if.tx_unit}"
max_brightness = ->
f = io.open "/sys/class/backlight/intel_backlight/max_brightness", "rb"
v = f\read "a"
f\close!
tonumber v
current_brightness = ->
f = io.open "/sys/class/backlight/intel_backlight/brightness", "rb"
v = f\read "a"
f\close!
tonumber v
brightness_percentage = -> (current_brightness! / max_brightness!) * 100
brightness_block = block 'Brightness', ->
interval 30.0
label " "
color normal_color
on_update =>
text "#{brightness_percentage!}%"
on_left_click =>
watchnr '/sys/class/backlight/intel_backlight', ->
on_changed '.*brightness', (event, dir, name) ->
brightness_block\update!
block 'Date', ->
interval 1.0
label " "
color normal_color
on_update =>
text os.date("%b-%d %H:%M:%S")
on_left_click =>
execute "gsimplecal"
block 'External IP', ->
ip = 'Unknown'
color normal_color
label " "
interval 30.0
on_update =>
out = command "dig TXT +short o-o.myaddr.l.google.com @ns1.google.com"
return unless out
ip = (out\gsub('"', ''))\trim!
text (ip != '' and ip or 'Unknown')
on_left_click =>
color warning_color
text "<copied>"
execute "echo '#{ip}' | wl-copy -n"
after 1.0, (t) ->
color normal_color
text ip
cpu_usage = require "cpu_usage"
block 'Cpu Usage', ->
cpu = cpu_usage!
text "#{format('%5.1f', 0)}%"
label ""
interval 2.0
on_update =>
cpu = cpu_usage!
if cpu < 50
color normal_color
else if cpu < 70
color warning_color
else
color critical_color
text "#{format('%5.1f', cpu)}%"
if has_executable 'acpi'
battery_acpi = require 'battery_acpi'
block 'Battery', ->
local time_remaining, alert_sent
percent = 100
status = 'charging'
batt100 = ' '
batt75 = ' '
batt50 = ' '
batt25 = ' '
batt0 = ' '
charging = ' '
interval 30.0
on_update =>
out = command 'acpi -b'
state, percent, time_remaining = battery_acpi out
return unless state
color normal_color
tr = time_remaining and " (#{time_remaining})" or ''
text "#{format('%3d', percent)}% #{tr}"
if state == 'discharging'
if percent < 10
unless alert_sent
alert_sent = true
execute "notify-desktop 'Low Battery' 'There is only #{time_remaining} of use left'"
color critical_color
label batt0
else if percent < 25
color warning_color
label batt25
else if percent < 50
color warning_color
label tt25
else if percent < 75
label batt75
else
label batt100
else if state == 'charging' or state == 'full'
alert_sent = false
color good_color
label charging
Pulse = require "pulse"
round = math.round
sinklist = Pulse\list_sinks!
pulse_blocks = {}
update_pulse_blocks = -> block\update! for block in *pulse_blocks
current_sink = ->
for sink in *sinklist
p = Pulse.new sink
return p if p.is_current
return
sinkupdater = timer 5.0, (t) ->
current_sink_list = Pulse\list_sinks!
if #current_sink_list != #sinklist
request_restart!
t\again!
for sink in *sinklist
name = if sink.name\match '^bluez'
'BT'
else
n = sink.name\split('.')
name = n[#n]
name = name\split('-')
name[1]\upper!
b = block "Volume #{name}", ->
pulse = Pulse.new sink
step = 0.02
interval 5.0
label "♪ #{name} "
text '?'
on_update =>
muted = pulse.mute
current = pulse.is_current
if muted
text "mute"
else
text "#{format('%3d', round(pulse.volume * 100))}%"
if current and muted
color warning_color
else if current
color good_color
else
color normal_color
on_left_click =>
if pulse.is_current
pulse\toggle_mute!
@update!
else
pulse\make_current!
update_pulse_blocks! -- all volume blocks including self
write_blocks! -- for the best possible feedback
on_scroll_up =>
pulse.volume = pulse.volume + step
@update!
write_blocks! -- for the best possible feedback
on_scroll_down =>
pulse.volume = pulse.volume - step
@update!
write_blocks! -- for the best possible feedback
append pulse_blocks, b
-- Let's reload this file when changing it, therefore
-- spook itself can be reconfigured without restarting it.
watch_file 'Spookfile', ->
on_changed (event) ->
request_restart!
watch "#{os.getenv('HOME')}/Sync", ->
on_deleted '(.*)/Sync/(.*)', (event, dir, name) ->
bn = fs.basename name
unless bn\match('^%..*')
execute "notify-desktop Sync \"~/Sync/#{name} DELETED\" >/dev/null"
on_changed '(.*)/Sync/(.*)', (event, dir, name) ->
bn = fs.basename name
unless bn\match('^%..*')
execute "notify-desktop Sync \"~/Sync/#{name} UPDATED\" >/dev/null"
write_blocks! unless was_reloaded!
wait_iterations = was_reloaded! and 2 or 0
json_writer = every 0.5, (t) ->
if wait_iterations > 0
wait_iterations -= 1
return
write_blocks!
restarter = every 1.0, ->
if restart_requested
restart_requested = false
is_reloaded!
reload_spook!
on_signal 'HUP', ->
current = current_sink!
if current
current\toggle_mute!
update_pulse_blocks!
write_blocks!
on_signal 'USR1', ->
current = current_sink!
if current
current.volume = current.volume + 0.02
update_pulse_blocks!
write_blocks!
on_signal 'USR2', ->
current = current_sink!
if current
current.volume = current.volume - 0.02
update_pulse_blocks!
write_blocks!
stdin_reader = on_read S.stdin, (r, fd) ->
data = fd\read!
if data
line, replaced = data\gsub '^%[', ''
line, replaced = line\gsub '^,', ''
status, event = pcall json.decode, line
if status
return unless event.name
block = named_blocks[event.name]
return unless block
switch event.button
when 1
block.left_click event
write_blocks!
when 2
block.middle_click event
write_blocks!
when 3
block.right_click event
write_blocks!
when 4
block.scroll_up event
write_blocks!
when 5
block.scroll_down event
write_blocks!
-- Just throwing in this call to better catch
-- issues stemming from GC
collectgarbage!