Skip to content

Instantly share code, notes, and snippets.

@pete
Created May 7, 2009 02:05
Show Gist options
  • Save pete/107880 to your computer and use it in GitHub Desktop.
Save pete/107880 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
Colors = {
:red => [99, 30],
:blue => [10, 99],
:green => [10, 99],
}
def ccalc(min, max, fraction)
min + ((max - min) * fraction)
end
def pull_state
bat_info = []
%w(alarm info state).each { |file|
bat_info += File.readlines('/proc/acpi/battery/BAT0/' + file)
}
bat_info.map! { |line| line.split(':').map { |i| i.strip } }
pbi = Hash.new { |h,k| h[k] = bat_info.assoc(k).last }
state = {
:rate => pbi['present rate'].to_i,
:capacity => pbi['last full capacity'].to_i,
:present => pbi['remaining capacity'].to_i,
}
state[:plugged_in] = state[:rate].zero?
state
end
def set_color
state = pull_state
fraction = state[:present].to_f / state[:capacity]
#fraction = ARGV[1].to_f
colors = []
[:red, :green, :blue].each { |c|
colors << ccalc(Colors[c].first, Colors[c].last, fraction).to_i
}
colors.map! { |c| '%02d' % c }
system "xsetroot -solid rgb:#{colors.join('/')}"
end
case ARGV.first
when nil
state = pull_state
fullness = (state[:present] * 100) / state[:capacity]
hours_left = state[:present].to_f / state[:rate]
hlstring = if state[:plugged_in]
"plugged in"
else
sprintf "%0.2f hours remaining", hours_left
end
printf "%d%% (#{hlstring})\n", fullness
when '-bg'
set_color
when '-d'
fork {
Dir.chdir '/'
loop { set_color; sleep 30 }
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment