From f4e17d9367fb27d55af9c34366bd74748dd76721 Mon Sep 17 00:00:00 2001 From: ahaoboy <504595380@qq.com> Date: Thu, 17 Oct 2024 18:43:27 +0800 Subject: [PATCH] support compress --- Cargo.lock | 52 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 ++-- ansi2-wasm/src-ts/cli.ts | 15 +++++++----- ansi2/Cargo.toml | 3 ++- ansi2/src/css.rs | 14 ++++++----- ansi2/src/main.rs | 9 ++++++- assets/update.sh | 6 ++--- lefthook.yml | 2 +- package.json | 1 + readme.md | 9 +++++++ 10 files changed, 96 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99db184..94aa971 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,6 +10,7 @@ dependencies = [ "clap", "html-escape", "nom", + "osvg", "wasm-bindgen", ] @@ -71,6 +72,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "anyhow" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" + [[package]] name = "base64" version = "0.22.1" @@ -228,6 +235,17 @@ version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +[[package]] +name = "osvg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0b52ecc70bdb0be07dc95542d95fc1031489aaad513cd101f05edcde95730d1" +dependencies = [ + "anyhow", + "clap", + "rquickjs", +] + [[package]] name = "proc-macro2" version = "1.0.88" @@ -246,6 +264,40 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "relative-path" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2" + +[[package]] +name = "rquickjs" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cbd33e0b668aea0ab238b9164523aca929096f9f40834700d71d91dd4888882" +dependencies = [ + "rquickjs-core", +] + +[[package]] +name = "rquickjs-core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e9129d69b7b8f7ee8ad1da5b12c7f4a8a8acd45f2e6dd9cb2ee1bc5a1f2fa3d" +dependencies = [ + "relative-path", + "rquickjs-sys", +] + +[[package]] +name = "rquickjs-sys" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf6f2288d8e7fbb5130f62cf720451641e99d55f6fde9db86aa2914ecb553fd2" +dependencies = [ + "cc", +] + [[package]] name = "same-file" version = "1.0.6" diff --git a/Cargo.toml b/Cargo.toml index 80141c5..c17dcd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,11 @@ homepage = "https://github.com/ahaoboy/ansi2" authors = ["ahaoboy"] [workspace.dependencies] -clap = { version = "4.5.20", features = ["derive"] } +clap = { version = "4.5.20", features = ["derive"] } wasm-bindgen = { version = "0.2.95" } nom = "7.1.3" html-escape = "0.2" base64 = "0.22.1" wasm-bindgen-test = "0.3.45" -ansi2 = { path = "./ansi2", features = ["wasm"] } +ansi2 = { path = "./ansi2", features = ["wasm"] } +osvg = "0.1.0" diff --git a/ansi2-wasm/src-ts/cli.ts b/ansi2-wasm/src-ts/cli.ts index f77a974..780d095 100644 --- a/ansi2-wasm/src-ts/cli.ts +++ b/ansi2-wasm/src-ts/cli.ts @@ -58,11 +58,12 @@ async function main() { const input = await readToString() program - .option("--format [type]", "output format", "svg") - .option("--theme [type]", "color theme", "vscode") - .option("--width [type]", "width", undefined) + .option("-f, --format [type]", "output format", "svg") + .option("-t, --theme [type]", "color theme", "vscode") + .option("-w, --width [type]", "width", undefined) .option("--font [type]", "font", undefined) - .option("--mode [type]", "mode", undefined) + .option("-m, --mode [type]", "mode", undefined) + .option("-c, --compress [type]", "compress", undefined) program.parse() @@ -74,11 +75,13 @@ async function main() { typeof options.width === "undefined" ? undefined : +options.width const font = typeof options.font === "undefined" ? undefined : getFontUrl(options.font) + + const compress = options.compress === "undefined" ? false : options.compress switch (format) { case "svg": { const s = to_svg(input, theme, width, font, mode) - const result = optimize(s) - process.stdout.write(result.data) + const result = compress ? optimize(s).data : s + process.stdout.write(result) break } case "html": { diff --git a/ansi2/Cargo.toml b/ansi2/Cargo.toml index eb7abad..3dad349 100644 --- a/ansi2/Cargo.toml +++ b/ansi2/Cargo.toml @@ -13,12 +13,13 @@ wasm-bindgen = { workspace = true, optional = true } nom = { workspace = true } html-escape = { workspace = true } base64 = { workspace = true } +osvg = { workspace = true, optional = true } [profile.release] opt-level = 3 [features] -cli = ["clap"] +cli = ["clap", "osvg"] wasm = ["wasm-bindgen"] [[bin]] diff --git a/ansi2/src/css.rs b/ansi2/src/css.rs index d54a653..bb1eb4c 100644 --- a/ansi2/src/css.rs +++ b/ansi2/src/css.rs @@ -28,15 +28,17 @@ pub(crate) fn to_style(theme: impl ColorTable, ty: CssType, mode: Option) }; let mut color256 = Vec::new(); - for i in 0..256 { - let (r, g, b) = COLOR256[i]; - color256.push(format!(".color256_{i}{{ {color_field}: rgb({r},{g},{b}) ;}}")); + for (i, (r, g, b)) in COLOR256.iter().enumerate() { + color256.push(format!( + ".color256_{i}{{ {color_field}: rgb({r},{g},{b}) ;}}" + )); } let mut color256bg = Vec::new(); - for i in 0..256 { - let (r, g, b) = COLOR256[i]; - color256bg.push(format!(".color256_bg_{i}{{ {bg_field}: rgb({r},{g},{b}) ;}}")); + for (i, (r, g, b)) in COLOR256.iter().enumerate() { + color256bg.push(format!( + ".color256_bg_{i}{{ {bg_field}: rgb({r},{g},{b}) ;}}" + )); } let color256_str = color256.join("\n") + &color256bg.join("\n"); diff --git a/ansi2/src/main.rs b/ansi2/src/main.rs index 12b11ae..54bf446 100644 --- a/ansi2/src/main.rs +++ b/ansi2/src/main.rs @@ -30,6 +30,9 @@ struct Args { #[arg(long)] font: Option, + + #[arg(short, long, default_value_t = false)] + compress: bool, } fn main() { @@ -62,11 +65,15 @@ fn main() { }); let s = String::from_utf8_lossy(&buf); - let output = match format { + let mut output = match format { Format::Svg => to_svg(s, theme, width, base64, mode), Format::Html => to_html(&s, theme, width, base64, mode), Format::Text => to_text(&s, width), }; + if args.compress { + output = osvg::osvg(&output).expect("compress error"); + } + println!("{}", output); } diff --git a/assets/update.sh b/assets/update.sh index e92555d..a75fe62 100755 --- a/assets/update.sh +++ b/assets/update.sh @@ -2,8 +2,8 @@ for i in win11 vitest 8bit-color 24bit-color nu-ls do - cat "$i.ans" | node ../ansi2-wasm/bin/cli.js > "$i.svg" - cat "$i.ans" | node ../ansi2-wasm/bin/cli.js --mode=light > "$i-light.svg" - cat "$i.ans" | node ../ansi2-wasm/bin/cli.js --mode=dark > "$i-dark.svg" + 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" echo "$i done" done \ No newline at end of file diff --git a/lefthook.yml b/lefthook.yml index 78d0a68..5fe50d8 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -2,4 +2,4 @@ pre-commit: commands: check: glob: "*.{js,ts,json,tsx}" - run: cargo clippy --fix --allow-dirty --allow-staged && npm run format && npm run lint:fix && git add {staged_files} + run: npm run pre-check && git add {staged_files} diff --git a/package.json b/package.json index 0838c3a..65d38f4 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.0.0", "private": true, "scripts": { + "pre-check": "cargo clippy --fix --allow-dirty --allow-staged && npm run format && npm run lint:fix", "format": "biome format --write ./", "lint:fix": "biome lint ./ --write --unsafe" }, diff --git a/readme.md b/readme.md index 7075f80..fe06309 100644 --- a/readme.md +++ b/readme.md @@ -69,6 +69,15 @@ dark / light neofetch | ansi2 --format=svg --mode=dark > neofetch.svg ``` +### compress + +Compressing using [osvg](https://github.com/ahaoboy/osvg) and [svgo](https://github.com/svg/svgo), this will increase the running time by several seconds, but can save half of the storage space. + +```bash +neofetch | ansi2 --format=svg --compress > neofetch.svg +neofetch | ansi2 --format=svg -c > neofetch.svg +``` + ## example ### neofetch