Skip to content

Commit

Permalink
support compress
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaoboy committed Oct 17, 2024
1 parent d724717 commit f4e17d9
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 20 deletions.
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 9 additions & 6 deletions ansi2-wasm/src-ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion ansi2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
14 changes: 8 additions & 6 deletions ansi2/src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ pub(crate) fn to_style(theme: impl ColorTable, ty: CssType, mode: Option<Mode>)
};

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");
Expand Down
9 changes: 8 additions & 1 deletion ansi2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct Args {

#[arg(long)]
font: Option<String>,

#[arg(short, long, default_value_t = false)]
compress: bool,
}

fn main() {
Expand Down Expand Up @@ -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);
}
6 changes: 3 additions & 3 deletions assets/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f4e17d9

Please sign in to comment.