-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdorfehs-bar.rb
913 lines (746 loc) · 22.3 KB
/
sdorfehs-bar.rb
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
#!/usr/bin/env ruby
#
# Copyright (c) 2009-2019 joshua stein <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require "date"
require "net/https"
require "uri"
require "json"
require "ffi"
config = {
# seconds to blink on and off during 1 second
:blink => [ 0.85, 0.15 ],
:colors => {
:symbol => "#bbbbbb",
:disabled => "#9d9d9d",
:ok => "#87de99",
:warn => "orange",
:alert => "#cb4b16",
:emerg => "#b5401e",
},
# cryptocurrencies to watch, as a hash of each symbol to a hash containing
# the :qty and :cost of amounts held
:cryptocurrencies => {},
# where to put the FIFO for controlling ourself
:fifo_path => "#{ENV["HOME"]}/.config/sdorfehs/bar-control",
# the size between module output, in points of the font
:sep_size => 25,
# stocks to watch, as a hash of each symbol to a hash containing the :qty and
# :cost of amounts held
# requires an API key from https://finnhub.io/register stored as
# config[:stocks_api_key]
:stocks => {},
# minimum temperature (f) at which sensors will be shown
:temp_min => 160,
# darksky.net api key used to fetch weather
:weather_api_key => "",
# lat and long for weather joined by a comma; if blank, an api will be used
# to get the lat/long based on ip address
:weather_lat_long => "",
# which modules are enabled, and in which order
:module_order => [ :weather, :cryptocurrencies, :stocks, :vscreen, :keepalive,
:audio, :network, :thermals, :power, :date, :time ],
}
# override defaults by eval'ing ~/.config/sdorfehs/bar-config.rb
if File.exist?(f = "#{ENV["HOME"]}/.config/sdorfehs/bar-config.rb")
eval(File.read(f))
end
class NilClass
def any?
false
end
end
class String
def any?
!empty?
end
end
# FFI interface to XResetScreenSaver
module X11
extend FFI::Library
ffi_lib "libX11"
attach_function :XOpenDisplay, [ :string ], :pointer
end
module Xss
extend FFI::Library
typedef :ulong, :XID
class XSyncValue < FFI::Struct
layout :hi, :int32,
:lo, :uint32
end
class XSyncSystemCounter < FFI::Struct
layout :name, :string,
:counter, :XID,
:resolution, XSyncValue
end
ffi_lib "libXss"
attach_function :XSyncQueryExtension, [ :pointer, :pointer, :pointer ], :int
attach_function :XSyncInitialize, [ :pointer, :pointer, :pointer ], :int
attach_function :XSyncListSystemCounters, [ :pointer, :pointer ], :pointer
attach_function :XSyncFreeSystemCounterList, [ :pointer ], :void
attach_function :XSyncQueryCounter, [ :pointer, :XID, :pointer ], :int
attach_function :XResetScreenSaver, [ :pointer ], :int
end
class Controller
attr_reader :config
attr_writer :dying
MODULES = {
:audio => {
:i3status => :volume,
},
:cryptocurrencies => {
:frequency => 60 * 5,
:error_frequency => 30,
},
:date => {
:frequency => 1,
},
:keepalive => {
:frequency => 30,
},
:network => {
:i3status => [ :ethernet, :wireless ],
},
:power => {
:i3status => :battery,
},
:stocks => {
:frequency => 60 * 5,
:error_frequency => 30,
},
:thermals => {
:i3status => :cpu_temperature,
},
:time => {
:frequency => 1,
},
:weather => {
:frequency => 60 * 10,
:error_frequency => 30,
},
:vscreen => {
:frequency => 60,
},
}
def initialize(config)
@config = config
@data = {}
@threads = {}
@bar = nil
@output = ""
@i3status = nil
@i3status_data = {}
@dying = false
@refresh = true
@restart = false
@mutex = Mutex.new
if File.exist?(@config[:fifo_path])
File.unlink(@config[:fifo_path])
end
File.mkfifo(@config[:fifo_path])
@fifo = File.open(@config[:fifo_path], "r+")
end
def color(col)
config[:colors][col]
end
def clense(str)
# escape control chars
str.to_s.gsub(/\^/, "\\^").gsub("\n", " ").gsub("\r", "")
end
def run!
if !File.exist?("/usr/local/bin/i3status")
STDERR.puts "i3status not found"
exit 1
end
# try to take i3status down with us
[ "QUIT", "TERM", "INT" ].each do |sig|
Kernel.trap(sig) do
@dying = true
end
end
# signal to reload config and restart i3status
Kernel.trap("HUP") do
@restart = true
@dying = true
end
Thread.abort_on_exception = true
# bring up a writer to sdorfehs bar
@bar = File.open("#{ENV["HOME"]}/.config/sdorfehs/bar", "w+")
# find sdorfehs pid, so we can see when it exits
@sdorfehs = ENV["SDORFEHS_PID"].to_i
if @sdorfehs == 0
@sdorfehs = `pgrep sdorfehs`.strip.to_i
end
if @sdorfehs == 0
puts "can't find sdorfehs pid"
exit 1
end
# read action commands
@threads[:fifo] = Thread.new do
while [email protected]?
line = @fifo.gets
case line.strip
when "quit"
@dying = true
when "restart"
@restart = true
@dying = true
when "keepalive"
MODULES[:keepalive][:enabled] = true
@threads[:keepalive].wakeup
when "nokeepalive"
MODULES[:keepalive][:enabled] = false
@threads[:keepalive].wakeup
when /^vscreen=(\d+)/
MODULES[:vscreen][:cur] = $1.to_i
update_data(:vscreen, vscreen, nil)
else
STDERR.puts "unknown fifo command: #{line.inspect}"
end
end
end
# send data to sdorfehs and handle blinking
@threads[:sdorfehs] = Thread.new do
while @bar
break if @dying
# make sure sdorfehs is still up
begin
Process.kill(0, @sdorfehs)
rescue Errno::ESRCH
cleanup_and_exit("sdorfehs (#{@sdorfehs}) is not running")
break
end
output = @output.dup
if output.match(/\^blink\(/)
output, dark = unblink(output)
# flash output, darken it for a brief moment, then show it again
@bar.puts output
@bar.flush
sleep config[:blink].first
@bar.puts dark
@bar.flush
sleep config[:blink].last
@bar.puts output
@bar.flush
else
@bar.puts output
@bar.flush
end
Thread.stop
end
cleanup_and_exit("sdorfehs bar interface went away")
end
@i3status = IO.popen("/usr/local/bin/i3status", "r+")
# cache new data, then wakeup any threads that are sleeping waiting for
# that data
@threads[:i3status] = Thread.new do
while @i3status && [email protected]?
break if @dying
# [{"name":"wireless","instance":"iwn0","full_text":"up|166 dBm"},...
next if !(m = @i3status.gets.to_s.match(/^,?(\[\{.*)/))
new_data = {}
JSON.parse(m[1]).each do |mod|
new_data[mod["name"].to_sym] = mod
end
mod_updates = []
(new_data.keys + @i3status_data.keys).uniq.each do |k,v|
if @i3status_data[k] != new_data[k]
# find any modules that are listening for this i3status data
MODULES.each do |mod,modp|
next if !modp[:i3status]
if modp[:i3status].is_a?(Array)
if modp[:i3status].include?(k)
mod_updates.push mod
end
elsif modp[:i3status] == k
mod_updates.push mod
end
end
end
end
@i3status_data = new_data
mod_updates.uniq.each do |mod|
if @threads[mod]
@threads[mod].wakeup
end
end
if @restart_i3status
if @i3status
Process.kill(9, @i3status.pid)
Process.waitpid(@i3status.pid)
end
@restart_i3status = false
@i3status = IO.popen("/usr/local/bin/i3status", "r+")
end
end
cleanup_and_exit("i3status died")
end
if config[:module_order].include?(:keepalive)
@threads[:keepalive_pinger] = Thread.new do
sync_event = FFI::MemoryPointer.new(:int32, 1)
error = FFI::MemoryPointer.new(:int32, 1)
ncounters = FFI::MemoryPointer.new(:int32, 1)
dpy = X11.XOpenDisplay(nil)
Xss.XSyncQueryExtension(dpy, sync_event, error).inspect
Xss.XSyncInitialize(dpy, sync_event, error)
counters = Xss.XSyncListSystemCounters(dpy, ncounters)
idler_counter = 0
(0 ... ncounters.read_int).each do |x|
c = Xss::XSyncSystemCounter.new(counters +
(x * Xss::XSyncSystemCounter.size))
if c[:name] == "IDLETIME"
idler_counter = c[:counter]
end
end
Xss.XSyncFreeSystemCounterList(counters)
if idler_counter == 0
STDERR.puts "keepalive: couldn't find IDLETIME counter"
exit 1
end
value = Xss::XSyncValue.new
while !@dying do
Thread.stop
if MODULES[:keepalive][:enabled]
# not sure why the counter has to be read before XResetScreenSaver
# but it does, otherwise XResetScreenSaver does nothing
Xss.XSyncQueryCounter(dpy, idler_counter, value)
Xss.XResetScreenSaver(dpy)
end
end
end
end
# spin up threads for each module
config[:module_order].each do |mod|
@threads[mod] = Thread.new do
while !@dying
error = false
begin
ret = self.send(mod)
rescue Timeout::Error, StandardError => e
STDERR.puts "error updating #{mod}: #{e}"
STDERR.puts e.backtrace.join("\n")
error = true
end
update_data(mod, ret, error)
if error && MODULES[mod][:error_frequency]
sleep MODULES[mod][:error_frequency]
elsif MODULES[mod][:frequency]
sleep MODULES[mod][:frequency]
else
Thread.stop
end
end
end
end
# hang around as long as everything is running
@threads.each do |k,v|
v.join
end
if @restart
cleanup_and_exit("restarting")
else
cleanup_and_exit("threads died")
end
end
# kill i3status when we die
def cleanup_and_exit(why)
if @bar
begin
@bar.puts "sdorfehs-bar exiting: #{why}"
@bar.flush
rescue
end
File.close(@bar)
end
if @i3status
Process.kill(9, @i3status.pid)
Process.waitpid
end
rescue
ensure
if @restart
exec("ruby", $0)
end
exit
end
def update_data(mod, ret, error = false)
@mutex.synchronize do
if @data[mod] == ret && !error
return
end
old_data = @data[mod].dup
if error
# try to show the last data for this module, it's better than nothing
if old_data.to_s == ""
@data[mod] = mod.to_s
end
@data[mod] << " ^fg(#{color(:alert)})!^fg()"
end
@data[mod] = ret
@output = config[:module_order].map{|m| @data[m] }.reject{|d| !d }.
join(sep)
if error
@data[mod] = old_data
end
end
@threads[:sdorfehs].wakeup
end
# find ^blink() strings and return a stripped out version and a dark version
# (a regular gsub won't work because we have to track parens)
def unblink(str)
new_str = ""
dark_str = ""
chunk = ""
x = 0
while x < str.length
chunk << str[x .. x]
x += 1
if m = chunk.match(/^(.*)\^blink\($/)
new_str << m[1]
dark_str << m[1] << "^fg(#{color(:disabled)})"
# keep eating characters until we see the closing )
opens = 0
while x < str.length
chr = str[x .. x]
x += 1
if chr == "("
opens += 1
elsif chr == ")"
if opens == 0
break
else
opens -= 1
end
end
new_str << chr
dark_str << chr
end
dark_str << "^fg()"
chunk = ""
end
end
if chunk != ""
new_str << chunk
dark_str << chunk
end
return [ new_str, dark_str ]
end
# separator bar
def sep
"^fn(courier new:size=#{config[:sep_size]}) ^fn(#{config[:font]})"
end
# data-collection routines
# audio volume, or mute
def audio
return nil if !@i3status_data[:volume]
o = "^fg()"
if @i3status_data[:volume]["full_text"].match(/mute/)
o << "^fg(#{color(:disabled)})"
end
o << "^ca(" <<
"1,sh -c '~/bin/volume mute'," <<
"4,sh -c '~/bin/volume up'," <<
"5,sh -c '~/bin/volume down')"
o << "^fg(#{color(:disabled)})vol/"
if @i3status_data[:volume]["full_text"].match(/mute/)
o << "---"
else
vol = @i3status_data[:volume]["full_text"].gsub(/[^0-9]/, "")
o << "^fg()#{vol}^fg(#{color(:disabled)})%"
end
o << "^ca()"
o << "^fg()"
end
# bluetooth interface status
def bluetooth
up = false
present = false
b = IO.popen("/usr/local/sbin/btconfig")
b.readlines.each do |sc|
if sc.match(/ubt\d/)
present = true
if sc.match(/UP/)
up = true
end
end
end
b.close
present ? "^fg(#{color(up ? :ok : :disabled)})bt^fg()" : nil
end
# prices of watched cryptocurrencies
def cryptocurrencies
return nil if !config[:cryptocurrencies].any?
sd = Net::HTTP.get(URI.parse("https://min-api.cryptocompare.com/" +
"data/pricemulti?fsyms=#{config[:cryptocurrencies].keys.join(",")}" +
"&tsyms=USD"))
js = JSON.parse(sd)
# => {"ETH"=>{"USD"=>916.69}, "BTC"=>{"USD"=>14904.82}}
out = []
js.each do |cur,usd|
c = config[:cryptocurrencies][cur.upcase.to_sym]
if !c
next
end
label = case cur.downcase
when "btc"
"^fn(courier new:size=13)\u{0243}^fn()"
when "eth"
"^fn(courier new:size=13)\u{039E}^fn()"
else
"#{c[:label] ? c[:label] : cur.downcase}"
end
out.push price(label, usd["USD"], nil, c[:qty], c[:cost])
end
out.join(" ")
end
def date
Time.now.strftime("%a %b %-d").downcase
end
def keepalive
if MODULES[:keepalive][:enabled]
@threads[:keepalive_pinger].wakeup
end
# brightness emoji
"^ca(1,sh -c 'echo #{MODULES[:keepalive][:enabled] ? "no" : ""}" <<
"keepalive > #{config[:fifo_path]}')" <<
"^fn(noto emoji)^fg(" <<
"#{MODULES[:keepalive][:enabled] ? "" : color(:disabled)})" <<
"\u{1F506}^fg()^fn(#{config[:font]})" <<
"^ca()"
end
# wireless interface state and signal quality, ethernet interface status
def network
wifi_up = false
wifi_signal = 0
if @i3status_data[:ethernet] &&
@i3status_data[:ethernet]["full_text"].to_s.match(/up/)
return "^fg()eth"
end
if @i3status_data[:wireless] &&
(m = @i3status_data[:wireless]["full_text"].to_s.match(/^up\|(.+)$/))
wifi_up = true
if m[1] == "?"
wifi_connected = false
else
wifi_connected = true
if n = m[1].match(/(\d+)%/) # old
wifi_signal = n[1].to_i
elsif n = m[1].match(/(-?\d+) dBm/)
wifi_signal = [ 2 * (n[1].to_i + 100), 100 ].min
end
end
else
return nil
end
wi = ""
# clicking on wifi will bounce interface
iface = @i3status_data[:wireless]["instance"]
wi << "^ca(1,sh -c 'doas ifconfig #{iface} down; " <<
"doas ifconfig #{iface} up')"
if wifi_connected && wifi_signal > 0
if wifi_signal >= 60
wi << "^fg()"
elsif wifi_signal >= 45
wi << "^fg(#{color(:alert)})"
else
wi << "^fg(#{color(:warn)})"
end
wi << "wifi^fg()"
elsif wifi_connected
wi << "^fg()wifi"
elsif wifi_up
wi << "^fg(#{color(:disabled)})wifi^fg()"
end
wi << "^ca()"
wi
end
# ac status, then each battery's percentage of power left
def power
return nil if !@i3status_data[:battery]
batt_max = batt_left = batt_perc = {}, {}, {}
ac_on = false
run_rate = 0.0
@i3status_data[:battery]["full_text"].split("|").each_with_index do |d,x|
case x
when 0
ac_on = (d == "CHR")
when 1
batt_perc = { 0 => d.to_i }
when 2
run_rate = d.to_f
end
end
out = "^fg(#{ac_on ? "" : color(:disabled)})ac"
total_perc = batt_perc.values.inject{|a,b| a + b }
if !ac_on || total_perc < 50
batt_perc.keys.each do |i|
out << "^fg(#{color(:disabled)})/"
blink = false
if batt_perc[i] <= 10.0
out << "^fg(#{color(:emerg)})"
if total_perc < 10.0 && !ac_on
blink = true
end
elsif batt_perc[i] < 30.0
out << "^fg(#{color(:alert)})"
else
out << "^fg()"
end
out << (blink ? "^blink(" : "") + batt_perc[i].to_s +
(blink ? ")" : "") + "^fg(#{color(:disabled)})%^fg()"
end
end
if !batt_perc.any?
out << "^fg(#{color(:disabled)})/?^fg()"
end
if run_rate > 0.0 && !ac_on
out << "^fg(#{color(:disabled)})/^fg()"
if run_rate >= 20.0
out << "^fg(#{color(:emerg)})"
elsif run_rate >= 10.0
out << "^fg(#{color(:alert)})"
end
out << "#{sprintf("%0.1f", run_rate)}^fg(#{color(:disabled)})w^fg()"
end
out
end
def price(symbol, quote, quote_color, qty, cost)
t = "#{symbol}^fg(#{color(:disabled)})/^fg(#{quote_color})"
if quote > 10
t << "$#{quote.floor}"
else
t << "$#{sprintf("%0.2f", quote)}"
end
if qty
quote = quote * qty.to_f
if cost
change = quote - cost
change_color = ""
if change == 0.0
change_color = ""
change = "=$#{change.floor}"
elsif change > 0.0
change_color = color(:ok)
change = "+$#{change.floor}"
elsif change < 0.0
change_color = color(:emerg)
change = "-$#{change.abs.ceil}"
end
t << " ^fg(#{change_color})#{change}^fg()"
else
t << " =$#{quote.floor}"
end
end
t
end
def stocks
return nil if !config[:stocks].any?
return nil if !config[:stocks_api_key].any?
out = []
config[:stocks].each do |ticker,c|
sd = Net::HTTP.get(URI.parse("https://finnhub.io/api/v1/quote?symbol=" +
"#{ticker}&token=#{config[:stocks_api_key]}"))
js = JSON.parse(sd)
# => {"c"=>261.74, "h"=>263.31, "l"=>260.68, "o"=>261.07, "pc"=>259.45}
quote_color = ""
if js["c"] > js["o"]
quote_color = color(:ok)
elsif js["c"] < js["o"]
quote_color = color(:emerg)
end
out.push price(ticker, js["c"], quote_color, c[:qty], c[:cost])
end
out.join(" ")
end
# any temperature sensors that are too hot
def thermals
return nil if !@i3status_data[:cpu_temperature]
temps = [
@i3status_data[:cpu_temperature]["full_text"].to_f
]
m = 0.0
temps.each{|t| m += t }
fh = (9.0 / 5.0) * (m / temps.length.to_f) + 32.0
if fh > config[:temp_min]
"^fg(#{color(:alert)})^blink(#{fh.to_i})^fg(#{color(:disabled)})f^fg()"
else
nil
end
end
def time
t = Time.now
"^fg()" << t.strftime("%H") << "^fg(#{color(:disabled)}):^fg()" <<
t.strftime("%M")
end
def vscreen
cur = MODULES[:vscreen][:cur].to_i
down = (cur == 0 ? cur : cur - 1)
up = (cur == 10 ? cur : cur + 1)
"^ca(" <<
"4,sdorfehs -c 'vselect #{up}'," <<
"5,sdorfehs -c 'vselect #{down}')" <<
"^fg(#{color(:disabled)})vscr/^fg()#{cur}^ca()"
end
# current temperature/humidity
def weather
return nil if !config[:weather_api_key].any?
if !config[:weather_lat_long].any?
js = JSON.parse(Net::HTTP.get(URI.parse("http://ip-api.com/json")))
if js["lat"] && js["lon"]
config[:weather_lat_long] = "#{js["lat"]},#{js["lon"]}"
end
end
return nil if !config[:weather_lat_long].any?
js = JSON.parse(Net::HTTP.get(URI.parse(
"https://api.openweathermap.org/data/2.5/weather?lat=" +
config[:weather_lat_long].split(",")[0] + "&lon=" +
config[:weather_lat_long].split(",")[1] + "&units=imperial&appid=" +
config[:weather_api_key])))
if js["cod"].to_i != 200
STDERR.puts "error updating weather: #{js.inspect}"
# don't return an error as we'll end up retrying on :error_frequency,
# which could be often. if this error is due to exceeding a limit, it'll
# just make the problem worse
return "^fg(#{color(:error)})error^fg()"
end
w = js["weather"][0]["description"].downcase.
gsub(/^scattered /, "").
gsub(/^overcast clouds/, "overcast").
gsub(/^broken clouds/, "cloudy").
gsub(/^overcast clouds/, "overcast").
gsub(/intensity /, "")
# add current temperature
w << " ^fg()" << js["main"]["temp"].to_i.to_s <<
"^fg(#{color(:disabled)})f^fg()"
# add current humidity
humidity = js["main"]["humidity"].to_i
w << "^fg(#{color(:disabled)})/^fg(" <<
(humidity > 60 ? color(:alert) : "") <<
")" << humidity.to_i.to_s << "^fg(#{color(:disabled)})%^fg()"
w
end
end
# on with the show
Controller.new(config).run!