From b9653c6d66f0752ba30263ac274c56a112b31296 Mon Sep 17 00:00:00 2001 From: Mist Date: Fri, 5 Jul 2024 16:14:30 +0800 Subject: [PATCH 1/4] [Refactor] separate calc_wh_with_min_width from calc_wh --- generator/src/code.rs | 12 +++++++++--- generator/src/components/breadcrumbs.rs | 4 ++-- generator/src/components/editor/code.rs | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/generator/src/code.rs b/generator/src/code.rs index 2b0849a..a666dcb 100644 --- a/generator/src/code.rs +++ b/generator/src/code.rs @@ -12,9 +12,6 @@ fn min_width(width: f32) -> f32 { } } -// Because the code block is input by users, we need to calculate the width and height -// to make sure render the width and height of the "editor" shape correctly -#[cached(key = "String", convert = r#"{ format!("{}", text) }"#)] pub fn calc_wh(text: &str, char_wdith: f32, line_height: f32) -> (f32, f32) { let trimmed_text = prepare_code(text); let lines = trimmed_text.lines(); @@ -28,6 +25,15 @@ pub fn calc_wh(text: &str, char_wdith: f32, line_height: f32) -> (f32, f32) { let width = max_length_line.len() as f32 * char_wdith; let height = lines.collect::>().len() as f32 * line_height; + (width, height) +} + +// Because the code block is input by users, we need to calculate the width and height +// to make sure render the width and height of the "editor" shape correctly +#[cached(key = "String", convert = r#"{ format!("{}", text) }"#)] +pub fn calc_wh_with_min_width(text: &str, char_wdith: f32, line_height: f32) -> (f32, f32) { + let (width, height) = calc_wh(text, char_wdith, line_height); + (min_width(width), height) } diff --git a/generator/src/components/breadcrumbs.rs b/generator/src/components/breadcrumbs.rs index a41d44b..94963d3 100644 --- a/generator/src/components/breadcrumbs.rs +++ b/generator/src/components/breadcrumbs.rs @@ -1,7 +1,7 @@ use cosmic_text::{Attrs, Color, Family}; use regex::Regex; -use crate::{code::calc_wh, edges::margin::Margin, text::FontRenderer}; +use crate::{code::calc_wh_with_min_width, edges::margin::Margin, text::FontRenderer}; use super::interface::{ component::Component, @@ -24,7 +24,7 @@ impl Component for Breadcrumbs { let style = RawComponentStyle::default(); if self.has_breadcrumbs { - let (w, h) = calc_wh(&self.path, 8., self.line_height); + let (w, h) = calc_wh_with_min_width(&self.path, 8., self.line_height); style.size(Size::Num(w), Size::Num(h)).margin(Margin { top: 5., diff --git a/generator/src/components/editor/code.rs b/generator/src/components/editor/code.rs index 7219d44..dfa62ec 100644 --- a/generator/src/components/editor/code.rs +++ b/generator/src/components/editor/code.rs @@ -1,5 +1,5 @@ use crate::{ - code::{calc_wh, prepare_code, CHAR_WIDTH}, + code::{calc_wh_with_min_width, prepare_code, CHAR_WIDTH}, components::interface::{ component::{Component, ComponentContext, RenderParams}, render_error, @@ -22,7 +22,7 @@ impl Component for Code { } fn style(&self) -> RawComponentStyle { - let (w, h) = calc_wh(&self.value, CHAR_WIDTH, self.line_height); + let (w, h) = calc_wh_with_min_width(&self.value, CHAR_WIDTH, self.line_height); Style::default().size(Size::Num(w), Size::Num(h)) } From 759cf46ca78964ca844202135fa594513eec7a34 Mon Sep 17 00:00:00 2001 From: Mist Date: Fri, 5 Jul 2024 16:25:19 +0800 Subject: [PATCH 2/4] [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), From 67e443b90007e90c39522e7f730edf66f7db6c06 Mon Sep 17 00:00:00 2001 From: Mist Date: Fri, 5 Jul 2024 16:52:15 +0800 Subject: [PATCH 3/4] [Fix] incorrect line number --- generator/src/code.rs | 7 +++++++ generator/src/copy_ascii.rs | 22 +++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/generator/src/code.rs b/generator/src/code.rs index a666dcb..e1f7560 100644 --- a/generator/src/code.rs +++ b/generator/src/code.rs @@ -12,6 +12,13 @@ fn min_width(width: f32) -> f32 { } } +pub fn calc_max_line_number_length(code_length: usize, start_line_number: usize) -> usize { + let max_line_number = code_length + start_line_number; + + // If code length is 1, the max_line_number will equal to start_line_number + (max_line_number - 1).to_string().len() +} + pub fn calc_wh(text: &str, char_wdith: f32, line_height: f32) -> (f32, f32) { let trimmed_text = prepare_code(text); let lines = trimmed_text.lines(); diff --git a/generator/src/copy_ascii.rs b/generator/src/copy_ascii.rs index e4ec80c..bddac0e 100644 --- a/generator/src/copy_ascii.rs +++ b/generator/src/copy_ascii.rs @@ -1,11 +1,13 @@ use std::cmp::max; -use crate::{code::calc_wh, config::TakeSnapshotParams}; +use crate::{ + code::{calc_max_line_number_length, 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 { @@ -17,10 +19,16 @@ fn optional(component: String, is_view: bool) -> String { #[allow(dead_code)] pub fn copy_ascii(params: TakeSnapshotParams) -> Result<()> { - let (width, _) = calc_wh(¶ms.code, 1., 1.); + let (width, height) = calc_wh(¶ms.code, 1., 1.); + let calc_line_number_width = + |start_line_number: usize| calc_max_line_number_length(height as usize, start_line_number); 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, + Some(start_line_number) => { + println!("{}", calc_line_number_width(start_line_number)); + + frame_width + SPACE_BOTH_SIDE + calc_line_number_width(start_line_number) + } None => frame_width, }; let line = format!("│{}│\n", "─".repeat(frame_width)); @@ -35,7 +43,11 @@ pub fn copy_ascii(params: TakeSnapshotParams) -> Result<()> { format!( "│ {:1$} │\n", match params.start_line_number { - Some(start_line_number) => format!(" {} {}", start_line_number + i, line), + Some(start_line_number) => format!( + "{:1$} {line}", + start_line_number + i, + calc_line_number_width(start_line_number), + ), None => line.to_string(), }, frame_width_with_content - 1 From eb84b4897c4631945de3585bba090f58a5184fff Mon Sep 17 00:00:00 2001 From: Mist Date: Fri, 5 Jul 2024 16:56:21 +0800 Subject: [PATCH 4/4] [Feat] remove unused logger --- generator/src/copy_ascii.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/generator/src/copy_ascii.rs b/generator/src/copy_ascii.rs index bddac0e..296437d 100644 --- a/generator/src/copy_ascii.rs +++ b/generator/src/copy_ascii.rs @@ -25,8 +25,6 @@ pub fn copy_ascii(params: TakeSnapshotParams) -> Result<()> { let frame_width = max(width as usize, params.file_path.len()) + SPACE_BOTH_SIDE; let frame_width = match params.start_line_number { Some(start_line_number) => { - println!("{}", calc_line_number_width(start_line_number)); - frame_width + SPACE_BOTH_SIDE + calc_line_number_width(start_line_number) } None => frame_width,