Skip to content

Commit d5196b2

Browse files
committed
[Perf] improve the performance of code snapshot generation
1 parent b9b18e9 commit d5196b2

25 files changed

+296
-251
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/code.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use codesnap::{
1515

1616
use crate::{highlight::HighlightLineRange, range::Range, CLI, STDIN_CODE_DEFAULT_CHAR};
1717

18-
pub fn create_code(cli: &CLI, code: Code) -> anyhow::Result<Content> {
18+
pub fn create_code(cli: &CLI, code_config: Code) -> anyhow::Result<Content> {
1919
let code = match cli.execute[..] {
2020
[] => {
2121
let range = Range::from_opt_string(cli.range.clone())?;
@@ -27,8 +27,8 @@ pub fn create_code(cli: &CLI, code: Code) -> anyhow::Result<Content> {
2727
.start_line_number(parsed_range.0 as u32)
2828
.build()?;
2929

30-
code.file_path = cli.from_file.clone().or(code.file_path);
31-
code.language = cli.language.clone().or(code.language);
30+
code.file_path = cli.from_file.clone().or(code_config.file_path);
31+
code.language = cli.language.clone().or(code_config.language);
3232
code.highlight_lines = create_highlight_lines(&cli, parsed_range, &code_snippet)?;
3333

3434
Content::Code(code)

cli/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fs::{self, create_dir_all, read_to_string};
33

44
use crate::logger;
55
use anyhow::Context;
6-
use codesnap::config::{CodeSnap, SnapshotConfig};
6+
use codesnap::config::CodeSnap;
77

88
const DEFAULT_CONFIG_CONTENT: &'static str = include_str!("../config.json");
99

cli/src/logger.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ use std::process;
22

33
use ansi_term::{Colour, Style};
44

5-
fn pretty_print(bg: Colour, level: &str, content: &str) {
5+
fn pretty_print(color: Colour, level: &str, content: &str) {
66
println!(
77
"{} {}",
8-
Style::new()
9-
.on(bg)
10-
.fg(Colour::RGB(255, 255, 255))
11-
.paint(format!(" {} ", level)),
12-
Style::new().fg(bg).paint(format!(" {}", content))
8+
Style::new().fg(color).paint(format!("[{}]", level)),
9+
content
1310
);
1411
}
1512

cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fn output_snapshot(cli: &CLI, snapshot: &SnapshotConfig) -> anyhow::Result<Strin
264264
Ok(format!("Snapshot saved to {} successful!", cli.output))
265265
}
266266

267-
fn generate_snapshot_with_config(cli: &CLI, mut codesnap: CodeSnap) -> anyhow::Result<()> {
267+
fn generate_snapshot_with_config(cli: &CLI, codesnap: CodeSnap) -> anyhow::Result<()> {
268268
let snapshot = create_snapshot_config(&cli, codesnap)?;
269269
let snapshot_type = cli.r#type.clone();
270270

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ serde_json = "1.0.132"
2525
ansi_term = "0.12.1"
2626
strip-ansi-escapes = "0.2.1"
2727
cansi = "2.2.1"
28+
lazy_static = "1.5.0"
2829

core/src/ansi.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ use crate::utils::color::parse_hex_to_cosmic_color;
55

66
pub struct ANSI {
77
raw_text: String,
8+
font_family: String,
89
}
910

1011
impl ANSI {
11-
pub fn from(text: &str) -> Self {
12+
pub fn from(text: &str, font_family: String) -> Self {
1213
Self {
1314
raw_text: text.to_string(),
15+
font_family,
1416
}
1517
}
1618

@@ -25,7 +27,7 @@ impl ANSI {
2527
category.text,
2628
Attrs::new()
2729
.color(parse_hex_to_cosmic_color(hex_color))
28-
.family(Family::Name("Caskaydia Cove Nerd Font")),
30+
.family(Family::Name(self.font_family.as_str())),
2931
)
3032
})
3133
.collect::<Vec<(&str, Attrs)>>()

core/src/components/background.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ impl Background {
2727
}
2828

2929
impl Component for Background {
30+
fn name(&self) -> &'static str {
31+
"Background"
32+
}
33+
3034
fn children(&self) -> &Vec<Box<dyn Component>> {
3135
&self.children
3236
}

core/src/components/breadcrumbs.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::path::MAIN_SEPARATOR_STR;
22

3-
use cosmic_text::{Attrs, Family};
3+
use cosmic_text::{Attrs, Family, Metrics};
44
use regex::Regex;
55

66
use crate::{
7-
config,
87
edges::margin::Margin,
9-
utils::text::{create_file_system_by_fonts_folder, FontRenderer},
108
utils::{code::calc_wh_with_min_width, color::RgbaColor},
119
};
1210

@@ -24,6 +22,10 @@ pub struct Breadcrumbs {
2422
}
2523

2624
impl Component for Breadcrumbs {
25+
fn name(&self) -> &'static str {
26+
"Breadcrumbs"
27+
}
28+
2729
fn children(&self) -> &Vec<Box<dyn Component>> {
2830
&self.children
2931
}
@@ -59,7 +61,7 @@ impl Component for Breadcrumbs {
5961
pixmap: &mut tiny_skia::Pixmap,
6062
context: &super::interface::component::ComponentContext,
6163
render_params: &super::interface::component::RenderParams,
62-
style: &super::interface::style::ComponentStyle,
64+
_style: &super::interface::style::ComponentStyle,
6365
_parent_style: &ComponentStyle,
6466
) -> super::interface::render_error::Result<()> {
6567
let config = context.take_snapshot_params.code_config.breadcrumbs.clone();
@@ -79,17 +81,10 @@ impl Component for Breadcrumbs {
7981
&context.take_snapshot_params.code_config.font_family,
8082
));
8183

82-
FontRenderer::new(
83-
12.,
84-
LINE_HEIGHT,
85-
context.scale_factor,
86-
create_file_system_by_fonts_folder(&context.take_snapshot_params.fonts_folder),
87-
)
88-
.draw_text(
84+
context.font_renderer.lock().unwrap().draw_text(
8985
render_params.x,
9086
render_params.y,
91-
style.width,
92-
LINE_HEIGHT,
87+
Metrics::new(12., LINE_HEIGHT),
9388
vec![(&path, attrs)],
9489
pixmap,
9590
);

core/src/components/code_block.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ pub struct CodeBlock {
88
}
99

1010
impl Component for CodeBlock {
11+
fn name(&self) -> &'static str {
12+
"CodeBlock"
13+
}
14+
1115
fn children(&self) -> &Vec<Box<dyn Component>> {
1216
&self.children
1317
}

0 commit comments

Comments
 (0)