Added context manager module

Added a context manager for handling and managing windows/buffers
constructed by built-ins. This was handled by the `new()` function in
presser/init.lua but is now a dedicated module.
This commit is contained in:
Ethan Smith-Coss 2023-01-09 19:44:44 +00:00 committed by Gitea
parent a8473e20c1
commit 543c72a2f4

View File

@ -0,0 +1,45 @@
local a = vim.api
local g = vim.g
-- setup a new global context manager for windows/buffers
g.presser_buf_ctx = g.presser_buf_ctx or {}
local M = {}
local get_ctx_head = function ( ctx )
return g.presser_buf_ctx[ctx][1]
end
M.create = function ( ctx )
local c = g.presser_buf_ctx
-- create specific context manager if it doesn't exist
if not c[ctx] then
c[ctx] = {}
g.presser_buf_ctx = c
end
end
-- :TODO: restructure function to allow for new data structure (see todo file)
M.update = function ( ctx, data )
local c = g.presser_buf_ctx
-- ensure that the context exists
if not c[ctx] then
M.create( ctx )
end
table.insert(c[ctx], data)
g.presser_buf_ctx = c
end
M.flush = function ()
for ctx, ctx_tbl in pairs( g.presser_buf_ctx ) do
for _, win_id in pairs( ctx_tbl ) do
a.nvim_win_close( win_id, true ) -- buffer contents are irrelevant in this context to save
end
end
g.presser_buf_ctx = {} -- clear the global context manager
end
return M