2024-02-14 09:15:26 -08:00
local static = require ( " codesnap.static " )
2024-05-04 06:03:10 -07:00
local visual_utils = require ( " codesnap.utils.visual " )
2024-03-16 10:07:12 -07:00
local table_utils = require ( " codesnap.utils.table " )
local string_utils = require ( " codesnap.utils.string " )
2024-04-05 08:23:17 -07:00
local config_module = require ( " codesnap.config " )
2024-04-27 04:07:41 -07:00
local highlight_module = require ( " codesnap.highlight " )
2024-03-16 10:07:12 -07:00
2024-02-21 02:33:44 -08:00
local main = {
cwd = static.cwd ,
preview_switch = static.preview_switch ,
2024-04-27 04:07:41 -07:00
highlight_mode_config = nil ,
2024-02-21 02:33:44 -08:00
}
2024-02-12 22:05:36 -08:00
function main . setup ( config )
2024-02-19 06:27:17 -08:00
static.config = table_utils.merge ( static.config , config == nil and { } or config )
2024-03-16 10:07:12 -07:00
end
2024-04-27 04:07:41 -07:00
function main . copy_into_clipboard_with_config ( config )
require ( " generator " ) . copy_into_clipboard ( config )
2024-03-16 10:07:12 -07:00
vim.cmd ( " delmarks <> " )
vim.notify ( " Save snapshot into clipboard successfully " )
2024-02-21 02:33:44 -08:00
end
2024-07-05 02:00:50 -07:00
-- Take ASCII code snapshot into clipboard
function main . copy_ascii_snapshot ( extension )
require ( " generator " ) . copy_ascii ( config_module.get_config ( extension ) )
vim.cmd ( " delmarks <> " )
vim.notify ( " Save snapshot into clipboard successfully " )
end
2024-04-27 04:07:41 -07:00
function main . save_snapshot_with_config ( config )
2024-03-16 10:07:12 -07:00
if string_utils.is_str_empty ( static.config . save_path ) then
2024-04-02 05:04:37 -07:00
error (
" If you want to save snapshot in somewhere, you should config the save_path before, refer: https://github.com/mistricky/codesnap.nvim?tab=readme-ov-file#save-the-snapshot " ,
0
)
2024-03-16 10:07:12 -07:00
end
2024-04-05 09:10:08 -07:00
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
require ( " generator " ) . save_snapshot ( config )
2024-03-16 10:07:12 -07:00
vim.cmd ( " delmarks <> " )
2024-04-25 10:01:17 -07:00
---@diagnostic disable-next-line: need-check-nil
2024-04-05 09:10:08 -07:00
vim.notify ( " Save snapshot in " .. config.save_path .. " successfully " )
2024-02-21 02:33:44 -08:00
end
2024-04-27 04:07:41 -07:00
-- Take a snapshot and copy it into clipboard
function main . copy_into_clipboard ( extension )
main.copy_into_clipboard_with_config ( config_module.get_config ( extension ) )
end
-- Take a snapshot and save it into the specified path
function main . save_snapshot ( extension )
main.save_snapshot_with_config ( config_module.get_config ( extension ) )
end
function main . highlight_mode_copy_into_clipboard ( extension )
main.highlight_mode_config = config_module.get_config ( extension )
2024-05-04 06:03:10 -07:00
highlight_module.create_highlight_selector_window (
" copy_into_clipboard_with_config " ,
visual_utils.get_selected_lines ( )
)
2024-04-27 04:07:41 -07:00
end
function main . highlight_mode_save_snapshot ( extension )
main.highlight_mode_config = config_module.get_config ( extension )
2024-05-04 06:03:10 -07:00
highlight_module.create_highlight_selector_window ( " save_snapshot_with_config " , visual_utils.get_selected_lines ( ) )
2024-04-27 04:07:41 -07:00
end
2024-02-12 22:05:36 -08:00
return main