repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
neovim/lua/bugabinga/pivi/publish.lua
Rawlocal status = require 'bugabinga.pivi.status'
local VARIABLE = 'pivi'
local EVENT = 'User'
local STATUS_PATTERN = 'PiviStatus'
local ACTIVITY_PATTERN = 'PiviActivity'
local read = function()
local value = vim.g[VARIABLE]
if type( value ) ~= 'table' then return { active = nil, sessions = {}, } end
value.sessions = value.sessions or {}
return value
end
local write = function( value ) vim.g[VARIABLE] = value end
local emit = function( pattern, data )
pcall( vim.api.nvim_exec_autocmds, EVENT, { pattern = pattern, data = data, modeline = false, } )
end
--- Caches one session's semantic status and announces the change.
--- Consumers read the cached variable; they never perform RPC.
local session_status = function( name, state )
vim.validate( 'name', name, 'string' )
vim.validate( 'state', state, 'table' )
local value = read()
value.sessions[name] = {
name = name,
connection = state.connection,
run = state.run,
tool = state.tool,
queue = state.queue,
compacting = state.compacting,
retry = state.retry,
error = state.error,
model = state.model,
thinking = state.thinking,
session = state.session,
context = state.context,
summary = status.summary( state ),
}
value.active = value.active or name
write( value )
emit( STATUS_PATTERN, { session = name, status = value.sessions[name], } )
end
local set_active = function( name )
local value = read()
value.active = name
write( value )
emit( STATUS_PATTERN, { session = name, status = value.sessions[name], } )
end
local remove = function( name )
local value = read()
value.sessions[name] = nil
if value.active == name then
value.active = next( value.sessions )
end
write( value )
emit( STATUS_PATTERN, { session = name, status = nil, } )
end
local activity = function( payload )
emit( ACTIVITY_PATTERN, payload )
end
--- Cached statusline text. Safe when pivi never started.
local summary = function( name )
local value = read()
local key = name or value.active
if not key then return '' end
local entry = value.sessions[key]
if not entry then return '' end
return entry.summary or ''
end
local reset = function()
write( { active = nil, sessions = {}, } )
end
return {
variable = VARIABLE,
status_pattern = STATUS_PATTERN,
activity_pattern = ACTIVITY_PATTERN,
read = read,
session_status = session_status,
set_active = set_active,
remove = remove,
activity = activity,
summary = summary,
reset = reset,
}