From e6d98b9900923173dba5e1085e51738279b51b75 Mon Sep 17 00:00:00 2001 From: eylles Date: Fri, 28 Apr 2023 03:17:06 -0600 Subject: [PATCH 1/4] add upower backend --- battery-widget/README.md | 1 + battery-widget/battery.lua | 79 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/battery-widget/README.md b/battery-widget/README.md index b15aac67..2f4e6367 100644 --- a/battery-widget/README.md +++ b/battery-widget/README.md @@ -34,6 +34,7 @@ It is possible to customize widget by providing a table with all or some of the | `warning_msg_position` | `bottom_right` | Position of the warning popup | | `warning_msg_icon` | ~/.config/awesome/awesome-wm-widgets/battery-widget/spaceman.jpg | Icon of the warning popup | | `enable_battery_warning` | `true` | Display low battery warning | +| `battery_backend` | acpi | Backend to provide battery information, acpi or upower | *Note: the widget expects following icons to be present in the folder: diff --git a/battery-widget/battery.lua b/battery-widget/battery.lua index 4b02a7c5..04a727ff 100644 --- a/battery-widget/battery.lua +++ b/battery-widget/battery.lua @@ -31,6 +31,7 @@ local function worker(user_args) local show_current_level = args.show_current_level or false local margin_left = args.margin_left or 0 local margin_right = args.margin_right or 0 + local battery_backend = args.battery_backend or "acpi" local display_notification = args.display_notification or false local display_notification_onClick = args.display_notification_onClick or true @@ -77,6 +78,7 @@ local function worker(user_args) -- One way of creating a pop-up notification - naughty.notify local notification local function show_battery_status(batteryType) + if battery_backend == "acpi" then awful.spawn.easy_async([[bash -c 'acpi']], function(stdout, _, _, _) naughty.destroy(notification) @@ -92,6 +94,33 @@ local function worker(user_args) } end ) + elseif battery_backend == "upower" then + awful.spawn.easy_async({ awful.util.shell, "-c", + [[ for battery in $(upower -e | grep battery); do + upower -i $battery | awk ' + { if ($1 == "native-path:") {path=$2} + else if ($1 == "percentage:") {percent=$2} + else if ($1 == "state:") {state=$2} + else if ($1 == "time") {time=$4" "$5} } + END { sub("battery-","",path) } + END { printf "%s:\n %s %s %s\n", path, percent, state, time }' ; + done ]] + }, + function(stdout, _, _, _) + naughty.destroy(notification) + notification = naughty.notify{ + text = stdout, + title = "Battery status", + icon = path_to_icons .. batteryType .. ".svg", + icon_size = dpi(16), + position = position, + timeout = 5, hover_timeout = 0.5, + width = 200, + screen = mouse.screen + } + end + ) + end end -- Alternative to naughty.notify - tooltip. You can compare both and choose the preferred one @@ -120,6 +149,7 @@ local function worker(user_args) local last_battery_check = os.time() local batteryType = "battery-good-symbolic" + if battery_backend == "acpi" then watch("acpi -i", timeout, function(widget, stdout) local battery_info = {} @@ -192,6 +222,55 @@ local function worker(user_args) -- battery_popup.text = string.gsub(stdout, "\n$", "") end, icon_widget) + elseif battery_backend == "upower" then + watch( + { awful.util.shell, "-c", "upower -i $(upower -e | grep BAT) | sed -n '/present/,/icon-name/p'" }, + timeout, + function(widget, stdout) + local bat_now = { + state = "N/A", + timefull = "N/A", + percentage = "N/A", + } + + for k, v in string.gmatch(stdout, '([%a]+[%a|-]+):%s*([%a|%d]+[,|%a|%d]-)') do + if k == "state" then bat_now.state = v + elseif k == 'full' then bat_now.timefull = v + elseif k == "percentage" then bat_now.percentage = tonumber(v) + end + end + + -- customize here + local charge = bat_now.percentage + local status = bat_now.state + if show_current_level then + level_widget.text = string.format('%d%%', charge) + end + + if (charge >= 0 and charge < 15) then + batteryType = "battery-empty%s-symbolic" + if enable_battery_warning and status ~= 'charging' and os.difftime(os.time(), last_battery_check) > 300 then + -- if 5 minutes have elapsed since the last warning + last_battery_check = os.time() + + show_battery_warning() + end + elseif (charge >= 15 and charge < 40) then batteryType = "battery-caution%s-symbolic" + elseif (charge >= 40 and charge < 60) then batteryType = "battery-low%s-symbolic" + elseif (charge >= 60 and charge < 80) then batteryType = "battery-good%s-symbolic" + elseif (charge >= 80 and charge <= 100) then batteryType = "battery-full%s-symbolic" + end + + if (status == 'charging' and bat_now.timefull ~= "N/A") then + batteryType = string.format(batteryType, '-charging') + else + batteryType = string.format(batteryType, '') + end + + widget.icon:set_image(path_to_icons .. batteryType .. ".svg") + end, + icon_widget) + end if display_notification then battery_widget:connect_signal("mouse::enter", function() show_battery_status(batteryType) end) From d8398ec20508a814e0ed24e61cb459caf4d55fe4 Mon Sep 17 00:00:00 2001 From: eylles Date: Fri, 28 Apr 2023 21:52:30 -0600 Subject: [PATCH 2/4] formatting formatting: reduce indentation. --- battery-widget/battery.lua | 84 +++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/battery-widget/battery.lua b/battery-widget/battery.lua index 04a727ff..7658a45a 100644 --- a/battery-widget/battery.lua +++ b/battery-widget/battery.lua @@ -223,52 +223,52 @@ local function worker(user_args) end, icon_widget) elseif battery_backend == "upower" then - watch( - { awful.util.shell, "-c", "upower -i $(upower -e | grep BAT) | sed -n '/present/,/icon-name/p'" }, - timeout, - function(widget, stdout) - local bat_now = { - state = "N/A", - timefull = "N/A", - percentage = "N/A", - } - - for k, v in string.gmatch(stdout, '([%a]+[%a|-]+):%s*([%a|%d]+[,|%a|%d]-)') do - if k == "state" then bat_now.state = v - elseif k == 'full' then bat_now.timefull = v - elseif k == "percentage" then bat_now.percentage = tonumber(v) - end - end + watch( + { awful.util.shell, "-c", "upower -i $(upower -e | grep BAT) | sed -n '/present/,/icon-name/p'" }, + timeout, + function(widget, stdout) + local bat_now = { + state = "N/A", + timefull = "N/A", + percentage = "N/A", + } - -- customize here - local charge = bat_now.percentage - local status = bat_now.state - if show_current_level then - level_widget.text = string.format('%d%%', charge) - end + for k, v in string.gmatch(stdout, '([%a]+[%a|-]+):%s*([%a|%d]+[,|%a|%d]-)') do + if k == "state" then bat_now.state = v + elseif k == 'full' then bat_now.timefull = v + elseif k == "percentage" then bat_now.percentage = tonumber(v) + end + end - if (charge >= 0 and charge < 15) then - batteryType = "battery-empty%s-symbolic" - if enable_battery_warning and status ~= 'charging' and os.difftime(os.time(), last_battery_check) > 300 then - -- if 5 minutes have elapsed since the last warning - last_battery_check = os.time() - - show_battery_warning() - end - elseif (charge >= 15 and charge < 40) then batteryType = "battery-caution%s-symbolic" - elseif (charge >= 40 and charge < 60) then batteryType = "battery-low%s-symbolic" - elseif (charge >= 60 and charge < 80) then batteryType = "battery-good%s-symbolic" - elseif (charge >= 80 and charge <= 100) then batteryType = "battery-full%s-symbolic" - end + -- customize here + local charge = bat_now.percentage + local status = bat_now.state + if show_current_level then + level_widget.text = string.format('%d%%', charge) + end - if (status == 'charging' and bat_now.timefull ~= "N/A") then - batteryType = string.format(batteryType, '-charging') - else - batteryType = string.format(batteryType, '') - end + if (charge >= 0 and charge < 15) then + batteryType = "battery-empty%s-symbolic" + if enable_battery_warning and status ~= 'charging' and os.difftime(os.time(), last_battery_check) > 300 then + -- if 5 minutes have elapsed since the last warning + last_battery_check = os.time() - widget.icon:set_image(path_to_icons .. batteryType .. ".svg") - end, + show_battery_warning() + end + elseif (charge >= 15 and charge < 40) then batteryType = "battery-caution%s-symbolic" + elseif (charge >= 40 and charge < 60) then batteryType = "battery-low%s-symbolic" + elseif (charge >= 60 and charge < 80) then batteryType = "battery-good%s-symbolic" + elseif (charge >= 80 and charge <= 100) then batteryType = "battery-full%s-symbolic" + end + + if (status == 'charging' and bat_now.timefull ~= "N/A") then + batteryType = string.format(batteryType, '-charging') + else + batteryType = string.format(batteryType, '') + end + + widget.icon:set_image(path_to_icons .. batteryType .. ".svg") + end, icon_widget) end From 6cf9b62089c4702c02797c05804da2344bcf17bc Mon Sep 17 00:00:00 2001 From: eylles Date: Wed, 7 Jun 2023 23:10:40 -0600 Subject: [PATCH 3/4] formatting --- battery-widget/battery.lua | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/battery-widget/battery.lua b/battery-widget/battery.lua index 7658a45a..fb1f3366 100644 --- a/battery-widget/battery.lua +++ b/battery-widget/battery.lua @@ -95,8 +95,9 @@ local function worker(user_args) end ) elseif battery_backend == "upower" then - awful.spawn.easy_async({ awful.util.shell, "-c", - [[ for battery in $(upower -e | grep battery); do + awful.spawn.easy_async( + { awful.util.shell, "-c", + [[ for battery in $(upower -e | grep 'battery\|headphone'); do upower -i $battery | awk ' { if ($1 == "native-path:") {path=$2} else if ($1 == "percentage:") {percent=$2} @@ -105,20 +106,20 @@ local function worker(user_args) END { sub("battery-","",path) } END { printf "%s:\n %s %s %s\n", path, percent, state, time }' ; done ]] - }, - function(stdout, _, _, _) - naughty.destroy(notification) - notification = naughty.notify{ - text = stdout, - title = "Battery status", - icon = path_to_icons .. batteryType .. ".svg", - icon_size = dpi(16), - position = position, - timeout = 5, hover_timeout = 0.5, - width = 200, - screen = mouse.screen - } - end + }, + function(stdout, _, _, _) + naughty.destroy(notification) + notification = naughty.notify{ + text = stdout, + title = "Battery status", + icon = path_to_icons .. batteryType .. ".svg", + icon_size = dpi(16), + position = position, + timeout = 5, hover_timeout = 0.5, + width = 200, + screen = mouse.screen + } + end ) end end From bb2b552b46bcfcee3ee19ed3c2053bc9494e6d88 Mon Sep 17 00:00:00 2001 From: eylles Date: Thu, 26 Oct 2023 23:09:38 -0600 Subject: [PATCH 4/4] support more devices --- battery-widget/battery.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/battery-widget/battery.lua b/battery-widget/battery.lua index fb1f3366..f46766ed 100644 --- a/battery-widget/battery.lua +++ b/battery-widget/battery.lua @@ -97,13 +97,14 @@ local function worker(user_args) elseif battery_backend == "upower" then awful.spawn.easy_async( { awful.util.shell, "-c", - [[ for battery in $(upower -e | grep 'battery\|headphone'); do + [[ for battery in $(upower -e | grep 'battery\|headphone\|phone\|headset'); do upower -i $battery | awk ' { if ($1 == "native-path:") {path=$2} else if ($1 == "percentage:") {percent=$2} else if ($1 == "state:") {state=$2} else if ($1 == "time") {time=$4" "$5} } END { sub("battery-","",path) } + END { sub("/org/bluez/","",path) } END { printf "%s:\n %s %s %s\n", path, percent, state, time }' ; done ]] },