Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] support line number & remove unused module #91

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generator/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cached::proc_macro::cached;
use regex::Regex;

const MIN_WIDTH: f32 = 100.;
pub const CHAR_WIDTH: f32 = 9.05;

fn min_width(width: f32) -> f32 {
if width < MIN_WIDTH {
Expand Down
2 changes: 2 additions & 0 deletions generator/src/components.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod background;
pub mod breadcrumbs;
pub mod code_block;
pub mod container;
pub mod editor;
pub mod interface;
pub mod line_number;
pub mod rect;
pub mod watermark;
29 changes: 29 additions & 0 deletions generator/src/components/code_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::edges::margin::Margin;

use super::interface::{
component::Component,
style::{RawComponentStyle, Style},
};

pub struct CodeBlock {
children: Vec<Box<dyn Component>>,
}

impl Component for CodeBlock {
fn children(&self) -> &Vec<Box<dyn Component>> {
&self.children
}

fn style(&self) -> RawComponentStyle {
Style::default().margin(Margin {
top: 10.,
..Margin::default()
})
}
}

impl CodeBlock {
pub fn from_children(children: Vec<Box<dyn Component>>) -> CodeBlock {
CodeBlock { children }
}
}
12 changes: 3 additions & 9 deletions generator/src/components/editor/code.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::{
code::{calc_wh, prepare_code},
code::{calc_wh, prepare_code, CHAR_WIDTH},
components::interface::{
component::{Component, ComponentContext, RenderParams},
render_error,
style::{ComponentStyle, RawComponentStyle, Size, Style},
},
edges::margin::Margin,
highlight::Highlight,
text::FontRenderer,
};
Expand All @@ -23,14 +22,9 @@ impl Component for Code {
}

fn style(&self) -> RawComponentStyle {
let (w, h) = calc_wh(&self.value, 9.05, self.line_height);
let (w, h) = calc_wh(&self.value, CHAR_WIDTH, self.line_height);

Style::default()
.size(Size::Num(w), Size::Num(h))
.margin(Margin {
top: 10.,
..Margin::default()
})
Style::default().size(Size::Num(w), Size::Num(h))
}

fn draw_self(
Expand Down
99 changes: 99 additions & 0 deletions generator/src/components/line_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use super::interface::{
component::{Component, ComponentContext, RenderParams},
render_error,
style::{ComponentStyle, RawComponentStyle, Size, Style},
};
use crate::{code::CHAR_WIDTH, edges::margin::Margin, text::FontRenderer};
use cosmic_text::{Attrs, Color, Family};

const FONT_SIZE: f32 = 14.;

#[derive(Default)]
pub struct LineNumber {
children: Vec<Box<dyn Component>>,
line_height: f32,
render_condition: bool,
line_number_content: Vec<String>,
number_of_digit: usize,
}

impl Component for LineNumber {
fn render_condition(&self) -> bool {
return self.render_condition;
}

fn children(&self) -> &Vec<Box<dyn Component>> {
&self.children
}

fn style(&self) -> RawComponentStyle {
Style::default()
.size(
Size::Num(CHAR_WIDTH * self.number_of_digit as f32),
Size::Num(self.line_number_content.len() as f32 * self.line_height),
)
.margin(Margin {
right: 10.,
..Margin::default()
})
}

fn draw_self(
&self,
pixmap: &mut tiny_skia::Pixmap,
context: &ComponentContext,
render_params: &RenderParams,
style: &ComponentStyle,
) -> render_error::Result<()> {
FontRenderer::new(
FONT_SIZE,
self.line_height,
context.scale_factor,
&context.take_snapshot_params.fonts_folder,
)
.draw_text(
render_params.x,
render_params.y,
style.width,
style.height,
vec![(
&self.line_number_content.join("\n"),
Attrs::new()
.color(Color::rgb(73, 81, 98))
.family(Family::Name(&context.take_snapshot_params.code_font_family)),
)],
pixmap,
);

Ok(())
}
}

impl LineNumber {
pub fn new(content: &str, start_line_number: Option<usize>, line_height: f32) -> LineNumber {
match start_line_number {
None => LineNumber::default(),
Some(start_line_number) => {
let lines = content.split("\n").collect::<Vec<&str>>();
let max_line_number = lines.len() + start_line_number;
let number_of_digit = (max_line_number - 1).to_string().len();

LineNumber {
line_number_content: (start_line_number..max_line_number)
.map(|line_number| {
format!(
"{:>width$}",
line_number.to_string(),
width = number_of_digit
)
})
.collect::<Vec<String>>(),
number_of_digit,
children: vec![],
render_condition: true,
line_height,
}
}
}
}
}
1 change: 1 addition & 0 deletions generator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct TakeSnapshotParams {
pub file_path: String,
pub breadcrumbs_separator: String,
pub has_breadcrumbs: bool,
pub start_line_number: Option<usize>,
}

impl FromObject for TakeSnapshotParams {
Expand Down
7 changes: 6 additions & 1 deletion generator/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use tiny_skia::Pixmap;

use crate::components::background::Background;
use crate::components::breadcrumbs::Breadcrumbs;
use crate::components::code_block::CodeBlock;
use crate::components::container::Container;
use crate::components::editor::code::Code;
use crate::components::editor::mac_title_bar::MacTitleBar;
use crate::components::interface::component::ComponentContext;
use crate::components::interface::render_error;
use crate::components::line_number::LineNumber;
use crate::components::rect::Rect;
use crate::components::watermark::Watermark;
use crate::config::TakeSnapshotParams;
Expand All @@ -33,7 +35,10 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result<Pixmap>
params.breadcrumbs_separator,
params.has_breadcrumbs,
)),
Box::new(Code::new(params.code, 20., 15.)),
Box::new(CodeBlock::from_children(vec![
Box::new(LineNumber::new(&params.code, params.start_line_number, 20.)),
Box::new(Code::new(params.code, 20., 15.)),
])),
],
)),
Box::new(Watermark::new(params.watermark)),
Expand Down
61 changes: 0 additions & 61 deletions lua/codesnap/client.lua

