summaryrefslogtreecommitdiff
path: root/dots/sketchybar/.config/sketchybar/items/widgets/battery.lua
diff options
context:
space:
mode:
authorHunt <lizhunt@amazon.com>2025-06-27 14:25:22 -0700
committerHunt <lizhunt@amazon.com>2025-06-27 14:44:54 -0700
commit0ba773cb8cb5162019d42c511f2580d601ab65d1 (patch)
tree7224f2e249df760213216a97ee73220e83a04b9f /dots/sketchybar/.config/sketchybar/items/widgets/battery.lua
parentdccdc3326cea3ad1e951438e6d38170d84d186d8 (diff)
downloaddotfiles-0ba773cb8cb5162019d42c511f2580d601ab65d1.tar.gz
dotfiles-0ba773cb8cb5162019d42c511f2580d601ab65d1.zip
Add sketchybar
Diffstat (limited to 'dots/sketchybar/.config/sketchybar/items/widgets/battery.lua')
-rw-r--r--dots/sketchybar/.config/sketchybar/items/widgets/battery.lua91
1 files changed, 91 insertions, 0 deletions
diff --git a/dots/sketchybar/.config/sketchybar/items/widgets/battery.lua b/dots/sketchybar/.config/sketchybar/items/widgets/battery.lua
new file mode 100644
index 0000000..32546f0
--- /dev/null
+++ b/dots/sketchybar/.config/sketchybar/items/widgets/battery.lua
@@ -0,0 +1,91 @@
+local constants = require("constants")
+local settings = require("config.settings")
+
+local isCharging = false
+
+local battery = sbar.add("item", constants.items.battery, {
+ position = "right",
+ update_freq = 60,
+})
+
+local batteryPopup = sbar.add("item", {
+ position = "popup." .. battery.name,
+ width = "dynamic",
+ label = {
+ padding_right = settings.dimens.padding.label,
+ padding_left = settings.dimens.padding.label,
+ },
+ icon = {
+ padding_left = 0,
+ padding_right = 0,
+ },
+})
+
+battery:subscribe({ "routine", "power_source_change", "system_woke" }, function()
+ sbar.exec("pmset -g batt", function(batteryInfo)
+ local icon = "!"
+ local label = "?"
+
+ local found, _, charge = batteryInfo:find("(%d+)%%")
+ if found then
+ charge = tonumber(charge)
+ label = charge .. "%"
+ end
+
+ local color = settings.colors.green
+ local charging, _, _ = batteryInfo:find("AC Power")
+
+ isCharging = charging
+
+ if charging then
+ icon = settings.icons.text.battery.charging
+ else
+ if found and charge > 80 then
+ icon = settings.icons.text.battery._100
+ elseif found and charge > 60 then
+ icon = settings.icons.text.battery._75
+ elseif found and charge > 40 then
+ icon = settings.icons.text.battery._50
+ elseif found and charge > 30 then
+ icon = settings.icons.text.battery._50
+ color = settings.colors.yellow
+ elseif found and charge > 20 then
+ icon = settings.icons.text.battery._25
+ color = settings.colors.orange
+ else
+ icon = settings.icons.text.battery._0
+ color = settings.colors.red
+ end
+ end
+
+ local lead = ""
+ if found and charge < 10 then
+ lead = "0"
+ end
+
+ battery:set({
+ icon = {
+ string = icon,
+ color = color
+ },
+ label = {
+ string = lead .. label,
+ padding_left = 0,
+ },
+ })
+ end)
+end)
+
+battery:subscribe("mouse.clicked", function(env)
+ local drawing = battery:query().popup.drawing
+
+ battery:set({ popup = { drawing = "toggle" } })
+
+ if drawing == "off" then
+ sbar.exec("pmset -g batt", function(batteryInfo)
+ local found, _, remaining = batteryInfo:find("(%d+:%d+) remaining")
+ local label = found and ("Time remaining: " .. remaining .. "h") or (isCharging and "Charging" or "No estimate")
+ batteryPopup:set({ label = label })
+ end)
+ end
+end)