Skip to content

Commit

Permalink
[Feat] add show_workspace config for display workspace in breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed May 7, 2024
1 parent a2d59ef commit ea40d82
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lua/codesnap/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ local function parse_save_path(save_path)
return parsed_save_path .. auto_generate_snap_filename()
end

local function get_file_path(show_workspace)
local relative_path = path_utils.get_relative_path()

return show_workspace and path_utils.get_workspace() .. "/" .. relative_path or relative_path
end

function config_module.get_config(extension)
local code = visual_utils.get_selected_text()
local start_line_number = visual_utils.get_start_line_number()
Expand All @@ -41,7 +47,7 @@ function config_module.get_config(extension)
fonts_folder = assets_folder .. "/fonts",
themes_folder = assets_folder .. "/themes",
theme = "base16-onedark",
file_path = static.config.has_breadcrumbs and path_utils.get_relative_path() or "",
file_path = static.config.has_breadcrumbs and get_file_path(static.config.show_workspace) or "",
start_line_number = static.config.has_line_number and start_line_number or nil,
}, static.config)

Expand Down
1 change: 1 addition & 0 deletions lua/codesnap/static.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return {
breadcrumbs_separator = "/",
has_breadcrumbs = false,
has_line_number = false,
show_workspace = false,
min_width = 0,
},
cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))),
Expand Down
16 changes: 14 additions & 2 deletions lua/codesnap/utils/path.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
local string_utils = require("codesnap.utils.string")
local path_utils = {}

function path_utils.get_escaped_cwd()
local cwd = vim.fn.getcwd()

return string_utils.escape(cwd)
end

function path_utils.back(path)
local parsed_path, _ = path:gsub("/[^\\/]+/?$", "")

return parsed_path
end

function path_utils.get_workspace()
local cwd = vim.fn.getcwd()
local _, _, workspace = string.find(cwd, "/([^/]+)$")

return workspace == nil and "" or workspace
end

function path_utils.get_relative_path()
local full_file_path = vim.fn.expand("%:p")
local cwd = vim.fn.getcwd()

return full_file_path:gsub(string_utils.escape(cwd), ""):sub(2)
return full_file_path:gsub(path_utils.get_escaped_cwd(), ""):sub(2)
end

return path_utils

0 comments on commit ea40d82

Please sign in to comment.