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
|
## 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
|
## 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_plugged` | `bool` | `false` | Hide the battery widget when connected to AC power. |
|
||||||
| `hide_full` | `bool` | `false` | Hide the battery widget when fully charged. |
|
| `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. |
|
| `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. |
|
| `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` | `select` | `auto` | Detect the battery automatically or set a custom name. |
|
||||||
| `battery_name` | `string` | `BAT0` | Set a custom name for the battery. |
|
| `battery_name` | `string` | `BAT0` | Set a custom name for the battery. |
|
||||||
@@ -32,3 +32,4 @@ Add it to your desktop/lockscreen widgets.
|
|||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
`hide_full` and `hide_plugged` do not hide the background due to plugin limitations.
|
`hide_full` and `hide_plugged` do not hide the background due to plugin limitations.
|
||||||
|
The warning threshold is pulled from noctalia's settings.
|
||||||
|
|||||||
+12
-12
@@ -1,6 +1,6 @@
|
|||||||
id = "yocraft/battery-widget"
|
id = "yocraft/battery-widget"
|
||||||
name = "Battery Widget"
|
name = "Battery Widget"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
plugin_api = 19
|
plugin_api = 19
|
||||||
author = "yocraft"
|
author = "yocraft"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
@@ -14,7 +14,7 @@ dependencies = []
|
|||||||
id = "widget"
|
id = "widget"
|
||||||
entry = "widget.luau"
|
entry = "widget.luau"
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "layout"
|
key = "layout"
|
||||||
type = "select"
|
type = "select"
|
||||||
label_key = "settings.layout.label"
|
label_key = "settings.layout.label"
|
||||||
@@ -25,28 +25,28 @@ options = [
|
|||||||
{ value = "vertical", label_key = "settings.layout.vertical" },
|
{ value = "vertical", label_key = "settings.layout.vertical" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "show_glyph"
|
key = "show_glyph"
|
||||||
type = "bool"
|
type = "bool"
|
||||||
label_key = "settings.show_glyph.label"
|
label_key = "settings.show_glyph.label"
|
||||||
description_key = "settings.show_glyph.description"
|
description_key = "settings.show_glyph.description"
|
||||||
default = true
|
default = true
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "show_label"
|
key = "show_label"
|
||||||
type = "bool"
|
type = "bool"
|
||||||
label_key = "settings.show_label.label"
|
label_key = "settings.show_label.label"
|
||||||
description_key = "settings.show_label.description"
|
description_key = "settings.show_label.description"
|
||||||
default = true
|
default = true
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "hide_plugged"
|
key = "hide_plugged"
|
||||||
type = "bool"
|
type = "bool"
|
||||||
label_key = "settings.hide_plugged.label"
|
label_key = "settings.hide_plugged.label"
|
||||||
description_key = "settings.hide_plugged.description"
|
description_key = "settings.hide_plugged.description"
|
||||||
default = false
|
default = false
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "hide_full"
|
key = "hide_full"
|
||||||
type = "bool"
|
type = "bool"
|
||||||
label_key = "settings.hide_full.label"
|
label_key = "settings.hide_full.label"
|
||||||
@@ -54,7 +54,7 @@ description_key = "settings.hide_full.description"
|
|||||||
default = false
|
default = false
|
||||||
|
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "color"
|
key = "color"
|
||||||
type = "color"
|
type = "color"
|
||||||
label_key = "settings.color.label"
|
label_key = "settings.color.label"
|
||||||
@@ -62,7 +62,7 @@ description_key = "settings.color.description"
|
|||||||
default = "on_surface"
|
default = "on_surface"
|
||||||
advanced = true
|
advanced = true
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "warning_color"
|
key = "warning_color"
|
||||||
type = "color"
|
type = "color"
|
||||||
label_key = "settings.warning_color.label"
|
label_key = "settings.warning_color.label"
|
||||||
@@ -70,7 +70,7 @@ description_key = "settings.warning_color.description"
|
|||||||
default = "error"
|
default = "error"
|
||||||
advanced = true
|
advanced = true
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "charging_color"
|
key = "charging_color"
|
||||||
type = "color"
|
type = "color"
|
||||||
label_key = "settings.charging_color.label"
|
label_key = "settings.charging_color.label"
|
||||||
@@ -79,7 +79,7 @@ default = "on_surface"
|
|||||||
advanced = true
|
advanced = true
|
||||||
|
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "battery"
|
key = "battery"
|
||||||
type = "select"
|
type = "select"
|
||||||
label_key = "settings.battery.label"
|
label_key = "settings.battery.label"
|
||||||
@@ -91,7 +91,7 @@ options = [
|
|||||||
{ value = "custom", label_key = "settings.battery.custom" },
|
{ value = "custom", label_key = "settings.battery.custom" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "battery_name"
|
key = "battery_name"
|
||||||
type = "string"
|
type = "string"
|
||||||
label_key = "settings.battery_name.label"
|
label_key = "settings.battery_name.label"
|
||||||
@@ -99,7 +99,7 @@ description_key = "settings.battery_name.description"
|
|||||||
default = "BAT0"
|
default = "BAT0"
|
||||||
visible_when = { key = "battery", values = ["custom"] }
|
visible_when = { key = "battery", values = ["custom"] }
|
||||||
|
|
||||||
[[setting]]
|
[[desktop_widget.setting]]
|
||||||
key = "refresh_interval"
|
key = "refresh_interval"
|
||||||
type = "int"
|
type = "int"
|
||||||
label_key = "settings.refresh_interval.label"
|
label_key = "settings.refresh_interval.label"
|
||||||
|
|||||||
@@ -46,7 +46,7 @@
|
|||||||
"label": "Show Label"
|
"label": "Show Label"
|
||||||
},
|
},
|
||||||
"warning_color": {
|
"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"
|
"label": "Warning Color"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
noctalia.setUpdateInterval(noctalia.getConfig("refresh_interval") * 1000)
|
|
||||||
|
|
||||||
local glyph
|
local glyph
|
||||||
local color
|
local color
|
||||||
local percent
|
local percent
|
||||||
local status
|
local status
|
||||||
|
|
||||||
|
local warning_threshold = 20
|
||||||
|
|
||||||
local path = "/sys/class/power_supply/"
|
local path = "/sys/class/power_supply/"
|
||||||
local batName
|
local batName
|
||||||
|
|
||||||
@@ -45,8 +45,29 @@ local function getGlyph()
|
|||||||
end
|
end
|
||||||
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()
|
local function getColor()
|
||||||
if status ~= "Charging" and percent <= 20 then
|
if status ~= "Charging" and percent <= warning_threshold then
|
||||||
color = noctalia.getConfig("warning_color")
|
color = noctalia.getConfig("warning_color")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -72,16 +93,28 @@ local function render()
|
|||||||
table.insert(content, ui.label({ text = percent .. "%", color = color }))
|
table.insert(content, ui.label({ text = percent .. "%", color = color }))
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
local function refreshBattery()
|
local function refreshBattery()
|
||||||
noctalia.runAsync(
|
noctalia.runAsync(
|
||||||
"cat "
|
"cat "
|
||||||
.. path .. batName .. "/capacity "
|
.. shellEscape(path .. batName .. "/capacity")
|
||||||
.. path .. batName .. "/status",
|
.. " "
|
||||||
|
.. shellEscape(path .. batName .. "/status"),
|
||||||
function(result)
|
function(result)
|
||||||
if result.exitCode ~= 0 then
|
if result.exitCode ~= 0 then
|
||||||
noctalia.log("battery-widget: failed to read battery: " .. result.stderr)
|
noctalia.log("battery-widget: failed to read battery: " .. result.stderr)
|
||||||
@@ -114,6 +147,9 @@ if not batName then
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
getWarningThreshold()
|
||||||
|
|
||||||
function update()
|
function update()
|
||||||
|
noctalia.setUpdateInterval(noctalia.getConfig("refresh_interval") * 1000)
|
||||||
refreshBattery()
|
refreshBattery()
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user