[Feat] use auto-generate filename when the filename is not specified (#72)

* [Chore] remove redundancy code

* [Feat] use auto-generate filename when the filename is not specified
pull/77/head
The Mist 2024-04-06 00:10:08 +08:00 committed by GitHub
parent 8bd3cd89e0
commit 42437163db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 4 deletions

View File

@ -2,7 +2,6 @@ local visual_utils = require("codesnap.utils.visual")
local path_utils = require("codesnap.utils.path") local path_utils = require("codesnap.utils.path")
local string_utils = require("codesnap.utils.string") local string_utils = require("codesnap.utils.string")
local static = require("codesnap.static") local static = require("codesnap.static")
local list_utils = require("codesnap.utils.list")
local table_utils = require("codesnap.utils.table") local table_utils = require("codesnap.utils.table")
local config_module = {} local config_module = {}
@ -33,6 +32,24 @@ local function parse_extension(specify_extension)
or parse_file_extension_by_highlighting_file_presets(filename, file_extension) or parse_file_extension_by_highlighting_file_presets(filename, file_extension)
end 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) function config_module.get_config(specify_extension)
local code = visual_utils.get_selected_text() local code = visual_utils.get_selected_text()
local extension = specify_extension or parse_extension(specify_extension) local extension = specify_extension or parse_extension(specify_extension)
@ -46,7 +63,7 @@ function config_module.get_config(specify_extension)
error("Cannot detect current filetype", 0) error("Cannot detect current filetype", 0)
end end
return table_utils.merge({ local config = table_utils.merge({
code = code, code = code,
extension = extension, extension = extension,
fonts_folder = assets_folder .. "/fonts", fonts_folder = assets_folder .. "/fonts",
@ -54,6 +71,10 @@ function config_module.get_config(specify_extension)
theme = "base16-onedark", theme = "base16-onedark",
file_path = static.config.has_breadcrumbs and path_utils.get_relative_path() or "", file_path = static.config.has_breadcrumbs and path_utils.get_relative_path() or "",
}, static.config) }, static.config)
config.save_path = parse_save_path(config.save_path)
return config
end end
return config_module return config_module

View File

@ -26,9 +26,17 @@ function main.save_snapshot(extension)
) )
end 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.cmd("delmarks <>")
vim.notify("Save snapshot in " .. static.config.save_path .. " successfully") vim.notify("Save snapshot in " .. config.save_path .. " successfully")
end end
return main return main