2024-02-14 17:15:26 +00:00
|
|
|
local static = require("codesnap.static")
|
2024-03-16 17:07:12 +00:00
|
|
|
local table_utils = require("codesnap.utils.table")
|
|
|
|
local generator = require("generator")
|
|
|
|
local string_utils = require("codesnap.utils.string")
|
2024-02-19 14:27:17 +00:00
|
|
|
local visual_utils = require("codesnap.utils.visual")
|
2024-03-17 14:34:01 +00:00
|
|
|
local path_utils = require("codesnap.utils.path")
|
2024-02-14 17:15:26 +00:00
|
|
|
|
2024-03-16 17:07:12 +00:00
|
|
|
local assets_folder = static.cwd .. "/assets"
|
|
|
|
|
|
|
|
local function get_extension()
|
|
|
|
local file_path = vim.fn.expand("%:p")
|
|
|
|
local file_extension = string.match(file_path, "%.([^%.]+)$")
|
|
|
|
|
|
|
|
return file_extension
|
|
|
|
end
|
|
|
|
|
2024-02-21 10:33:44 +00:00
|
|
|
local main = {
|
|
|
|
cwd = static.cwd,
|
|
|
|
preview_switch = static.preview_switch,
|
|
|
|
}
|
2024-02-13 06:05:36 +00:00
|
|
|
|
|
|
|
function main.setup(config)
|
2024-02-19 14:27:17 +00:00
|
|
|
static.config = table_utils.merge(static.config, config == nil and {} or config)
|
2024-03-16 17:07:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_config()
|
|
|
|
local code = visual_utils.get_selected_text()
|
|
|
|
local extension = get_extension()
|
2024-02-19 14:27:17 +00:00
|
|
|
|
2024-03-16 17:07:12 +00:00
|
|
|
if string_utils.is_str_empty(code) then
|
|
|
|
error("Please select code which you want to take snapshot first")
|
2024-02-19 14:27:17 +00:00
|
|
|
end
|
|
|
|
|
2024-03-16 17:07:12 +00:00
|
|
|
if string_utils.is_str_empty(extension) then
|
|
|
|
error("Cannot detect current filetype")
|
|
|
|
end
|
2024-02-19 14:27:17 +00:00
|
|
|
|
2024-03-16 17:07:12 +00:00
|
|
|
return table_utils.merge({
|
|
|
|
code = code,
|
|
|
|
extension = extension,
|
|
|
|
fonts_folder = assets_folder .. "/fonts",
|
|
|
|
themes_folder = assets_folder .. "/themes",
|
|
|
|
theme = "base16-onedark",
|
2024-03-17 14:34:01 +00:00
|
|
|
file_path = static.config.has_breadcrumbs and path_utils.get_relative_path() or "",
|
2024-03-16 17:07:12 +00:00
|
|
|
}, static.config)
|
2024-02-13 06:05:36 +00:00
|
|
|
end
|
|
|
|
|
2024-03-16 17:07:12 +00:00
|
|
|
function main.copy_into_clipboard()
|
|
|
|
generator.copy_into_clipboard(get_config())
|
|
|
|
vim.cmd("delmarks <>")
|
|
|
|
vim.notify("Save snapshot into clipboard successfully")
|
2024-02-21 10:33:44 +00:00
|
|
|
end
|
|
|
|
|
2024-03-16 17:07:12 +00:00
|
|
|
function main.save_snapshot()
|
|
|
|
if string_utils.is_str_empty(static.config.save_path) then
|
|
|
|
error("Cannot find save_path from config")
|
|
|
|
end
|
|
|
|
|
|
|
|
generator.save_snapshot(get_config())
|
|
|
|
vim.cmd("delmarks <>")
|
|
|
|
vim.notify("Save snapshot in " .. static.config.save_path .. " successfully")
|
2024-02-21 10:33:44 +00:00
|
|
|
end
|
|
|
|
|
2024-02-13 06:05:36 +00:00
|
|
|
return main
|