Hello ion-users,
I added a 'meter' to the statusbar of ion3 for displaying my laptop's
battery status. I'm sending it to the list, maybe it's actually useful
for others as well.
At the moment it displays:
* percentage of battery remaining
* an estimation of the remaining time until the battery is fully
(dis)charged (depending on the current state, e.g. if it's charging or
not)
* a 'graphical' bar showing the battery capacity
The output looks like this:
battery: 43% [>>>>------] 01:23
The angle brackets direction indicates if the battery is currently
charging or discharging.
The attached code could be generalized a bit more i guess, but it
works for me :) It currently only works with ACPI and not with (still
used but decrepated) APM.
greets
--
Bas Kok
5,69d4
< -- Bas Kok <[EMAIL PROTECTED]> 20041018
< -- battery information displayed in statusbar
<
< -- reads contents of a file in /proc and returns a hash containing
< -- values
< --
< local function get_proc(proc_file)
< local proc_info = {}
<
< -- parse /proc information for the first battery managed by acpi
< local f = io.open(proc_file, 'r')
< for l in f:lines() do
< local _, _, field, value = string.find(l, '^([^:]*):%s*(.*)')
< if field then
< proc_info[field] = value
< end
< end
<
< return proc_info
< end
<
< --
< -- return a string containing battery information
< --
< local function get_battery_info()
<
< -- put battery proc information in the batt hash
< local batt = get_proc('/proc/acpi/battery/BAT1/info')
< for key,value in get_proc('/proc/acpi/battery/BAT1/state') do
< batt[key] = value
< end
<
< -- determine if laptop is charging or discharging
< local charging = (batt["charging state"] == "charging")
<
< -- compute the percentage of battery power remaining
< local _, _, current = string.find(batt["remaining capacity"], '^(%d*)')
< local _, _, max = string.find(batt["design capacity"], '^(%d*)')
< local percentage = 100*current/max
<
< -- create a bar picturing the battery state
< local bar = "["
< if charging then ch=">" else ch="<" end
< for i = 1, percentage/10 do bar = bar .. ch end
< for i = percentage/10, 10 do bar = bar .. "-" end
< bar = bar .. "]"
<
< -- compute the remaining time until battery is empty
< local _, _, rate = string.find(batt["present rate"], '^(%d*)')
< local minutes = 0
< local hours = 0
< if charging then
< minutes = 60*(max-current)/rate
< else
< minutes = 60*current/rate
< end
< while minutes >= 60 do
< hours = hours + 1
< minutes = minutes - 60
< end
<
< return string.format("%d%% %s %.2d:%.2d", percentage, bar, hours, minutes)
< end
<
< ext_statusbar.register_meter('battery', get_battery_info, 'xx')
94c29
< template="[ %date || load: %load || mail: %mail_new/%mail_total || battery:
%battery ]",
---
> --template="[ %date || load: %load || mail: %mail_new/%mail_total ]",