feat: add power gauge/delta/minmax to ac-power and extract shared utils

ac-power enhancements:
- Add visual block gauge [████░░░░] colored by power level
- Show delta indicator (+50W) colored by trend (green=dropping, red=rising)
- Track min/max values with persistence to ~/.cache/mpv/
- Multi-process safe file writes (merges values on save)
- New script-message: ac-power-reset-minmax

Shared library (scripts/lib/utils.lua):
- Extract common helpers: trim, clamp, rgb_to_bgr, colorize
- Update all scripts to use shared module via package.path
This commit is contained in:
2025-12-18 17:14:36 +01:00
parent f4064e013c
commit b890a4efcd
5 changed files with 298 additions and 45 deletions

View File

@@ -50,6 +50,9 @@ local mp = require("mp")
local msg = require("mp.msg")
local utils = require("mp.utils")
package.path = mp.command_native({ "expand-path", "~~/scripts/lib/?.lua;" }) .. package.path
local lib = require("utils")
local state = {
active = false,
zone = nil,
@@ -72,16 +75,6 @@ local function dbg(text)
end
end
local function clamp(value, min_v, max_v)
if value < min_v then
return min_v
end
if value > max_v then
return max_v
end
return value
end
local function run_cmd(args)
local res = utils.subprocess({ args = args, cancellable = false })
if not res or res.error or res.status ~= 0 then
@@ -149,7 +142,7 @@ local function current_brightness_percent()
local cur, max = out:match(",(%d+),(%d+),")
local c, m = tonumber(cur), tonumber(max)
if c and m and m > 0 then
state.cached_brightness_pct = clamp((c / m) * 100, 0, 100)
state.cached_brightness_pct = lib.clamp((c / m) * 100, 0, 100)
return state.cached_brightness_pct
end
end
@@ -163,12 +156,12 @@ local function current_brightness_percent()
if not c or not m or m <= 0 then
return nil
end
state.cached_brightness_pct = clamp((c / m) * 100, 0, 100)
state.cached_brightness_pct = lib.clamp((c / m) * 100, 0, 100)
return state.cached_brightness_pct
end
local function set_brightness_percent(pct)
local target = clamp(pct, config.min_brightness, 100)
local target = lib.clamp(pct, config.min_brightness, 100)
local ok = run_cmd(brightness_args({ "set", string.format("%.2f%%", target) }))
if ok then
state.cached_brightness_pct = target
@@ -191,7 +184,7 @@ local function current_volume_percent()
if not val then
return nil
end
state.cached_volume_pct = clamp(val * 100, 0, config.max_volume)
state.cached_volume_pct = lib.clamp(val * 100, 0, config.max_volume)
return state.cached_volume_pct
elseif backend == "pactl" then
local out = run_cmd({ config.pactl_path, "get-sink-volume", "@DEFAULT_SINK@" })
@@ -203,7 +196,7 @@ local function current_volume_percent()
if not pct then
return nil
end
state.cached_volume_pct = clamp(pct, 0, config.max_volume)
state.cached_volume_pct = lib.clamp(pct, 0, config.max_volume)
return state.cached_volume_pct
end
return nil
@@ -214,7 +207,7 @@ local function set_volume_percent(pct)
if not backend then
return
end
local target = clamp(pct, 0, config.max_volume)
local target = lib.clamp(pct, 0, config.max_volume)
if backend == "wpctl" then
run_cmd({
config.wpctl_path,