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:
@@ -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