This file was deleted.

2 changes: 2 additions & 0 deletions lua/codesnap/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ end

function config_module.get_config(extension)
local code = visual_utils.get_selected_text()
local start_line_number = visual_utils.get_start_line_number()

if string_utils.is_str_empty(code) then
error("No code is selected", 0)
Expand All @@ -41,6 +42,7 @@ function config_module.get_config(extension)
themes_folder = assets_folder .. "/themes",
theme = "base16-onedark",
file_path = static.config.has_breadcrumbs and path_utils.get_relative_path() 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)
Expand Down
1 change: 1 addition & 0 deletions lua/codesnap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function main.save_snapshot(extension)

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

Expand Down
1 change: 1 addition & 0 deletions lua/codesnap/static.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ return {
bg_theme = "default",
breadcrumbs_separator = "/",
has_breadcrumbs = false,
has_line_number = false,
},
cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))),
preview_switch = true,
Expand Down
11 changes: 0 additions & 11 deletions lua/codesnap/utils/logger.lua

This file was deleted.

5 changes: 4 additions & 1 deletion lua/codesnap/utils/visual.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local visual_utils = {}

-- Get all the lines from "from" to "to" and return them as a single string
-- If "from" and "to" are the same, return the line at "from"

local function get_whole_lines(from, to)
local lines = {}
if from == to then
Expand All @@ -15,6 +14,10 @@ local function get_whole_lines(from, to)
return table.concat(lines, "\n")
end

function visual_utils.get_start_line_number()
return vim.fn.line("'<")
end

function visual_utils.get_selected_text()
local selected_text = vim.fn.getline("'<", "'>")

Expand Down
Loading