From 9bb53077d3656e4ea2cc8a607d8ca42e6449dcde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20LAJOIE?= Date: Fri, 20 Dec 2019 01:14:22 +0100 Subject: [PATCH] Rework battery_widget adding external configuration --- battery-widget/README.md | 33 ++++++++++--- battery-widget/battery.lua | 94 +++++++++++++++++++++----------------- 2 files changed, 79 insertions(+), 48 deletions(-) diff --git a/battery-widget/README.md b/battery-widget/README.md index 57f7c3f..4ec0488 100644 --- a/battery-widget/README.md +++ b/battery-widget/README.md @@ -4,17 +4,26 @@ Simple and easy-to-install widget for Awesome Window Manager. This widget consists of: - - an icon which shows the battery level: + - an icon which shows the battery level: ![Battery Widget](./bat-wid-1.png) - - a pop-up window, which shows up when you hover over an icon: - ![Battery Widget](./bat-wid-2.png) - Alternatively you can use a tooltip (check the code): + - a pop-up window, which shows up when you hover over an icon: + ![Battery Widget](./bat-wid-2.png) + Alternatively you can use a tooltip (check the code): ![Battery Widget](./bat-wid-22.png) - - a pop-up warning message which appears on bottom right corner when battery level is less that 15% (you can get the image [here](https://vk.com/images/stickers/1933/512.png)): - ![Battery Widget](./bat-wid-3.png) + - a pop-up warning message which appears on bottom right corner when battery level is less that 15% (you can get the image [here](https://vk.com/images/stickers/1933/512.png)): + ![Battery Widget](./bat-wid-3.png) Note that widget uses the Arc icon theme, so it should be [installed](https://github.com/horst3180/arc-icon-theme#installation) first under **/usr/share/icons/Arc/** folder. +## Customization + +It is possible to customize widget by providing a table with all or some of the following config parameters: + +| Name | Default | Description | +|---|---|---| +| `notification` | `false` | Display a notification on mouseover | +| `notification_position` | `top_right` | The notification position | + ## Installation This widget reads the output of acpi tool. @@ -26,4 +35,14 @@ $ acpi Battery 0: Discharging, 66%, 02:34:06 remaining ``` -Then refer to the [installation](https://github.com/streetturtle/awesome-wm-widgets#installation) section of the repo. +```lua +local battery_widget = require("awesome-wm-widgets.battery-widget.battery") + +... +s.mytasklist, -- Middle widget + { -- Right widgets + layout = wibox.layout.fixed.horizontal, + ... + battery_widget(), + ... +``` diff --git a/battery-widget/battery.lua b/battery-widget/battery.lua index aa3623d..d7c149c 100644 --- a/battery-widget/battery.lua +++ b/battery-widget/battery.lua @@ -12,6 +12,7 @@ local awful = require("awful") local naughty = require("naughty") local watch = require("awful.widget.watch") local wibox = require("wibox") +local dpi = require('beautiful').xresources.apply_dpi -- acpi sample outputs -- Battery 0: Discharging, 75%, 01:51:38 remaining @@ -20,60 +21,67 @@ local wibox = require("wibox") local PATH_TO_ICONS = "/usr/share/icons/Arc/status/symbolic/" local HOME = os.getenv("HOME") -local battery_widget = wibox.widget { - { - id = "icon", - widget = wibox.widget.imagebox, - resize = false - }, - layout = wibox.container.margin(_, 0, 0, 3) -} +local battery_widget = {} +local function worker(args) + local args = args or {} + local display_notification = args.notification or false + local position = args.notification_position or "top_right" + battery_widget = wibox.widget { + { + id = "icon", + widget = wibox.widget.imagebox, + resize = false + }, + layout = wibox.container.margin(_, 0, 0, 3) + } --- Popup with battery info --- One way of creating a pop-up notification - naughty.notify -local notification -local function show_battery_status() - awful.spawn.easy_async([[bash -c 'acpi']], + -- Popup with battery info + -- One way of creating a pop-up notification - naughty.notify + local notification + local function show_battery_status(batteryType) + awful.spawn.easy_async([[bash -c 'acpi']], 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, } end - ) -end + ) + end --- Alternative to naughty.notify - tooltip. You can compare both and choose the preferred one ---battery_popup = awful.tooltip({objects = {battery_widget}}) + -- Alternative to naughty.notify - tooltip. You can compare both and choose the preferred one + --battery_popup = awful.tooltip({objects = {battery_widget}}) --- To use colors from beautiful theme put --- following lines in rc.lua before require("battery"): --- beautiful.tooltip_fg = beautiful.fg_normal --- beautiful.tooltip_bg = beautiful.bg_normal + -- To use colors from beautiful theme put + -- following lines in rc.lua before require("battery"): + -- beautiful.tooltip_fg = beautiful.fg_normal + -- beautiful.tooltip_bg = beautiful.bg_normal -local function show_battery_warning() - naughty.notify{ - icon = HOME .. "/.config/awesome/nichosi.png", - icon_size=100, - text = "Huston, we have a problem", - title = "Battery is dying", - timeout = 5, hover_timeout = 0.5, - position = "bottom_right", - bg = "#F06060", - fg = "#EEE9EF", - width = 300, - } -end + local function show_battery_warning() + naughty.notify{ + icon = HOME .. "/.config/awesome/nichosi.png", + icon_size=100, + text = "Huston, we have a problem", + title = "Battery is dying", + timeout = 5, hover_timeout = 0.5, + position = "bottom_right", + bg = "#F06060", + fg = "#EEE9EF", + width = 300, + } + end -local last_battery_check = os.time() + local last_battery_check = os.time() + local batteryType = "battery-good-symbolic" -watch("acpi -i", 10, + watch("acpi -i", 10, function(widget, stdout, stderr, exitreason, exitcode) - local batteryType - local battery_info = {} local capacities = {} for s in stdout:gmatch("[^\r\n]+") do @@ -132,7 +140,11 @@ watch("acpi -i", 10, end, battery_widget) -battery_widget:connect_signal("mouse::enter", function() show_battery_status() end) -battery_widget:connect_signal("mouse::leave", function() naughty.destroy(notification) end) + if display_notification then + battery_widget:connect_signal("mouse::enter", function() show_battery_status(batteryType) end) + battery_widget:connect_signal("mouse::leave", function() naughty.destroy(notification) end) + end + return battery_widget +end -return battery_widget +return setmetatable(battery_widget, { __call = function(_, ...) return worker(...) end })