From e5ae6746a4667e3ab91753ba46c6d37d72f56945 Mon Sep 17 00:00:00 2001 From: don philipe Date: Tue, 7 Dec 2021 17:20:44 +0100 Subject: [PATCH] Add battery widget --- battery.lua | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 battery.lua diff --git a/battery.lua b/battery.lua new file mode 100644 index 0000000..5316715 --- /dev/null +++ b/battery.lua @@ -0,0 +1,83 @@ +local gears = require("gears") +local vicious = require("vicious") +local wibox = require("wibox") + +local icon_full +local icon_full_charg +local icon_good +local icon_good_charg +local icon_medium +local icon_medium_charg +local icon_low +local icon_low_charg +local icon_empty +local icon_empty_charg + + +-- Create layout with widgets +local widget = wibox.widget { + { + id = "icon", + widget = wibox.widget.imagebox, + resize = true + }, + { + id = "text", + widget = wibox.widget.textbox, + resize = true + }, + layout = wibox.layout.align.horizontal +} +vicious.register(widget.text, vicious.widgets.bat, "$2% $3h", 61, "BAT0") + +-- Widget function that is available from the outside. +function widget.set_icons(full, full_charg, good, good_charg, medium, medium_charg, low, low_charg, empty, empty_charg) + icon_full = full + icon_full_charg = full_charg + icon_good = good + icon_good_charg = good_charg + icon_medium = medium + icon_medium_charg = medium_charg + icon_low = low + icon_low_charg = low_charg + icon_empty = empty + icon_empty_charg = empty_charg +end + +gears.timer { + timeout = 5, + call_now = true, + autostart = true, + callback = function() + state = vicious.call(vicious.widgets.bat, "$1", "BAT0") + percent_string = vicious.call(vicious.widgets.bat, "$2", "BAT0") + percent = tonumber(percent_string) + if state:match("-") then -- discharging + if percent >= 90 then + widget.icon:set_image(icon_full) + elseif percent >= 60 then + widget.icon:set_image(icon_good) + elseif percent >= 30 then + widget.icon:set_image(icon_medium) + elseif percent >= 10 then + widget.icon:set_image(icon_low) + else + widget.icon:set_image(icon_empty) + end + else -- charging + if percent >= 90 then + widget.icon:set_image(icon_full_charg) + elseif percent >= 60 then + widget.icon:set_image(icon_good_charg) + elseif percent >= 30 then + widget.icon:set_image(icon_medium_charg) + elseif percent >= 10 then + widget.icon:set_image(icon_low_charg) + else + widget.icon:set_image(icon_empty_charg) + end + end + end +} + +return widget