Skip to content

Commit

Permalink
ansi: fix bg-color
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaoboy committed Oct 21, 2024
1 parent 89aa925 commit 5242c5a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
5 changes: 3 additions & 2 deletions ansi-preview/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"module": "Node16",
"target": "ES2022",
// "rootDir": "src",
"rootDir": "src",
"useDefineForClassFields": true,
"lib": ["ESNext", "ES2022", "es2021", "DOM"],
"allowJs": true,
Expand All @@ -19,5 +19,6 @@
"declaration": true,
"declarationMap": false,
"sourceMap": false
}
},
"include": ["src"]
}
11 changes: 3 additions & 8 deletions ansi2/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub fn to_html<S: AsRef<str>>(
s.push_str("<div class='row'>");
for c in row.iter() {
let mut text_class = vec!["char".into()];
let mut bg_class = vec!["char".into()];
if c.bold {
text_class.push("bold".into());
}
Expand Down Expand Up @@ -69,9 +68,8 @@ pub fn to_html<S: AsRef<str>>(

if !c.bg_color.is_default() {
let name = "bg-".to_string() + &c.bg_color.class_name();
bg_class.push(name);

if let crate::lex::AnsiColor::Rgb(r, g, b) = c.color {
text_class.push(name);
if let crate::lex::AnsiColor::Rgb(r, g, b) = c.bg_color {
color256.insert(format!(
".bg-rgb_{r}_{g}_{b}{{background:{};}}\n",
get_hex((r, g, b))
Expand All @@ -84,17 +82,14 @@ pub fn to_html<S: AsRef<str>>(
}

let text_class = text_class.join(" ").trim().to_string();
let bg_class = bg_class.join(" ");
let html_char = c.char.to_string();
let html_char = html_escape::encode_text(&html_char);
let class_str = if text_class.is_empty() {
String::new()
} else {
format!("class='{text_class}'")
};
s.push_str(&format!(
"<div class='{bg_class}'><div {class_str}>{html_char}</div></div>",
))
s.push_str(&format!("<div {class_str}>{html_char}</div>",))
}
s.push_str("</div>");
}
Expand Down
2 changes: 1 addition & 1 deletion ansi2/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn to_svg<S: AsRef<str>, T: ColorTable>(
r#"<rect x="{cur_x}px" y="{cur_y}px" width="{fn_w}px" height="{fn_h}px" {class_str}/>"#
));

if let crate::lex::AnsiColor::Rgb(r, g, b) = c.color {
if let crate::lex::AnsiColor::Rgb(r, g, b) = c.bg_color {
color256.insert(format!(
".bg-rgb_{r}_{g}_{b}{{fill:{};}}\n",
get_hex((r, g, b))
Expand Down
6 changes: 5 additions & 1 deletion assets/update.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/bin/bash

for i in win11 vitest 8bit-color 24bit-color nu-ls ansi-default sample colortable hyperlink-demo
for i in win11 vitest 8bit-color 24bit-color nu-ls ansi-default colortable hyperlink-demo
do
cat "$i.ans" | node ../ansi2-wasm/bin/cli.js -c > "$i.svg"
cat "$i.ans" | node ../ansi2-wasm/bin/cli.js --mode=light -c > "$i-light.svg"
cat "$i.ans" | node ../ansi2-wasm/bin/cli.js --mode=dark -c > "$i-dark.svg"

cat "$i.ans" | node ../ansi2-wasm/bin/cli.js -c -f html > "$i.html"
cat "$i.ans" | node ../ansi2-wasm/bin/cli.js --mode=light -c -f html > "$i-light.html"
cat "$i.ans" | node ../ansi2-wasm/bin/cli.js --mode=dark -c -f html > "$i-dark.html"
echo "$i done"
done

0 comments on commit 5242c5a

Please sign in to comment.