Skip to content

Instantly share code, notes, and snippets.

@manveru
Created October 24, 2010 14:39
Show Gist options
  • Save manveru/643570 to your computer and use it in GitHub Desktop.
Save manveru/643570 to your computer and use it in GitHub Desktop.
# Battery sublet file
# Created with sur-0.1
configure :battery do |s|
s.interval = 60
s.full = 0
# Path
s.now = []
s.status = []
# Icons
s.icons = {
:ac => Subtlext::Icon.new("ac.xbm"),
:full => Subtlext::Icon.new("bat_full_02.xbm"),
:low => Subtlext::Icon.new("bat_low_02.xbm"),
:empty => Subtlext::Icon.new("bat_empty_02.xbm")
}
# Find battery slot and capacity
begin
Dir.glob "/sys/class/power_supply/CMB*/" do |path|
case File.basename(Dir["#{path}/{charge_full_design,energy_full_design}"].first)
when 'charge_full_design'
full, now = 'charge_full', 'charge_now'
when 'energy_now'
full, now = 'energy_full_design', 'energy_now'
else
raise "Unrecognized power supply: #{path}"
end
s.now << File.join(path, now)
s.status << File.join(path, 'status')
s.full += File.read(File.join(path, full)).to_i
end
rescue => err
puts err, err.backtrace
File.open('/home/manveru/tmp/subtle_error.log', 'a+'){|io| io.puts(err.message, *err.backtrace) }
end
end
on :run do |s|
begin
now = s.now.inject(0){|s,v| s + File.read(v).to_i }
state = File.read(s.status.first).strip
percent = (now * 100 / s.full).to_i
# Select icon
icon = case state
when "Charging" then :ac
when "Discharging"
case percent
when 67..100 then :full
when 34..66 then :low
when 0..33 then :empty
end
when "Full" then :ac
end
s.data = "%d%%%s" % [ percent, s.icons[icon] ]
rescue => err # Sanitize to prevent unloading
s.data = "subtle"
File.open('/home/manveru/tmp/subtle_error.log', 'a+'){|io| io.puts(err.message, *err.backtrace) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment