[Feat] add feature that allow users can generate ASCII snapshots (#138)

This commit is contained in:
The Mist 2024-12-07 22:33:40 +08:00 committed by GitHub
parent 191c1adc9b
commit a1da69f40a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 286 additions and 150 deletions

4
generator/Cargo.lock generated
View file

@ -250,9 +250,9 @@ dependencies = [
[[package]]
name = "codesnap"
version = "0.7.1"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "969705c26569d1cc4ba1f77a6bf904108403d57f4e7c6de074508c2b14e26d73"
checksum = "14c21b08e3ff00129f07650e6f607193f2688003035e400ec2417b762cdf9ba8"
dependencies = [
"anyhow",
"arboard",

View file

@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]
[dependencies]
codesnap = "0.7.1"
codesnap = "0.7.2"
mlua = { version = "0.10.0", features = ["module", "luajit", "serialize"] }
serde = { version = "1.0.204", features = ["derive"] }

View file

@ -1,5 +1,7 @@
mod snapshot_config;
use std::{ffi::OsStr, path::Path};
use codesnap::snapshot::{image_snapshot::ImageSnapshot, snapshot_data::SnapshotData};
use mlua::prelude::*;
use snapshot_config::SnapshotConfigLua;
@ -22,11 +24,21 @@ impl From<String> for SnapshotType {
}
impl SnapshotType {
fn snapshot_data(&self, image_snapshot: ImageSnapshot) -> LuaResult<SnapshotData> {
fn snapshot_data(
&self,
image_snapshot: ImageSnapshot,
is_raw: bool,
) -> LuaResult<SnapshotData> {
let data = match self {
SnapshotType::Png => image_snapshot.png_data(),
SnapshotType::Svg => todo!(),
SnapshotType::Html => todo!(),
SnapshotType::Png => {
if is_raw {
image_snapshot.raw_data()
} else {
image_snapshot.png_data()
}
}
SnapshotType::Svg => image_snapshot.svg_data(),
SnapshotType::Html => image_snapshot.html_data(),
}
.map_err(|_| mlua::Error::RuntimeError("Failed to generate snapshot data".to_string()))?;
@ -34,29 +46,55 @@ impl SnapshotType {
}
}
fn save(
_: &Lua,
(snapshot_type, path, config): (String, String, SnapshotConfigLua),
) -> LuaResult<()> {
let image_snapshot = config
fn create_image_snapshot_by_config(config: &SnapshotConfigLua) -> LuaResult<ImageSnapshot> {
config
.0
.create_snapshot()
.map_err(|_| mlua::Error::RuntimeError("Failed to create snapshot".to_string()))?;
let snapshot_type: SnapshotType = snapshot_type.into();
.map_err(|_| mlua::Error::RuntimeError("Failed to create image snapshot".to_string()))
}
snapshot_type
.snapshot_data(image_snapshot)?
fn save(_: &Lua, (path, config): (String, SnapshotConfigLua)) -> LuaResult<()> {
let snapshot_type: SnapshotType = Path::new(&path)
.extension()
.and_then(OsStr::to_str)
.ok_or_else(|| mlua::Error::RuntimeError("Invalid file extension".to_string()))?
.to_string()
.into();
SnapshotType::from(snapshot_type)
.snapshot_data(create_image_snapshot_by_config(&config)?, false)?
.save(&path)
.map_err(|_| mlua::Error::RuntimeError(format!("Failed to save snapshot data to {}", path)))
}
fn copy_to_clipboard(_: &Lua, snapshot_type: SnapshotType) {}
fn copy(_: &Lua, (snapshot_type, config): (String, SnapshotConfigLua)) -> LuaResult<()> {
SnapshotType::from(snapshot_type)
.snapshot_data(create_image_snapshot_by_config(&config)?, true)?
.copy()
.map_err(|_| mlua::Error::RuntimeError("Failed to copy snapshot to clipboard".to_string()))
}
fn copy_ascii(_: &Lua, config: SnapshotConfigLua) -> LuaResult<()> {
config
.0
.create_ascii_snapshot()
.raw_data()
.map_err(|_| mlua::Error::RuntimeError("Failed to generate ASCII snapshot".to_string()))?
.copy()
.map_err(|_| {
mlua::Error::RuntimeError("Failed to copy ASCII snapshot to clipboard".to_string())
})?;
Ok(())
}
#[mlua::lua_module(skip_memory_check)]
fn codesnap_generator(lua: &Lua) -> LuaResult<LuaTable> {
fn generator(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("save", lua.create_function(save)?)?;
exports.set("copy", lua.create_function(copy)?)?;
exports.set("copy_ascii", lua.create_function(copy_ascii)?)?;
Ok(exports)
}

View file

@ -5,33 +5,7 @@ local static = require("codesnap.static")
local table_utils = require("codesnap.utils.table")
local config_module = {}
local assets_folder = static.cwd .. "/assets"
-- 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
local function get_file_path(show_workspace)
local relative_path = path_utils.get_relative_path()
return show_workspace and path_utils.get_workspace() .. "/" .. relative_path or relative_path
end
function config_module.get_config(extension)
function config_module.get_config()
local code = visual_utils.get_selected_text()
local start_line_number = visual_utils.get_start_line_number()
@ -40,19 +14,20 @@ function config_module.get_config(extension)
return
end
local config = table_utils.merge({
code = code,
extension = extension,
code_file_path = vim.fn.expand("%:p"),
fonts_folder = assets_folder .. "/fonts",
themes_folder = assets_folder .. "/themes",
theme = "base16-onedark",
file_path = static.config.has_breadcrumbs and get_file_path(static.config.show_workspace) or "",
start_line_number = static.config.has_line_number and start_line_number or nil,
}, static.config)
config.save_path = parse_save_path(config.save_path)
local config = table_utils.assign(static.config, {
code = {
content = code,
file_path = vim.fn.expand("%:p"),
line_number = {
start_number = start_line_number,
color = "#495162",
},
},
-- file_path = static.config.has_breadcrumbs and get_file_path(static.config.show_workspace) or "",
-- start_line_number = static.config.has_line_number and start_line_number or nil,
})
-- config.save_path = parse_save_path(config.save_path)
return config
end

View file

@ -2,75 +2,114 @@ local static = require("codesnap.static")
local visual_utils = require("codesnap.utils.visual")
local table_utils = require("codesnap.utils.table")
local string_utils = require("codesnap.utils.string")
local path_utils = require("codesnap.utils.path")
local module = require("codesnap.module")
local config_module = require("codesnap.config")
local highlight_module = require("codesnap.highlight")
local main = {
cwd = static.cwd,
preview_switch = static.preview_switch,
highlight_mode_config = nil,
}
-- Prepare the path of the Rust module
-- Concat lib?.extension and ?.extension to package.cpath
package.cpath = path_utils.join(";", package.cpath, module.generator_file("?"), module.generator_file("lib?"))
function main.setup(config)
static.config = table_utils.merge(static.config, config == nil and {} or config)
static.config = table_utils.merge_config(static.config, config == nil and {} or config)
end
function main.copy_into_clipboard_with_config(config)
require("generator").copy_into_clipboard(config)
vim.cmd("delmarks <>")
vim.notify("Save snapshot into clipboard successfully")
end
-- Save snapshot to specified save_path
--- @param save_path string
function main.save(save_path)
local generator = require("generator")
-- 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
function main.save_snapshot_with_config(config)
if string_utils.is_str_empty(static.config.save_path) then
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
)
if save_path == nil then
error("Save path is not specified", 0)
end
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)
generator.save(save_path, config_module.get_config())
vim.cmd("delmarks <>")
---@diagnostic disable-next-line: need-check-nil
vim.notify("Save snapshot in " .. config.save_path .. " successfully")
vim.notify("Save snapshot in " .. save_path .. " successfully!")
end
-- 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))
-- Copy snapshot into clipboard
--- @param type? string
function main.copy(type)
local snapshot_type = type == nil and "png" or type
local generator = require("generator")
generator.copy(snapshot_type, config_module.get_config())
vim.cmd("delmarks <>")
vim.notify("The snapshot is copied into clipboard successfully!")
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))
-- Generate ASCII code snapshot and copy it into clipboard
function main.copy_ascii()
local generator = require("generator")
generator.copy_ascii(config_module.get_config())
vim.cmd("delmarks <>")
vim.notify("The ASCII code snapshot is copied into clipboard successfully!")
end
function main.highlight_mode_copy_into_clipboard(extension)
main.highlight_mode_config = config_module.get_config(extension)
highlight_module.create_highlight_selector_window(
"copy_into_clipboard_with_config",
visual_utils.get_selected_lines()
)
end
function main.highlight_mode_save_snapshot(extension)
main.highlight_mode_config = config_module.get_config(extension)
highlight_module.create_highlight_selector_window("save_snapshot_with_config", visual_utils.get_selected_lines())
end
-- function main.copy_into_clipboard_with_config(config)
-- require("generator").copy_into_clipboard(config)
-- vim.cmd("delmarks <>")
-- vim.notify("Save snapshot into clipboard successfully")
-- end
--
-- -- 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
--
-- function main.save_snapshot_with_config(config)
-- if string_utils.is_str_empty(static.config.save_path) then
-- 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
-- )
-- end
--
-- 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)
-- vim.cmd("delmarks <>")
-- ---@diagnostic disable-next-line: need-check-nil
-- vim.notify("Save snapshot in " .. config.save_path .. " successfully")
-- end
--
-- -- 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)
--
-- highlight_module.create_highlight_selector_window(
-- "copy_into_clipboard_with_config",
-- visual_utils.get_selected_lines()
-- )
-- end
--
-- function main.highlight_mode_save_snapshot(extension)
-- main.highlight_mode_config = config_module.get_config(extension)
--
-- highlight_module.create_highlight_selector_window("save_snapshot_with_config", visual_utils.get_selected_lines())
-- end
return main

