From 3a7bb799950d6f95118ae341b4c39ac705b21c3a Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Sat, 17 Dec 2016 17:58:23 +0100 Subject: multi battery widget --- config/awesome/battery.lua | 79 ++++++++++++++++++++++++++-------------------- config/awesome/rc.lua | 3 +- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/config/awesome/battery.lua b/config/awesome/battery.lua index a76ce29..100d77b 100644 --- a/config/awesome/battery.lua +++ b/config/awesome/battery.lua @@ -1,71 +1,80 @@ local naughty = require("naughty") local beautiful = require("beautiful") -function batteryInfoUpdate(adapter, warn, widget) +function batteryInfoUpdate(adapters, widget) spacer = " " - local fcap = io.open("/sys/class/power_supply/"..adapter.."/capacity") - local fsta = io.open("/sys/class/power_supply/"..adapter.."/status") - local cap = fcap:read() - local sta = fsta:read() - fcap:close() - fsta:close() - - if sta:match("Charging") then - icon = "▴" - elseif sta:match("Discharging") then - icon = "▾" - else - icon = "‐" + + local total_cur = 0.0 + local total_max = 0.0 + local status = '' + + for i = 1, #adapters do + local fcap = io.open("/sys/class/power_supply/"..adapters[i].."/capacity") + local fsta = io.open("/sys/class/power_supply/"..adapters[i].."/status") + local cap = fcap:read() + local sta = fsta:read() + fcap:close() + fsta:close() + + if sta:match("Charging") then + icon = "▴" + elseif sta:match("Discharging") then + icon = "▾" + else + icon = "‐" + end + + status = status .. cap..'%'..icon + if i < #adapters then + status = status ..',' + end + total_cur = total_cur + tonumber(cap) + total_max = total_max + 1.0 end - if tonumber(cap) < 10 and warn then + local cap = total_cur/total_max + + if cap < 10 then local bg = beautiful.bg_focus local fg = beautiful.fg_focus - if tonumber(cap) <= 5 then + if cap <= 5 then bg = beautiful.bg_urgent fg = beautiful.fg_urgent end naughty.notify({ title = "Battery Warning", - text = "Battery "..adapter.." low at "..cap.."%!", + text = "Battery low at "..cap.."%!", timeout = 15, position = "top_right", fg = fg, bg = bg }) end + local color=nil - if warn then - if tonumber(cap) < 10 then - color='red' - elseif tonumber(cap) < 20 then - color='orange' - end + if tonumber(cap) < 10 then + color='red' + elseif tonumber(cap) < 20 then + color='orange' end - local s = cap..'%'..icon if color then - s = ""..s..'' + status = ""..status..'' end - widget:set_markup(spacer..s..spacer) + widget:set_markup(spacer..status..spacer) end local wibox = require("wibox") -bat0_widget = wibox.widget.textbox() -bat0_widget:set_align("right") - -bat1_widget = wibox.widget.textbox() -bat1_widget:set_align("right") +battery_widget = wibox.widget.textbox() +battery_widget:set_align("right") -batteryInfoUpdate("BAT0", true, bat0_widget) -batteryInfoUpdate("BAT1", false, bat1_widget) +batteryInfoUpdate( {"BAT0", "BAT1"}, battery_widget) battery_timer = timer({timeout = 20}) battery_timer:connect_signal("timeout", function() - batteryInfoUpdate("BAT0", true, bat0_widget) - batteryInfoUpdate("BAT1", false, bat1_widget) + batteryInfoUpdate( {"BAT0", "BAT1"}, battery_widget) end) battery_timer:start() diff --git a/config/awesome/rc.lua b/config/awesome/rc.lua index dfbc563..f02c679 100644 --- a/config/awesome/rc.lua +++ b/config/awesome/rc.lua @@ -206,8 +206,7 @@ for s = 1, screen.count() do -- Widgets that are aligned to the right local right_layout = wibox.layout.fixed.horizontal() if s == 1 then right_layout:add(wibox.widget.systray()) end - right_layout:add(bat0_widget) - right_layout:add(bat1_widget) + right_layout:add(battery_widget) right_layout:add(volume_widget) right_layout:add(mytextclock) right_layout:add(mylayoutbox[s]) -- cgit v1.2.3