diff --git a/lua/codesnap/static.lua b/lua/codesnap/static.lua index 14ac580..0c571c9 100644 --- a/lua/codesnap/static.lua +++ b/lua/codesnap/static.lua @@ -1,15 +1,4 @@ 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 { config = { @@ -26,7 +15,7 @@ return { min_width = 0, bg_x_padding = 122, 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("(.*[/\\])"))), preview_switch = true, diff --git a/lua/codesnap/utils/path.lua b/lua/codesnap/utils/path.lua index 6318c3e..37c06f8 100644 --- a/lua/codesnap/utils/path.lua +++ b/lua/codesnap/utils/path.lua @@ -1,4 +1,5 @@ local string_utils = require("codesnap.utils.string") +local platform_utils = require("codesnap.utils.platform") local path_utils = {} 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) 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 diff --git a/lua/codesnap/utils/platform.lua b/lua/codesnap/utils/platform.lua new file mode 100644 index 0000000..9e4c767 --- /dev/null +++ b/lua/codesnap/utils/platform.lua @@ -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