From 90b57ed4b1a48aeb436ac09af17274138b8746d9 Mon Sep 17 00:00:00 2001 From: TheOnePath Date: Mon, 9 Jan 2023 19:44:44 +0000 Subject: [PATCH] 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. --- presser/context_manager/init.lua | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 presser/context_manager/init.lua diff --git a/presser/context_manager/init.lua b/presser/context_manager/init.lua new file mode 100644 index 0000000..ed74d37 --- /dev/null +++ b/presser/context_manager/init.lua @@ -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