Skip to content

Commit

Permalink
[Feat] use auto-generate filename when the filename is not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Apr 5, 2024
1 parent 0771929 commit 2eede49
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
24 changes: 23 additions & 1 deletion lua/codesnap/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ local function parse_extension(specify_extension)
or parse_file_extension_by_highlighting_file_presets(filename, file_extension)
end

-- 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

function config_module.get_config(specify_extension)
local code = visual_utils.get_selected_text()
local extension = specify_extension or parse_extension(specify_extension)
Expand All @@ -45,14 +63,18 @@ function config_module.get_config(specify_extension)
error("Cannot detect current filetype", 0)
end

return table_utils.merge({
local config = table_utils.merge({
code = code,
extension = 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 "",
}, static.config)

config.save_path = parse_save_path(config.save_path)

return config
end

return config_module
12 changes: 10 additions & 2 deletions lua/codesnap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ function main.save_snapshot(extension)
)
end

require("generator").save_snapshot(config_module.get_config(extension))
local matched_extension = string.match(static.config.save_path, "%.(.+)$")

if matched_extension ~= "png" and matched_extension ~= nil then
error("The extension of save_path should be .png", 0)
end

local config = config_module.get_config(extension)

require("generator").save_snapshot(config)
vim.cmd("delmarks <>")
vim.notify("Save snapshot in " .. static.config.save_path .. " successfully")
vim.notify("Save snapshot in " .. config.save_path .. " successfully")
end

return main

0 comments on commit 2eede49

Please sign in to comment.