2024-04-05 15:23:17 +00:00
|
|
|
local visual_utils = require("codesnap.utils.visual")
|
|
|
|
local path_utils = require("codesnap.utils.path")
|
|
|
|
local string_utils = require("codesnap.utils.string")
|
|
|
|
local static = require("codesnap.static")
|
|
|
|
local table_utils = require("codesnap.utils.table")
|
|
|
|
local config_module = {}
|
|
|
|
|
|
|
|
local assets_folder = static.cwd .. "/assets"
|
|
|
|
|
2024-04-05 16:10:08 +00:00
|
|
|
-- Auto generated codesnap filename based on the following rule:
|
|
|
|
-- CodeSnap_y-m-d_at_h:m:s
|
|
|
|
local function auto_generate_snap_filename()
|
|
|
|
return os.date("CodeSnap_%Y-%m-%d_at_%H:%M:%S.png")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If the save_path is already configured, but no explicit filename is specified,
|
|
|
|
-- it will be replaced with auto-generated filename
|
|
|
|
local function parse_save_path(save_path)
|
|
|
|
if save_path == nil or string_utils.ends_with(save_path, "png") then
|
|
|
|
return save_path
|
|
|
|
end
|
|
|
|
|
|
|
|
local parsed_save_path = string_utils.ends_with(save_path, "/") and save_path or save_path .. "/"
|
|
|
|
|
|
|
|
return parsed_save_path .. auto_generate_snap_filename()
|
|
|
|
end
|
|
|
|
|
2024-04-07 09:24:43 +00:00
|
|
|
function config_module.get_config(extension)
|
2024-04-05 15:23:17 +00:00
|
|
|
local code = visual_utils.get_selected_text()
|
2024-04-25 17:01:17 +00:00
|
|
|
local start_line_number = visual_utils.get_start_line_number()
|
2024-04-05 15:23:17 +00:00
|
|
|
|
|
|
|
if string_utils.is_str_empty(code) then
|
|
|
|
error("No code is selected", 0)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-04-05 16:10:08 +00:00
|
|
|
local config = table_utils.merge({
|
2024-04-05 15:23:17 +00:00
|
|
|
code = code,
|
|
|
|
extension = extension,
|
2024-04-07 09:24:43 +00:00
|
|
|
code_file_path = vim.fn.expand("%:p"),
|
2024-04-05 15:23:17 +00:00
|
|
|
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 "",
|
2024-04-25 17:01:17 +00:00
|
|
|
start_line_number = static.config.has_line_number and start_line_number or nil,
|
2024-04-05 15:23:17 +00:00
|
|
|
}, static.config)
|
2024-04-05 16:10:08 +00:00
|
|
|
|
|
|
|
config.save_path = parse_save_path(config.save_path)
|
|
|
|
|
|
|
|
return config
|
2024-04-05 15:23:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return config_module
|