Bas Kok wrote:

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



Aah well done, I've been wanting one of those...

I've made a few changes though, there where a couple of issues.

firstly you didn't register_meter properly, the last parameter is meant to be a string that shows the template.

Secondly, when the batter is fully charged ion was freezing due to a divide by 0 error. So I added a check to see if the batter is "charged", which also updates the bar to look like [=========], since it is neither < nor >.

j


--
-- Ion statusbar extension configuration file
-- 

-- 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"]

    -- compute the percentage of battery power remaining
    local _, _, current = string.find(batt["remaining capacity"], '^(%d*)')
    local _, _, max = string.find(batt["design capacity"], '^(%d*)')
    -- local _, _, max = string.find(batt["last full capacity"], '^(%d*)')
    local percentage = 100*current/max

    -- create a bar picturing the battery state
    local bar = "["
    if charging == "charging" then
        ch=">"
    elseif charging == "charged" 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 == "charged" or rate == 0 then
        minutes = 0
    elseif charging == "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, '100% [==========] 00:00')

-- Configure format and updating
ext_statusbar.set{
    -- ISO-8601 date format
    --date_format='%Y-%m-%d %H:%M',
    -- Finnish date format
    --date_format='%d.%m.%Y %H:%M'
    -- Locale date format (usually shows seconds, which would require
    -- updating rather often and can be distracting)
    --date_format='%c',

    -- Update interval in seconds
    --update_interval=10,
    
    -- Mail checking interval
    --mail_interval=60,
    
    -- Template. Tokens %string are replaced with the value of the 
    -- corresponding meter. Currently supported meters are:
    --   %date     date
    --   %load     load average
    --   %mail_new    new mail count
    --   %mail_unread unread mail count
    --   %mail_total  total mail count
    template="[ %date || load: %load || battery: %battery ]",
}


-- Create a statusbar
ext_statusbar.create{
    -- First screen, bottom left corner
    screen=0,
    pos='bl',
}

Reply via email to