summaryrefslogtreecommitdiff
path: root/_usage_footer.lua
diff options
context:
space:
mode:
Diffstat (limited to '_usage_footer.lua')
-rw-r--r--_usage_footer.lua177
1 files changed, 177 insertions, 0 deletions
diff --git a/_usage_footer.lua b/_usage_footer.lua
new file mode 100644
index 0000000..2c27e8b
--- /dev/null
+++ b/_usage_footer.lua
@@ -0,0 +1,177 @@
+-- Identical private copies share one footer hook, without a companion rock.
+-- Each published section renders on its own row.
+local M = {}
+-- v3 sections include the whole row, unlike v2 which added a prefix in the hook.
+local slot = "panto.subscription_usage.v3"
+
+local function text(value)
+ return tostring(value):gsub("[%c]", " "):sub(1, 100)
+end
+
+local function reset_time(value)
+ if type(value) == "number" then
+ if value == value and value >= 0 and value <= 253402300799 then return value end
+ return nil
+ end
+ if type(value) ~= "string" then return nil end
+ local y, m, d, h, n, tail = value:match("^(%d%d%d%d)%-(%d%d)%-(%d%d)[Tt ](%d%d):(%d%d)(.*)$")
+ if not y then return nil end
+ local s, zone = tail:match("^:(%d%d)(.*)$")
+ if s then zone = zone:gsub("^%.%d+", "") else s, zone = "0", tail end
+ y, m, d, h, n, s = tonumber(y), tonumber(m), tonumber(d), tonumber(h), tonumber(n), tonumber(s)
+ local leap = y % 4 == 0 and (y % 100 ~= 0 or y % 400 == 0)
+ local days = { 31, leap and 29 or 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+ if y < 1 or m < 1 or m > 12 or d < 1 or d > days[m] or h > 23 or n > 59 or s > 59 then return nil end
+ local offset = 0
+ if zone ~= "Z" and zone ~= "z" then
+ local sign, oh, om = zone:match("^([+-])(%d%d):(%d%d)$")
+ if not sign or tonumber(oh) > 23 or tonumber(om) > 59 then return nil end
+ offset = (tonumber(oh) * 60 + tonumber(om)) * 60 * (sign == "+" and 1 or -1)
+ end
+ -- Gregorian civil date to Unix days; independent of local timezone/DST.
+ y = y - (m <= 2 and 1 or 0)
+ local era = math.floor(y / 400)
+ local year = y - era * 400
+ local day = math.floor((153 * (m + (m > 2 and -3 or 9)) + 2) / 5) + d - 1
+ local unix_days = era * 146097 + year * 365 + math.floor(year / 4) - math.floor(year / 100) + day - 719468
+ return unix_days * 86400 + h * 3600 + n * 60 + s - offset
+end
+
+local function countdown(reset, now)
+ if not reset then return "reset unknown" end
+ local seconds = reset - now
+ if seconds <= 0 then return "reset due" end
+ if seconds < 60 then return "reset in <1m" end
+ local minutes = math.floor(seconds / 60)
+ local parts = {}
+ for _, unit in ipairs({ {1440, "d"}, {60, "h"}, {1, "m"} }) do
+ local amount = math.floor(minutes / unit[1])
+ if amount > 0 then parts[#parts + 1] = amount .. unit[2] end
+ minutes = minutes % unit[1]
+ end
+ return "reset in " .. table.concat(parts, " ")
+end
+
+M.text, M.reset_time, M.countdown = text, reset_time, countdown
+
+-- Build one subscription's complete section text as a renderer. The renderer
+-- is re-evaluated each frame so countdowns and theme colors stay live without
+-- refetching. opts.status appends a status word. Invalid windows are dropped here.
+function M.section(ext, label, windows, opts)
+ opts = type(opts) == "table" and opts or { status = opts }
+ label = text(label):gsub("^%S+", string.lower)
+ local provider = (label:match("^(.-) %(") or label):lower()
+ local status = opts.status and text(opts.status) or nil
+ local snapshot = {}
+ for _, window in ipairs(windows or {}) do
+ local percent = window.used_percent
+ local valid = type(percent) == "number" and percent == percent and percent >= 0 and percent < math.huge
+ local at = reset_time(window.resets_at)
+ if valid or (percent == nil and at ~= nil) then
+ snapshot[#snapshot + 1] = {
+ label = text(window.label or "quota"), used_percent = valid and percent or nil, resets_at = at,
+ }
+ end
+ end
+ return function(now)
+ local style = type(ext.style) == "function" and ext.style() or {}
+ local base, reset = style.dim or style.footer or "\27[2m", style.reset or "\27[0m"
+ local blue = style.syntax_function or "\27[36m"
+ local accent = "\27[1m" .. blue
+ -- Use the same severity colors as the native footer context meter.
+ local green = style.tool_success or "\27[32m"
+ local yellow = style.compaction_header or "\27[33m"
+ local red = style.tool_error or "\27[31m"
+ local label_color = ({
+ claude = yellow, codex = style.canvas_fg or "\27[97m",
+ ["opencode go"] = style.thinking_header or "\27[35m",
+ })[provider] or blue
+ local function paint(color, value) return reset .. color .. value .. reset .. base end
+ local parts = {}
+ for _, window in ipairs(snapshot) do
+ local percent = window.used_percent
+ local color = percent and (percent <= 33 and green or percent <= 66 and yellow or red) or base
+ local value = percent and string.format("%.1f%%", percent):gsub("%.0%%", "%%") or "?%"
+ local meter = ""
+ if percent then
+ -- Match the native context meter's glyphs, rounding and minimum nonzero fill.
+ local filled = math.floor(math.min(percent, 100) / 20 + 0.5)
+ if percent > 0 then filled = math.max(1, filled) end
+ local fill = filled > 0 and paint(color, string.rep("━", filled)) or ""
+ meter = fill .. string.rep("─", 5 - filled) .. " "
+ end
+ parts[#parts + 1] = paint(accent, window.label) .. " " .. paint(color, value) .. " "
+ .. meter .. countdown(window.resets_at, now)
+ end
+ local detail = table.concat(parts, ", ")
+ if status then
+ detail = detail == "" and status or (detail .. " · " .. status)
+ end
+ return detail ~= "" and (paint("\27[1m" .. label_color, label) .. " usage: " .. detail .. reset) or nil
+ end
+end
+
+local function start_clock(state)
+ if state.timer then return end
+ local ok, uv = pcall(require, "luv")
+ if not ok or type(uv.new_timer) ~= "function" then return end
+ state.timer = uv.new_timer()
+ state.timer:start(60000, 60000, function()
+ if not state.closed and state.handle then state.handle:invalidate() end
+ end)
+ state.timer:unref()
+end
+
+-- Install during activate(), before render_footer; sections may arrive later.
+function M.install(ext)
+ if type(ext.on) ~= "function" then return nil end
+ local state = package.loaded[slot]
+ if not state then
+ state = { sections = {} }
+ package.loaded[slot] = state
+ ext.on("render_footer", function(event)
+ local inner = event:get_component()
+ state.handle = event:set_component({
+ render = function(_, width)
+ local lines = inner:render(width)
+ local now, keys = os.time(), {}
+ for key in pairs(state.sections) do keys[#keys + 1] = key end
+ table.sort(keys)
+ for _, key in ipairs(keys) do
+ local section = state.sections[key]
+ local line = type(section) == "function" and section(now) or section
+ if type(line) == "string" and line ~= "" then
+ lines[#lines + 1] = line
+ end
+ end
+ return lines
+ end,
+ })
+ end)
+ ext.on("session_end", function()
+ state.closed = true
+ if state.timer then state.timer:stop(); state.timer:close(); state.timer = nil end
+ end)
+ end
+ return state
+end
+
+-- Publish a section renderer (or plain string).
+-- Each account has a stable key and its own row.
+-- Also accepts (ext, key, label, windows, status, opts) for direct callers.
+function M.publish(ext, key, section, windows, status, opts)
+ local state = M.install(ext)
+ if not state or state.closed then return false end
+ if windows ~= nil or status ~= nil or opts ~= nil then
+ local section_opts = type(opts) == "table" and opts or {}
+ if status ~= nil then section_opts.status = status end
+ section = M.section(ext, section, windows, section_opts)
+ end
+ if type(section) ~= "function" and type(section) ~= "string" then return false end
+ state.sections[key] = section
+ start_clock(state)
+ if state.handle then state.handle:invalidate() end
+ return true
+end
+
+return M