24
lua/codesnap/module.lua Normal file
View file

@ -0,0 +1,24 @@
local module = {}
local path_utils = require("codesnap.utils.path")
local OS_LIB_EXTENSION_MAP = {
mac = "dylib",
osx = "dylib",
windows = "dll",
linux = "so",
}
local RUST_BUILD_DIR = path_utils.with_dir_name("../../../generator/target/debug")
function module.get_lib_extension()
local extension = OS_LIB_EXTENSION_MAP[jit.os:lower()]
return extension or "so"
end
function module.generator_file(filename)
return path_utils.join("/", RUST_BUILD_DIR, filename .. "." .. module.get_lib_extension())
end
return module

View file

@ -2,21 +2,47 @@ local path_utils = require("codesnap.utils.path")
return {
config = {
mac_window_bar = true,
title = "CodeSnap.nvim",
code_font_family = "CaskaydiaCove Nerd Font",
watermark_font_family = "Pacifico",
watermark = "CodeSnap.nvim",
bg_theme = "default",
breadcrumbs_separator = "/",
has_breadcrumbs = false,
has_line_number = false,
show_workspace = false,
min_width = 0,
bg_x_padding = 122,
bg_y_padding = 82,
save_path = path_utils.get_default_save_path(),
window = {
mac_window_bar = true,
shadow = 20,
margin = {
x = 82,
y = 82,
},
border = {
color = "#ffffff30",
},
},
code = {
font_family = "CaskaydiaCove Nerd Font",
theme = "candy",
},
watermark = {
content = "CodeSnap",
font_family = "Pacifico",
color = "#ffffff",
},
scale_factor = 3,
background = {
start = {
x = 0,
y = 0,
},
["end"] = {
x = "max",
y = 0,
},
stops = {
{
position = 0,
color = "#6bcba5",
},
{
position = 1,
color = "#caf4c2",
},
},
},
},
cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))),
preview_switch = true,
}

