From 759cf46ca78964ca844202135fa594513eec7a34 Mon Sep 17 00:00:00 2001 From: Mist Date: Fri, 5 Jul 2024 16:25:19 +0800 Subject: [PATCH] [Feat] support take code snapshot as ascii art --- generator/src/copy_ascii.rs | 55 +++++++++++++++++++++++++++++++++++++ generator/src/lib.rs | 3 ++ lua/codesnap/init.lua | 7 +++++ plugin/codesnap.lua | 6 ++++ 4 files changed, 71 insertions(+) create mode 100644 generator/src/copy_ascii.rs diff --git a/generator/src/copy_ascii.rs b/generator/src/copy_ascii.rs new file mode 100644 index 0000000..e4ec80c --- /dev/null +++ b/generator/src/copy_ascii.rs @@ -0,0 +1,55 @@ +use std::cmp::max; + +use crate::{code::calc_wh, config::TakeSnapshotParams}; +use arboard::Clipboard; +use nvim_oxi::Result; + +const SPACE_BOTH_SIDE: usize = 2; +const LINE_NUMBER_SPACE: usize = 3; + +fn optional(component: String, is_view: bool) -> String { + if is_view { + component + } else { + "".to_string() + } +} + +#[allow(dead_code)] +pub fn copy_ascii(params: TakeSnapshotParams) -> Result<()> { + let (width, _) = calc_wh(¶ms.code, 1., 1.); + let frame_width = max(width as usize, params.file_path.len()) + SPACE_BOTH_SIDE; + let frame_width = match params.start_line_number { + Some(_) => frame_width + LINE_NUMBER_SPACE, + None => frame_width, + }; + let line = format!("│{}│\n", "─".repeat(frame_width)); + let frame_width_with_content = frame_width - 1; + let top_frame = format!("╭{}╮\n", "─".repeat(frame_width)); + let bottom_frame = format!("╰{}╯", "─".repeat(frame_width)); + let code = params + .code + .lines() + .enumerate() + .map(|(i, line)| { + format!( + "│ {:1$} │\n", + match params.start_line_number { + Some(start_line_number) => format!(" {} {}", start_line_number + i, line), + None => line.to_string(), + }, + frame_width_with_content - 1 + ) + }) + .collect::(); + let text_line = |text: &str| format!("│ {:1$}│\n", text, frame_width_with_content); + let breadcrumbs = optional( + format!("{}{line}", text_line(¶ms.file_path)), + params.has_breadcrumbs, + ); + let ascii_snapshot = format!("{top_frame}{breadcrumbs}{code}{bottom_frame}"); + + Clipboard::new().unwrap().set_text(ascii_snapshot).unwrap(); + + Ok(()) +} diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 5d9dd52..639f162 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -3,6 +3,7 @@ mod color; mod components; mod config; mod copy; +mod copy_ascii; mod edges; mod highlight; mod path; @@ -11,6 +12,7 @@ mod snapshot; mod text; use copy::copy_into_clipboard; +use copy_ascii::copy_ascii; use nvim_oxi::{Dictionary, Function, Result}; use save::save_snapshot; @@ -22,5 +24,6 @@ fn generator() -> Result { Function::from_fn(copy_into_clipboard), ), ("save_snapshot", Function::from_fn(save_snapshot)), + ("copy_ascii", Function::from_fn(copy_ascii)), ])) } diff --git a/lua/codesnap/init.lua b/lua/codesnap/init.lua index 2b3f2ea..5da5971 100644 --- a/lua/codesnap/init.lua +++ b/lua/codesnap/init.lua @@ -21,6 +21,13 @@ function main.copy_into_clipboard_with_config(config) 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( diff --git a/plugin/codesnap.lua b/plugin/codesnap.lua index 845a9a8..8fe68fa 100644 --- a/plugin/codesnap.lua +++ b/plugin/codesnap.lua @@ -23,6 +23,12 @@ vim.api.nvim_create_user_command("CodeSnap", take_snapshot(codesnap.copy_into_cl 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),