Skip to content

Commit

Permalink
minify css
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaoboy committed Oct 18, 2024
1 parent 7ead167 commit ef08229
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 67 deletions.
51 changes: 10 additions & 41 deletions ansi2/src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,28 @@ pub(crate) fn to_style(
let light_color_css: String = light_colors
.iter()
.fold(String::new(), |mut acc, (name, c)| {
acc.push_str(&format!(".{name}{{ {color_field}: {};}} ", c));
acc.push_str(&format!(".{name}{{{color_field}:{};}}", c));
acc
});

let bg_light_color_css: String =
light_colors
.iter()
.fold(String::new(), |mut acc, (name, c)| {
acc.push_str(&format!(".bg-{name}{{ {bg_field}: {};}} ", c));
acc.push_str(&format!(".bg-{name}{{{bg_field}:{};}} ", c));
acc
});
let dark_color_css: String = dark_colors
.iter()
.fold(String::new(), |mut acc, (name, c)| {
acc.push_str(&format!(".{name}{{ {color_field}: {};}} ", c));
acc.push_str(&format!(".{name}{{{color_field}:{};}}", c));
acc
});

let bg_dark_color_css: String = dark_colors
.iter()
.fold(String::new(), |mut acc, (name, c)| {
acc.push_str(&format!(".bg-{name}{{ {bg_field}: {};}} ", c));
acc.push_str(&format!(".bg-{name}{{{bg_field}:{};}}", c));
acc
});

Expand Down Expand Up @@ -144,14 +144,7 @@ pub(crate) fn to_style(
};

let style = format!(
r#"
{root_style}
{default_text_style}
{common_style}
{color_css}
{bg_color_css}
{color256_str}
"#
r#"{root_style}{default_text_style}{common_style}{color_css}{bg_color_css}{color256_str}"#
)
.trim()
.to_string();
Expand All @@ -170,39 +163,15 @@ pub(crate) fn to_style(
};

let root_css = format!(
r#"
:root {{color-scheme: light dark; background-color: {light_bg_color}}}
{light_color_css}
{bg_light_color_css}
{default_light_text_style}
"#
r#":root {{color-scheme: light dark; background-color: {light_bg_color}}}{light_color_css}{bg_light_color_css}{default_light_text_style}"#
);

let dark_css = format!(
r#"
@media (prefers-color-scheme: dark) {{
:root {{background-color: {dark_bg_color}}}
{dark_color_css}
{bg_dark_color_css}
{default_dark_text_style}
}}
"#
)
r#"@media (prefers-color-scheme: dark) {{:root {{background-color: {dark_bg_color}}}{dark_color_css}{bg_dark_color_css}{default_dark_text_style}}}"#)
.trim()
.to_string();

format!(
r#"
{root_css}
{dark_css}
{common_style}
{color256_str}
"#,
)
.trim()
.to_string()
format!(r#"{root_css}{dark_css}{common_style}{color256_str}"#,)
.trim()
.to_string()
}
27 changes: 10 additions & 17 deletions ansi2/src/html.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;

use crate::{
css::{to_style, CssType, Mode},
css::{get_hex, to_style, CssType, Mode},
theme::ColorTable,
Canvas,
};
Expand All @@ -25,14 +25,8 @@ pub fn to_html<S: AsRef<str>>(
if let Some(url) = font {
if url.starts_with("http") || url.starts_with("data:font;base64") {
font_family = "ansi2-custom-font".into();
font_style = format!(
r#"
@font-face {{
font-family: ansi2-custom-font;
src: url({url});
}}
"#
)
font_style =
format!(r#"@font-face {{font-family: ansi2-custom-font;src: url({url});}}"#)
} else {
font_family = url;
}
Expand Down Expand Up @@ -66,7 +60,10 @@ pub fn to_html<S: AsRef<str>>(
text_class.push(name);

if let crate::lex::AnsiColor::Rgb(r, g, b) = c.color {
color256.insert(format!(".rgb_{r}_{g}_{b}{{ color: rgb({r},{g},{b}) ;}}\n"));
color256.insert(format!(
".rgb_{r}_{g}_{b}{{color:{};}}\n",
get_hex((r, g, b))
));
}
}

Expand All @@ -76,7 +73,8 @@ pub fn to_html<S: AsRef<str>>(

if let crate::lex::AnsiColor::Rgb(r, g, b) = c.color {
color256.insert(format!(
".bg-rgb_{r}_{g}_{b}{{ background: rgb({r},{g},{b}) ;}}\n"
".bg-rgb_{r}_{g}_{b}{{background:{};}}\n",
get_hex((r, g, b))
));
}
}
Expand Down Expand Up @@ -112,12 +110,7 @@ pub fn to_html<S: AsRef<str>>(
{color256_str}
.ansi-main{{display:flex;flex-direction:column;}}
.row{{display: flex;}}
.char{{
margin: 0;
padding: 0;
font-family: {font_family};
white-space: pre;
}}</style></head><body>{s}</body></html>
.char{{margin: 0;padding: 0;font-family: {font_family};white-space: pre;}}</style></head><body>{s}</body></html>
"#
)
}
10 changes: 2 additions & 8 deletions ansi2/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@ pub fn to_svg<S: AsRef<str>>(
if let Some(url) = font {
if url.starts_with("http") || url.starts_with("data:font;base64") {
font_family = "ansi2-custom-font".into();
font_style = format!(
r#"
@font-face {{
font-family: ansi2-custom-font;
src: url({url});
}}
"#
)
font_style =
format!(r#"@font-face {{font-family: ansi2-custom-font;src: url({url});}}"#)
} else {
font_family = url;
}
Expand Down
2 changes: 1 addition & 1 deletion lefthook.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pre-commit:
commands:
check:
glob: "*.{js,ts,json,tsx}"
glob: "*.{js,ts,json,tsx,rs,toml}"
run: npm run pre-check && git add {staged_files}

0 comments on commit ef08229

Please sign in to comment.