2024-03-17 14:34:01 +00:00
|
|
|
local string_utils = require("codesnap.utils.string")
|
2024-07-30 07:06:11 +00:00
|
|
|
local platform_utils = require("codesnap.utils.platform")
|
2024-02-19 14:27:17 +00:00
|
|
|
local path_utils = {}
|
|
|
|
|
2024-05-07 13:34:14 +00:00
|
|
|
function path_utils.get_escaped_cwd()
|
|
|
|
local cwd = vim.fn.getcwd()
|
|
|
|
|
|
|
|
return string_utils.escape(cwd)
|
|
|
|
end
|
|
|
|
|
2024-02-19 14:27:17 +00:00
|
|
|
function path_utils.back(path)
|
|
|
|
local parsed_path, _ = path:gsub("/[^\\/]+/?$", "")
|
|
|
|
|
|
|
|
return parsed_path
|
|
|
|
end
|
|
|
|
|
2024-05-07 13:34:14 +00:00
|
|
|
function path_utils.get_workspace()
|
|
|
|
local cwd = vim.fn.getcwd()
|
|
|
|
local _, _, workspace = string.find(cwd, "/([^/]+)$")
|
|
|
|
|
|
|
|
return workspace == nil and "" or workspace
|
|
|
|
end
|
|
|
|
|
2024-03-17 14:34:01 +00:00
|
|
|
function path_utils.get_relative_path()
|
|
|
|
local full_file_path = vim.fn.expand("%:p")
|
|
|
|
|
2024-05-07 13:34:14 +00:00
|
|
|
return full_file_path:gsub(path_utils.get_escaped_cwd(), ""):sub(2)
|
2024-03-17 14:34:01 +00:00
|
|
|
end
|
|
|
|
|
2024-07-30 07:06:11 +00:00
|
|
|
-- Get default save path by OS
|
|
|
|
-- If Linux, use XDG_PICTURE_DIR
|
|
|
|
-- if mac use ~/Pictures
|
|
|
|
-- if windows use FOLDERID_Pictures (If support is added back)
|
|
|
|
function path_utils.get_default_save_path()
|
|
|
|
local home_picture_folder = os.getenv("HOME") .. "/Pictures"
|
|
|
|
|
|
|
|
return platform_utils.match_os({
|
|
|
|
Darwin = function()
|
|
|
|
return home_picture_folder
|
|
|
|
end,
|
|
|
|
Linux = function()
|
|
|
|
return os.getenv("XDG_PICTURES_DIR") or home_picture_folder
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2024-02-19 14:27:17 +00:00
|
|
|
return path_utils
|