View file

@ -2,6 +2,20 @@ local string_utils = require("codesnap.utils.string")
local platform_utils = require("codesnap.utils.platform")
local path_utils = {}
function path_utils.join(separator, ...)
local args = { ... }
return table.concat(args, separator)
end
function path_utils.dir_name()
return debug.getinfo(1).source:match("@?(.*/)")
end
function path_utils.with_dir_name(path)
return path_utils.dir_name() .. path
end
function path_utils.get_escaped_cwd()
local cwd = vim.fn.getcwd()

View file

@ -2,9 +2,17 @@ local list_utils = require("codesnap.utils.list")
local table_utils = {}
function table_utils.assign(t, props)
local parsed_t = t or {}
for k, v in pairs(props) do
t[k] = v
if type(v) == "table" then
parsed_t[k] = table_utils.assign(parsed_t[k], v)
else
parsed_t[k] = v
end
end
return parsed_t
end
function table_utils.merge(t1, t2)
@ -15,6 +23,22 @@ function table_utils.merge(t1, t2)
return t1
end
-- Merge two tables, if the value of the key in t2 is "none", it will be removed from t1
-- which is useful for removing a key from the config, in CodeSnap, set border = None to remove the border
function table_utils.merge_config(t1, t2)
for k, v in pairs(t1) do
if type(v) == "table" and type(t2[k]) == "table" then
t1[k] = table_utils.merge_config(v, t2[k])
elseif t2[k] == "none" then
t1[k] = nil
elseif t2[k] ~= nil then
t1[k] = t2[k]
end
end
return t1
end
function table_utils.is_array(t)
return type(t[1]) == "number"
end
@ -67,14 +91,4 @@ function table_utils.to_string(t)
return parse(t)
end
-- function table_utils.to_string(t)
-- local result = ""
--
-- for key, value in pairs(t) do
-- result = result .. key .. ":" .. tostring(value) .. ","
-- end
--
-- return "{" .. result .. "}"
-- end
return table_utils

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -19,24 +19,30 @@ local function take_snapshot(take_snapshot_function)
end
end
vim.api.nvim_create_user_command("CodeSnap", take_snapshot(codesnap.copy_into_clipboard), { nargs = "*", range = "%" })
vim.api.nvim_create_user_command("CodeSnapSave", take_snapshot(codesnap.save), { nargs = "*", range = "%" })
vim.api.nvim_create_user_command("CodeSnapSave", take_snapshot(codesnap.save_snapshot), { nargs = "*", range = "%" })
vim.api.nvim_create_user_command("CodeSnap", take_snapshot(codesnap.copy), { nargs = "*", range = "%" })
vim.api.nvim_create_user_command(
"CodeSnapASCII",
take_snapshot(codesnap.copy_ascii_snapshot),
{ nargs = "*", range = "%" }
)
vim.api.nvim_create_user_command("CodeSnapASCII", take_snapshot(codesnap.copy_ascii), { nargs = "*", range = "%" })
vim.api.nvim_create_user_command(
"CodeSnapHighlight",
take_snapshot(codesnap.highlight_mode_copy_into_clipboard),
{ nargs = "*", range = "%" }
)
vim.api.nvim_create_user_command(
"CodeSnapSaveHighlight",
take_snapshot(codesnap.highlight_mode_save_snapshot),
{ nargs = "*", range = "%" }
)
-- vim.api.nvim_create_user_command("CodeSnap", take_snapshot(codesnap.copy_into_clipboard), { nargs = "*", range = "%" })
--
-- vim.api.nvim_create_user_command("CodeSnapSave", take_snapshot(codesnap.save_snapshot), { nargs = "*", range = "%" })
--
-- vim.api.nvim_create_user_command(
-- "CodeSnapASCII",
-- take_snapshot(codesnap.copy_ascii_snapshot),
-- { nargs = "*", range = "%" }
-- )
--
-- vim.api.nvim_create_user_command(
-- "CodeSnapHighlight",
-- take_snapshot(codesnap.highlight_mode_copy_into_clipboard),
-- { nargs = "*", range = "%" }
-- )
--
-- vim.api.nvim_create_user_command(
-- "CodeSnapSaveHighlight",
-- take_snapshot(codesnap.highlight_mode_save_snapshot),
-- { nargs = "*", range = "%" }
-- )