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

@@ -48,6 +48,9 @@ local config = {
local mp = require("mp")
local msg = require("mp.msg")
package.path = mp.command_native({ "expand-path", "~~/scripts/lib/?.lua;" }) .. package.path
local lib = require("utils")
local state = {
is_stream = false,
buffering = false,
@@ -69,21 +72,6 @@ local function dbg(text)
end
end
local function rgb_to_bgr(rgb)
local r = rgb:sub(1, 2)
local g = rgb:sub(3, 4)
local b = rgb:sub(5, 6)
return b .. g .. r
end
local function colorize(text, color_rgb)
if not config.use_colors then
return text
end
local bgr = rgb_to_bgr(color_rgb)
return "{\\1c&H" .. bgr .. "&}" .. text .. "{\\1c&HFFFFFF&}"
end
local function clear_osd()
mp.set_osd_ass(0, 0, "")
end
@@ -189,14 +177,14 @@ local function show_warning(warning_type, message, color)
state.last_warning[warning_type] = mp.get_time()
state.active_warning = warning_type
local col = color or config.color_warning
local styled = colorize("" .. message, col)
local styled = lib.colorize("" .. message, col, config.use_colors)
show_osd(styled, config.osd_duration)
dbg(("warning: %s"):format(message))
end
local function show_recovery()
if state.active_warning and config.show_recovery then
local styled = colorize("✓ Recovered", config.color_recovery)
local styled = lib.colorize("✓ Recovered", config.color_recovery, config.use_colors)
show_osd(styled, config.osd_duration)
dbg("recovery shown")
end
@@ -256,12 +244,13 @@ local function on_cache_buffering_state(_, pct)
local eta = predict_buffer_time(cache, target)
local eta_str = format_prediction(eta)
if eta_str then
local pred = colorize("(" .. eta_str .. " to resume)", config.color_info)
local pred =
lib.colorize("(" .. eta_str .. " to resume)", config.color_info, config.use_colors)
table.insert(parts, pred)
end
end
local text = table.concat(parts, " ")
local styled = colorize("" .. text, config.color_warning)
local styled = lib.colorize("" .. text, config.color_warning, config.use_colors)
show_osd(styled, config.osd_duration)
end
end
@@ -294,7 +283,8 @@ local function on_cache_duration(_, duration)
local eta = predict_buffer_time(duration, 0)
local eta_str = format_prediction(eta)
if eta_str then
local pred = colorize("(stall in " .. eta_str .. ")", config.color_info)
local pred =
lib.colorize("(stall in " .. eta_str .. ")", config.color_info, config.use_colors)
table.insert(parts, pred)
end
end