aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-10-03 16:16:13 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-10-03 16:16:13 -0500
commitffaadd8efcd4ac5b4e86be63c5cbb150b98d3266 (patch)
tree334c301b330b073d1459e2978eca413e54753715
parent338e6dd84046ad31f114e1d13e89dd646c35a707 (diff)
feat(nvim,wip): add flist utility
-rw-r--r--nvim/.config/nvim/lua/tobyvin/utils/fs.lua87
1 files changed, 87 insertions, 0 deletions
diff --git a/nvim/.config/nvim/lua/tobyvin/utils/fs.lua b/nvim/.config/nvim/lua/tobyvin/utils/fs.lua
index a02c351..d1f6601 100644
--- a/nvim/.config/nvim/lua/tobyvin/utils/fs.lua
+++ b/nvim/.config/nvim/lua/tobyvin/utils/fs.lua
@@ -57,4 +57,91 @@ M.xdg = setmetatable({}, {
end,
})
+function M.getflist()
+ return _G.flist
+end
+
+function M.setflist(flist, mode)
+ flist = flist or {}
+ if _G.flist == nil or mode == "a" then
+ _G.flist = vim.tbl_extend("keep", flist, {
+ title = "flist",
+ items = {},
+ })
+ else
+ vim.list_extend(_G.flist.items, flist.items or {})
+ flist.items = nil
+ _G.flist = vim.tbl_extend("force", _G.flist, flist)
+ end
+end
+
+function M.fopen()
+ error("Not implemented")
+end
+
+function M.fnext(opts)
+ opts = vim.tbl_extend("keep", opts or {}, {
+ count = 1,
+ bang = false,
+ })
+
+ if not _G.flist or #_G.flist.items == 0 then
+ error("No files")
+ elseif _G.flist.idx == #_G.flist.items then
+ error("No more items")
+ end
+
+ _G.flist.idx = _G.flist.idx and _G.flist.idx + opts.count or 1
+ local filename = _G.flist.items[_G.flist.idx].filename
+ vim.cmd.edit({ filename, bang = opts.bang })
+end
+
+function M.fprev(opts)
+ opts = vim.tbl_extend("keep", opts or {}, {
+ count = 1,
+ bang = false,
+ })
+
+ if not _G.flist or #_G.flist.items == 0 then
+ error("No files")
+ elseif _G.flist.idx == 1 then
+ error("No more items")
+ end
+
+ _G.flist.idx = _G.flist.idx and _G.flist.idx - opts.count or 1
+ local filename = _G.flist.items[_G.flist.idx].filename
+ vim.cmd.edit({ filename, bang = opts.bang })
+end
+
+function M.flist(dir)
+ local bufname = vim.api.nvim_buf_get_name(0)
+ if not dir then
+ if bufname then
+ dir = vim.fs.dirname(bufname)
+ else
+ dir = vim.uv.cwd()
+ end
+ end
+
+ local flist = {
+ title = dir,
+ items = {},
+ }
+
+ for name, type in vim.iter(vim.fs.dir(dir)) do
+ if type == "file" then
+ local filename = vim.fs.joinpath(dir, name)
+ table.insert(flist.items, {
+ filename = filename,
+ text = name,
+ })
+ if filename == bufname then
+ flist.idx = #flist.items
+ end
+ end
+ end
+
+ return flist
+end
+
return M