Created
March 11, 2013 21:34
-
-
Save pete/5137983 to your computer and use it in GitHub Desktop.
See 0README. Power management.
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
These change the background color in X (and I think they don't work if you are | |
using a desktop system, so the `xset` command ought to be changed for whatever | |
Gnome/KDE/LXDE/XFCE/ASDFJKLSEMICOLON thing you are using if you are using such a | |
thing) based on battery power remaining. The Lua one requires luaposix to be | |
installed. | |
The reason there are two versions is that Ruby was, when it ought to have been | |
sleeping, doing some select() loop. I figured Lua would not do that to me, and | |
it does not. (Porting is usually more fun than fighting the language.) | |
These are dirty scripts. The Ruby one in particular. It was written way back | |
in 2008 and occasionally patched. They are not particularly well-designed, | |
either internally or from a usability perspective. You've been warned. |
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 lua | |
colors = { | |
red = {min = 99, max = 30}, | |
green = {min = 10, max = 99}, | |
blue = {min = 10, max = 99} | |
} | |
fcache = {} | |
battery_path = io.popen('ls -d /proc/acpi/battery/BAT*/ | sed 1q'):lines()() | |
function cstr(t, frac) | |
return string.format("%d", t.min + ((t.max - t.min) * frac)) | |
end | |
function cmd(frac) | |
return "xsetroot -solid rgb:" .. | |
cstr(colors.red, frac) .. "/" .. | |
cstr(colors.green, frac) .. "/" .. | |
cstr(colors.blue, frac) | |
end | |
function pull_state() | |
local state = {} | |
for i, fname in pairs({"alarm", "info", "state"}) do | |
for line in io.lines(battery_path .. fname) do | |
local colon = string.find(line, ":") | |
local n = string.find(line, "%d") | |
if colon and n then | |
local key = string.sub(line, 1, colon -1) | |
local val = tonumber(string.match(line, "%d+")) | |
if key == "present rate" then state.rate = val | |
elseif key == "last full capacity" then state.capacity = val | |
elseif key == "remaining capacity" then state.present = val end | |
end | |
end | |
end | |
state.plugged_in = state.rate == 0 | |
state.pct = (state.present * 100) / state.capacity | |
return state | |
end | |
function set_color() | |
local state = pull_state() | |
local fraction = math.floor(state.pct) | |
if not fcache[fraction] then fcache[fraction] = cmd(state.pct / 100) end | |
os.execute(fcache[fraction]) | |
end | |
if arg[1] == "-bg" then | |
set_color() | |
elseif arg[1] == "-d" then | |
local p = require("posix") | |
local pid = p.fork() | |
if pid == 0 then | |
p.chdir("/") | |
devnull = p.open("/dev/null", p.O_RDWR) | |
p.dup2(devnull, 0) | |
p.dup2(devnull, 1) | |
p.dup2(devnull, 2) | |
pid = p.fork() | |
if pid == 0 then | |
while true do | |
set_color() | |
p.sleep(61) | |
end | |
else | |
os.exit(0) | |
end | |
else | |
os.exit(0) | |
end | |
else | |
local state = pull_state() | |
local rem = state.present / state.rate | |
local hls | |
if state.plugged_in then hls = "(plugged in)" | |
else hls = string.format("(%0.02f hours remaining)", rem) end | |
print(string.format("%6.02f%% ", state.pct) .. hls) | |
end |
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 ruby | |
Colors = { | |
:red => [99, 30], | |
:blue => [10, 99], | |
:green => [10, 99], | |
} | |
FCache = [] | |
BatteryPath = Dir['/proc/acpi/battery/BAT*/'].first | |
def ccalc(min, max, fraction) | |
min + ((max - min) * fraction) | |
end | |
def pull_state | |
bat_info = [] | |
%w(alarm info state).each { |file| | |
bat_info.concat File.readlines(BatteryPath + file) | |
} | |
bat_info.map! { |line| line.split(':').each { |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] * 100) / state[:capacity] | |
FCache[fraction] ||= begin | |
f = fraction.to_f / 100 | |
'xsetroot -solid rgb:%d/%d/%d' % [:red, :green, :blue].map! { |c| | |
ccalc(Colors[c].first, Colors[c].last, f) | |
} | |
end | |
system(FCache[fraction]) | |
end | |
case ARGV.first | |
when nil | |
state = pull_state | |
fullness = (state[:present] * 100.0) / 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 "%6.02f%% (#{hlstring})\n", fullness | |
when '-bg' | |
set_color | |
when '-d' | |
fork { | |
Dir.chdir '/' | |
fork { loop { set_color; sleep 61 } } | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment