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:
parent
5f6839ca43
commit
90b57ed4b1
45
presser/context_manager/init.lua
Normal file
45
presser/context_manager/init.lua
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user