Update Battery widget plugin (#201)
* fix: setUpdateInterval in update function * fix: add shellEscape on cat command * dump version * fix: migrate settings to desktop_widget.settings * feat: auto gap based on the layout * feat: retrieve warning threshold from noctalia's settings
This commit is contained in:
@@ -11,7 +11,7 @@ Desktop/lockscreen widget that displays the current battery level and charging s
|
||||
|
||||
## Usage
|
||||
|
||||
Add it to your desktop/lockscreen widgets.
|
||||
Add it to your desktop/lockscreen widgets using the widget editor. You can configure it in the widget settings.
|
||||
|
||||
## Settings
|
||||
|
||||
@@ -23,7 +23,7 @@ Add it to your desktop/lockscreen widgets.
|
||||
| `hide_plugged` | `bool` | `false` | Hide the battery widget when connected to AC power. |
|
||||
| `hide_full` | `bool` | `false` | Hide the battery widget when fully charged. |
|
||||
| `color` | `color` | `on_surface` | Color role for this widget's icon and label; fixed hex colors are also supported. |
|
||||
| `warning_color` | `color` | `error` | Color applied when charge is at or below 20%. |
|
||||
| `warning_color` | `color` | `error` | Color applied when charge is at or below warning threshold. |
|
||||
| `charging_color` | `color` | `on_surface` | Color applied when connected to AC power. |
|
||||
| `battery` | `select` | `auto` | Detect the battery automatically or set a custom name. |
|
||||
| `battery_name` | `string` | `BAT0` | Set a custom name for the battery. |
|
||||
@@ -32,3 +32,4 @@ Add it to your desktop/lockscreen widgets.
|
||||
## Notes
|
||||
|
||||
`hide_full` and `hide_plugged` do not hide the background due to plugin limitations.
|
||||
The warning threshold is pulled from noctalia's settings.
|
||||
|
||||
+82
-82
@@ -1,6 +1,6 @@
|
||||
id = "yocraft/battery-widget"
|
||||
name = "Battery Widget"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
plugin_api = 19
|
||||
author = "yocraft"
|
||||
license = "MIT"
|
||||
@@ -14,96 +14,96 @@ dependencies = []
|
||||
id = "widget"
|
||||
entry = "widget.luau"
|
||||
|
||||
[[setting]]
|
||||
key = "layout"
|
||||
type = "select"
|
||||
label_key = "settings.layout.label"
|
||||
description_key = "settings.layout.description"
|
||||
default = "horizontal"
|
||||
options = [
|
||||
{ value = "horizontal", label_key = "settings.layout.horizontal" },
|
||||
{ value = "vertical", label_key = "settings.layout.vertical" },
|
||||
]
|
||||
[[desktop_widget.setting]]
|
||||
key = "layout"
|
||||
type = "select"
|
||||
label_key = "settings.layout.label"
|
||||
description_key = "settings.layout.description"
|
||||
default = "horizontal"
|
||||
options = [
|
||||
{ value = "horizontal", label_key = "settings.layout.horizontal" },
|
||||
{ value = "vertical", label_key = "settings.layout.vertical" },
|
||||
]
|
||||
|
||||
[[setting]]
|
||||
key = "show_glyph"
|
||||
type = "bool"
|
||||
label_key = "settings.show_glyph.label"
|
||||
description_key = "settings.show_glyph.description"
|
||||
default = true
|
||||
[[desktop_widget.setting]]
|
||||
key = "show_glyph"
|
||||
type = "bool"
|
||||
label_key = "settings.show_glyph.label"
|
||||
description_key = "settings.show_glyph.description"
|
||||
default = true
|
||||
|
||||
[[setting]]
|
||||
key = "show_label"
|
||||
type = "bool"
|
||||
label_key = "settings.show_label.label"
|
||||
description_key = "settings.show_label.description"
|
||||
default = true
|
||||
[[desktop_widget.setting]]
|
||||
key = "show_label"
|
||||
type = "bool"
|
||||
label_key = "settings.show_label.label"
|
||||
description_key = "settings.show_label.description"
|
||||
default = true
|
||||
|
||||
[[setting]]
|
||||
key = "hide_plugged"
|
||||
type = "bool"
|
||||
label_key = "settings.hide_plugged.label"
|
||||
description_key = "settings.hide_plugged.description"
|
||||
default = false
|
||||
[[desktop_widget.setting]]
|
||||
key = "hide_plugged"
|
||||
type = "bool"
|
||||
label_key = "settings.hide_plugged.label"
|
||||
description_key = "settings.hide_plugged.description"
|
||||
default = false
|
||||
|
||||
[[setting]]
|
||||
key = "hide_full"
|
||||
type = "bool"
|
||||
label_key = "settings.hide_full.label"
|
||||
description_key = "settings.hide_full.description"
|
||||
default = false
|
||||
[[desktop_widget.setting]]
|
||||
key = "hide_full"
|
||||
type = "bool"
|
||||
label_key = "settings.hide_full.label"
|
||||
description_key = "settings.hide_full.description"
|
||||
default = false
|
||||
|
||||
|
||||
[[setting]]
|
||||
key = "color"
|
||||
type = "color"
|
||||
label_key = "settings.color.label"
|
||||
description_key = "settings.color.description"
|
||||
default = "on_surface"
|
||||
advanced = true
|
||||
[[desktop_widget.setting]]
|
||||
key = "color"
|
||||
type = "color"
|
||||
label_key = "settings.color.label"
|
||||
description_key = "settings.color.description"
|
||||
default = "on_surface"
|
||||
advanced = true
|
||||
|
||||
[[setting]]
|
||||
key = "warning_color"
|
||||
type = "color"
|
||||
label_key = "settings.warning_color.label"
|
||||
description_key = "settings.warning_color.description"
|
||||
default = "error"
|
||||
advanced = true
|
||||
[[desktop_widget.setting]]
|
||||
key = "warning_color"
|
||||
type = "color"
|
||||
label_key = "settings.warning_color.label"
|
||||
description_key = "settings.warning_color.description"
|
||||
default = "error"
|
||||
advanced = true
|
||||
|
||||
[[setting]]
|
||||
key = "charging_color"
|
||||
type = "color"
|
||||
label_key = "settings.charging_color.label"
|
||||
description_key = "settings.charging_color.description"
|
||||
default = "on_surface"
|
||||
advanced = true
|
||||
[[desktop_widget.setting]]
|
||||
key = "charging_color"
|
||||
type = "color"
|
||||
label_key = "settings.charging_color.label"
|
||||
description_key = "settings.charging_color.description"
|
||||
default = "on_surface"
|
||||
advanced = true
|
||||
|
||||
|
||||
[[setting]]
|
||||
key = "battery"
|
||||
type = "select"
|
||||
label_key = "settings.battery.label"
|
||||
description_key = "settings.battery.description"
|
||||
default = "auto"
|
||||
options = [
|
||||
{ value = "auto", label_key = "settings.battery.auto" },
|
||||
{ value = "bat0", label_key = "settings.battery.bat0" },
|
||||
{ value = "custom", label_key = "settings.battery.custom" },
|
||||
]
|
||||
[[desktop_widget.setting]]
|
||||
key = "battery"
|
||||
type = "select"
|
||||
label_key = "settings.battery.label"
|
||||
description_key = "settings.battery.description"
|
||||
default = "auto"
|
||||
options = [
|
||||
{ value = "auto", label_key = "settings.battery.auto" },
|
||||
{ value = "bat0", label_key = "settings.battery.bat0" },
|
||||
{ value = "custom", label_key = "settings.battery.custom" },
|
||||
]
|
||||
|
||||
[[setting]]
|
||||
key = "battery_name"
|
||||
type = "string"
|
||||
label_key = "settings.battery_name.label"
|
||||
description_key = "settings.battery_name.description"
|
||||
default = "BAT0"
|
||||
visible_when = { key = "battery", values = ["custom"] }
|
||||
[[desktop_widget.setting]]
|
||||
key = "battery_name"
|
||||
type = "string"
|
||||
label_key = "settings.battery_name.label"
|
||||
description_key = "settings.battery_name.description"
|
||||
default = "BAT0"
|
||||
visible_when = { key = "battery", values = ["custom"] }
|
||||
|
||||
[[setting]]
|
||||
key = "refresh_interval"
|
||||
type = "int"
|
||||
label_key = "settings.refresh_interval.label"
|
||||
description_key = "settings.refresh_interval.description"
|
||||
default = 60
|
||||
min = 1
|
||||
max = 600
|
||||
[[desktop_widget.setting]]
|
||||
key = "refresh_interval"
|
||||
type = "int"
|
||||
label_key = "settings.refresh_interval.label"
|
||||
description_key = "settings.refresh_interval.description"
|
||||
default = 60
|
||||
min = 1
|
||||
max = 600
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
"label": "Show Label"
|
||||
},
|
||||
"warning_color": {
|
||||
"description": "Color applied when charge is at or below 20%.",
|
||||
"description": "Color applied when charge is at or below noctalia's warning threshold.",
|
||||
"label": "Warning Color"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
noctalia.setUpdateInterval(noctalia.getConfig("refresh_interval") * 1000)
|
||||
|
||||
local glyph
|
||||
local color
|
||||
local percent
|
||||
local status
|
||||
|
||||
local warning_threshold = 20
|
||||
|
||||
local path = "/sys/class/power_supply/"
|
||||
local batName
|
||||
|
||||
@@ -45,8 +45,29 @@ local function getGlyph()
|
||||
end
|
||||
end
|
||||
|
||||
local function getWarningThreshold()
|
||||
noctalia.runAsync(
|
||||
"grep warning_threshold ~/.local/state/noctalia/settings.toml | awk '{print $3}'",
|
||||
function(result)
|
||||
if result.exitCode ~= 0 then
|
||||
noctalia.log("battery-widget: failed to get warning_threshold: " .. result.stderr)
|
||||
return
|
||||
end
|
||||
|
||||
local res = result.stdout
|
||||
|
||||
if not warning_threshold then
|
||||
noctalia.log("battery-widget: invalid warning_threshold: " .. res)
|
||||
return
|
||||
end
|
||||
|
||||
warning_threshold = tonumber(noctalia.string.trim(res))
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
local function getColor()
|
||||
if status ~= "Charging" and percent <= 20 then
|
||||
if status ~= "Charging" and percent <= warning_threshold then
|
||||
color = noctalia.getConfig("warning_color")
|
||||
return
|
||||
end
|
||||
@@ -72,16 +93,28 @@ local function render()
|
||||
table.insert(content, ui.label({ text = percent .. "%", color = color }))
|
||||
end
|
||||
|
||||
local layout = noctalia.getConfig("layout") == "vertical" and ui.column or ui.row
|
||||
local vertical = noctalia.getConfig("layout") == "vertical"
|
||||
|
||||
desktopWidget.render(layout({ gap = 4, align = "center" }, content))
|
||||
local layout = vertical and ui.column or ui.row
|
||||
|
||||
desktopWidget.render(
|
||||
layout({
|
||||
gap = vertical and 0 or 4,
|
||||
align = "center"
|
||||
}, content)
|
||||
)
|
||||
end
|
||||
|
||||
local function shellEscape(raw)
|
||||
return "'" .. raw:gsub("'", "'\\''") .. "'"
|
||||
end
|
||||
|
||||
local function refreshBattery()
|
||||
noctalia.runAsync(
|
||||
"cat "
|
||||
.. path .. batName .. "/capacity "
|
||||
.. path .. batName .. "/status",
|
||||
.. shellEscape(path .. batName .. "/capacity")
|
||||
.. " "
|
||||
.. shellEscape(path .. batName .. "/status"),
|
||||
function(result)
|
||||
if result.exitCode ~= 0 then
|
||||
noctalia.log("battery-widget: failed to read battery: " .. result.stderr)
|
||||
@@ -114,6 +147,9 @@ if not batName then
|
||||
return
|
||||
end
|
||||
|
||||
getWarningThreshold()
|
||||
|
||||
function update()
|
||||
noctalia.setUpdateInterval(noctalia.getConfig("refresh_interval") * 1000)
|
||||
refreshBattery()
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user