Skip to content

Commit

Permalink
[Feat] add feature that allow users can generate ASCII snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Dec 7, 2024
1 parent 191c1ad commit 7cca21a
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 152 deletions.
4 changes: 2 additions & 2 deletions generator/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

68 changes: 53 additions & 15 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,41 +24,77 @@ 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()))?;

Ok(data)
}
}

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)
}
55 changes: 15 additions & 40 deletions lua/codesnap/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

Expand Down
133 changes: 86 additions & 47 deletions lua/codesnap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

function main.setup(config)
static.config = table_utils.merge(static.config, config == nil and {} or config)
end
-- 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.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")
function main.setup(config)
static.config = table_utils.merge_config(static.config, config == nil and {} or config)
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, "%.(.+)$")
-- Save snapshot to specified save_path
--- @param save_path string
function main.save(save_path)
local generator = require("generator")

if matched_extension ~= "png" and matched_extension ~= nil then
error("The extension of save_path should be .png", 0)
if save_path == nil then
error("Save path is not specified", 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))
end
-- Copy snapshot into clipboard
--- @param type? string
function main.copy(type)
local snapshot_type = type == nil and "png" or type
local generator = require("generator")

-- 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))
generator.copy(snapshot_type, config_module.get_config())
vim.cmd("delmarks <>")
vim.notify("The snapshot is copied into clipboard successfully!")
end

function main.highlight_mode_copy_into_clipboard(extension)
main.highlight_mode_config = config_module.get_config(extension)
-- Generate ASCII code snapshot and copy it into clipboard
function main.copy_ascii()
local generator = require("generator")

highlight_module.create_highlight_selector_window(
"copy_into_clipboard_with_config",
visual_utils.get_selected_lines()
)
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_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 changes: 24 additions & 0 deletions lua/codesnap/module.lua
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 7cca21a

Please sign in to comment.