refactor(utils): directory formatting
parent
1d348a3df6
commit
e2ae8b321e
|
|
@ -12,12 +12,18 @@ function M.fire(event)
|
||||||
vim.api.nvim_exec_autocmds("User", { pattern = "Persisted" .. event })
|
vim.api.nvim_exec_autocmds("User", { pattern = "Persisted" .. event })
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the current session for the cwd and git branch
|
---Get the current working directory
|
||||||
|
---@return string
|
||||||
|
function M.cwd()
|
||||||
|
return utils.sanitize_dir(vim.fn.getcwd())
|
||||||
|
end
|
||||||
|
|
||||||
|
---Get the current session for the current working directory and git branch
|
||||||
---@param opts? {branch?: boolean}
|
---@param opts? {branch?: boolean}
|
||||||
---@return string
|
---@return string
|
||||||
function M.current(opts)
|
function M.current(opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
local name = vim.fn.getcwd():gsub("[\\/:]+", "%%")
|
local name = M.cwd()
|
||||||
|
|
||||||
if config.use_git_branch and opts.branch ~= false then
|
if config.use_git_branch and opts.branch ~= false then
|
||||||
local branch = M.branch()
|
local branch = M.branch()
|
||||||
|
|
@ -120,12 +126,10 @@ function M.delete(opts)
|
||||||
|
|
||||||
if session and uv.fs_stat(session) ~= 0 then
|
if session and uv.fs_stat(session) ~= 0 then
|
||||||
M.fire("DeletePre")
|
M.fire("DeletePre")
|
||||||
|
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
M.stop()
|
M.stop()
|
||||||
vim.fn.system("rm " .. e(session))
|
vim.fn.system("rm " .. e(session))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
M.fire("DeletePost")
|
M.fire("DeletePost")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -142,15 +146,12 @@ end
|
||||||
---Determines whether to load, start or stop a session
|
---Determines whether to load, start or stop a session
|
||||||
function M.toggle()
|
function M.toggle()
|
||||||
M.fire("Toggle")
|
M.fire("Toggle")
|
||||||
|
|
||||||
if vim.g.persisting == nil then
|
if vim.g.persisting == nil then
|
||||||
return M.load()
|
return M.load()
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.g.persisting then
|
if vim.g.persisting then
|
||||||
return M.stop()
|
return M.stop()
|
||||||
end
|
end
|
||||||
|
|
||||||
return M.start()
|
return M.start()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -163,7 +164,7 @@ function M.allowed_dir(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
local dir = opts.dir or vim.fn.getcwd()
|
local dir = opts.dir or M.cwd()
|
||||||
|
|
||||||
return utils.dirs_match(dir, config.allowed_dirs) and not utils.dirs_match(dir, config.ignored_dirs)
|
return utils.dirs_match(dir, config.allowed_dirs) and not utils.dirs_match(dir, config.ignored_dirs)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
--- Escape special pattern matching characters in a string
|
||||||
|
---@param dir string
|
||||||
|
function M.sanitize_dir(dir)
|
||||||
|
return dir:gsub("[\\/:]+", "%%")
|
||||||
|
end
|
||||||
|
|
||||||
---Get the directory pattern based on OS
|
---Get the directory pattern based on OS
|
||||||
---@return string
|
---@return string
|
||||||
function M.dir_pattern()
|
function M.dir_pattern()
|
||||||
|
|
@ -10,19 +16,6 @@ function M.dir_pattern()
|
||||||
return pattern
|
return pattern
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Escape special pattern matching characters in a string
|
|
||||||
---@param input string
|
|
||||||
---@return string
|
|
||||||
function M.escape_dir_pattern(input)
|
|
||||||
local magic_chars = { "%", "(", ")", "+", "-", "*", "?", "[", "^", "$" }
|
|
||||||
|
|
||||||
for _, char in ipairs(magic_chars) do
|
|
||||||
input = input:gsub("%" .. char, "%%" .. char)
|
|
||||||
end
|
|
||||||
|
|
||||||
return input
|
|
||||||
end
|
|
||||||
|
|
||||||
---Check if a directory is a subdirectory of another
|
---Check if a directory is a subdirectory of another
|
||||||
---@param parent string
|
---@param parent string
|
||||||
---@param child string
|
---@param child string
|
||||||
|
|
@ -36,17 +29,17 @@ end
|
||||||
---@param dirs table The table of directories to search in
|
---@param dirs table The table of directories to search in
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.dirs_match(dir, dirs)
|
function M.dirs_match(dir, dirs)
|
||||||
dir = M.escape_dir_pattern(vim.fn.expand(dir))
|
dir = M.sanitize_dir(vim.fn.expand(dir))
|
||||||
|
|
||||||
for _, search in ipairs(dirs) do
|
for _, search in ipairs(dirs) do
|
||||||
if type(search) == "string" then
|
if type(search) == "string" then
|
||||||
search = M.escape_dir_pattern(vim.fn.expand(search))
|
search = M.sanitize_dir(vim.fn.expand(search))
|
||||||
if M.is_subdirectory(search, dir) then
|
if M.is_subdirectory(search, dir) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
elseif type(search) == "table" then
|
elseif type(search) == "table" then
|
||||||
if search.exact then
|
if search.exact then
|
||||||
search = M.escape_dir_pattern(vim.fn.expand(search[1]))
|
search = M.sanitize_dir(vim.fn.expand(search[1]))
|
||||||
if dir == search then
|
if dir == search then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue