Skip to content

Commit

Permalink
[Feat] support take code snapshot as ascii art
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Jul 5, 2024
1 parent b9653c6 commit 759cf46
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
55 changes: 55 additions & 0 deletions generator/src/copy_ascii.rs
Original file line number Diff line number Diff line change
@@ -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(&params.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::<String>();
let text_line = |text: &str| format!("│ {:1$}│\n", text, frame_width_with_content);
let breadcrumbs = optional(
format!("{}{line}", text_line(&params.file_path)),
params.has_breadcrumbs,
);
let ascii_snapshot = format!("{top_frame}{breadcrumbs}{code}{bottom_frame}");

Clipboard::new().unwrap().set_text(ascii_snapshot).unwrap();

Ok(())
}
3 changes: 3 additions & 0 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod color;
mod components;
mod config;
mod copy;
mod copy_ascii;
mod edges;
mod highlight;
mod path;
Expand All @@ -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;

Expand All @@ -22,5 +24,6 @@ fn generator() -> Result<Dictionary> {
Function::from_fn(copy_into_clipboard),
),
("save_snapshot", Function::from_fn(save_snapshot)),
("copy_ascii", Function::from_fn(copy_ascii)),
]))
}
7 changes: 7 additions & 0 deletions lua/codesnap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions plugin/codesnap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 759cf46

Please sign in to comment.