[Perf] wrap platform utils and refactor the logic to get default (#126)

save_path
pull/131/head
The Mist 2024-07-30 03:06:11 -04:00 committed by GitHub
parent a8bba8a5f1
commit ef8c9bca99
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 12 deletions

View File

@ -1,15 +1,4 @@
local path_utils = require("codesnap.utils.path") local path_utils = require("codesnap.utils.path")
-- Get user os
-- If linux, use XDG_PICTURE_DIR, if mac use ~/Pictures, if windows use FOLDERID_Pictures (If support is added back)
local default_save_path = nil
local os_name = vim.loop.os_uname().sysname
if os_name == "Linux" then
default_save_path = os.getenv("XDG_PICTURES_DIR") or (os.getenv("HOME") .. "/Pictures")
elseif os_name == "Darwin" then
default_save_path = os.getenv("HOME") .. "/Pictures"
else
error("codesnap.nvim only supports Linux and MacOS")
end
return { return {
config = { config = {
@ -26,7 +15,7 @@ return {
min_width = 0, min_width = 0,
bg_x_padding = 122, bg_x_padding = 122,
bg_y_padding = 82, bg_y_padding = 82,
save_path = default_save_path, save_path = path_utils.get_default_save_path(),
}, },
cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))), cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))),
preview_switch = true, preview_switch = true,

View File

@ -1,4 +1,5 @@
local string_utils = require("codesnap.utils.string") local string_utils = require("codesnap.utils.string")
local platform_utils = require("codesnap.utils.platform")
local path_utils = {} local path_utils = {}
function path_utils.get_escaped_cwd() function path_utils.get_escaped_cwd()
@ -26,4 +27,21 @@ function path_utils.get_relative_path()
return full_file_path:gsub(path_utils.get_escaped_cwd(), ""):sub(2) return full_file_path:gsub(path_utils.get_escaped_cwd(), ""):sub(2)
end end
-- 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
return path_utils return path_utils

View File

@ -0,0 +1,15 @@
local platform_utils = {}
local current_os_name = vim.loop.os_uname().sysname
function platform_utils.match_os(matches_table)
local fn = matches_table[current_os_name]
if fn == nil then
error("codesnap.nvim not supported on " .. current_os_name)
end
return fn()
end
return platform_utils