Created
December 26, 2009 13:43
-
-
Save badboy/263950 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby18 | |
# encoding: utf-8 | |
require 'time' | |
DZEN_CONFIG = { | |
:delimiter => "^fg(#333333)^p(5;-2)^ro(2)^p()^fg()^p(5)", | |
:ending => "^p(6)\n", | |
:interval => 3 | |
} | |
class DZEN | |
def initialize(options) | |
@interval = options.delete(:interval) || 3 | |
@delimiter = options.delete(:delimiter) || '|' | |
@ending = options.delete(:ending) || "\n" | |
@modules = [] | |
end | |
def <<(other) | |
@modules << other | |
self | |
end | |
def run | |
trap(:INT) { puts; exit 1; } | |
loop do | |
puts @modules.map{|e| e.respond_to?(:call) ? e.call : e.to_s}.join(@delimiter) + @ending | |
STDOUT.flush | |
sleep @interval | |
end | |
rescue Errno::EPIPE | |
exit 0 | |
end | |
def self.color(color, text) | |
"^fg(#{color})#{text}^fg()" | |
end | |
class Cache | |
def initialize(command, time) | |
@command = command | |
@time = time | |
@last_time = Time.now | |
@value = nil | |
end | |
def get | |
return exec if @value.nil? | |
if time_diff < @time | |
return @value | |
else | |
exec | |
end | |
end | |
def get! | |
exec | |
end | |
def reset(time = nil) | |
@time = time if time | |
@value = nil | |
end | |
def exec | |
@value = `#{@command}` | |
@last_time = Time.now | |
@value | |
end | |
def time_diff | |
(Time.now - @last_time).round | |
end | |
end | |
end | |
def color_number(n, border, options = {}) | |
options = { :normal => "#ff8700", :critical => "red" }.merge(options) | |
if n.to_i == 0 | |
"0" | |
elsif n.to_i < border | |
DZEN.color(options[:normal], n.to_i) | |
else | |
DZEN.color(options[:critical], n.to_i) | |
end | |
end | |
puts "--- loading ---#{DZEN_CONFIG[:ending]}" | |
gajim = lambda do | |
unread = `export LC_ALL=C; gajim-remote get_unread_msgs_number 2>&1`.chomp | |
if unread.match(/gajim is not running/i) | |
"gajim: -" | |
else | |
"gajim: #{color_number(unread, 11)}" | |
end | |
end | |
pidgin = lambda do | |
unread = `pidgin-unread.py 2>/dev/null`.to_i | |
if $?.success? | |
"pidgin: #{color_number(unread, 11)}" | |
else | |
"pidgin: -" | |
end | |
end | |
hdd_temp = lambda { "HDD: #{`nc localhost 7634`.split(/\|/)[-2]}°C" } | |
cpu_temp = lambda { "CPU: #{`sensors`.scan(/Core \d:\s+\+(\d+)\.\d/).flatten.map{|e|"#{e}°C"}.join("/")}" } | |
cpu_freq = lambda { IO.read("/proc/cpuinfo").scan(/^cpu MHz\s+:\s+(\d+)/).flatten.map{|e|"%.1fGhz" % (e.to_i/1000.0)}.join("/") } | |
cpu = lambda { "#{cpu_temp.call} #{cpu_freq.call}" } | |
time = lambda { Time.now.strftime("%d.%m.%Y %H:%M") } | |
loadavg = lambda { IO.read('/proc/loadavg').split(/ /)[0,3].join(' ') } | |
# check for newmails every 5 mins (5 * 60sec), else used cached result | |
MAIL_COMMAND = "newmail -q /home/badboy/.mail" | |
$mail_cache = DZEN::Cache.new(MAIL_COMMAND, 300) | |
mail = lambda do | |
mail_count = `#{MAIL_COMMAND}`.to_i | |
#mail_count = $mail_cache.get.to_i | |
if mail_count > 0 | |
$mail_cache.reset | |
"^ca(1,i3-msg 3)^i(/home/badboy/bin/mail.xbm) #{mail_count}^ca()" | |
#"^ca(1,i3-msg 3)^fg(red)^i(/home/badboy/bin/mail.xbm)^fg()^ca()" | |
else | |
"^i(/home/badboy/bin/mail.xbm) #{mail_count}" | |
end | |
end | |
battery = lambda do | |
bat = `acpi -i` | |
ac = `acpi -a`.match(/: on-line/) | |
if bat.empty? | |
if ac | |
'ac adapter' | |
else | |
'battery: -' | |
end | |
else | |
if md = bat.match(/(\d+)%/) | |
per = md[1].to_i | |
if t = bat.match(/(\d\d:\d\d):/) | |
left = " (#{t[1]})" | |
end | |
if bat.match(/Charging/) | |
charge = ' (charging)' | |
left = "" | |
end | |
if bat.match(/Full/) | |
charge = ' (full)' | |
left = "" | |
end | |
if per < 20 | |
DZEN.color('#ff8700', "battery: #{per}%#{charge}#{left}") | |
end | |
if per < 11 | |
DZEN.color(:red, "battery: #{per}%#{charge}#{left}") | |
else | |
"battery: #{per}%#{charge}" | |
end | |
else | |
DZEN.color(:red, 'battery: -') | |
end | |
end | |
end | |
dzen = DZEN.new(DZEN_CONFIG) | |
dzen << gajim << pidgin << mail << battery << hdd_temp << cpu << loadavg << time | |
dzen.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment