Skip to content

Commit 6732346

Browse files
committed
perf: Avoid CLI output formatting when it is not needed
1 parent f0627cf commit 6732346

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
- Use `codegen-units=1` and `lto=fat`.
1717
- Reduce the number of allocations in CLI.
18+
- Avoid CLI output formatting when it is not needed.
1819

1920
## [0.3.3] - 2020-07-07
2021

src/main.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use rayon::prelude::*;
33
use std::borrow::Cow;
44
use std::error::Error;
55
use std::fs::File;
6-
use std::io::{self, Read};
6+
use std::io::{self, Read, Write};
77

8-
const VERSION: &str = env!("CARGO_PKG_VERSION");
9-
const HELP_MESSAGE: &str = concat!(
8+
const VERSION_MESSAGE: &[u8] = concat!("css-inline ", env!("CARGO_PKG_VERSION"), "\n").as_bytes();
9+
const HELP_MESSAGE: &[u8] = concat!(
1010
"css-inline ",
1111
env!("CARGO_PKG_VERSION"),
1212
r#"
@@ -42,12 +42,12 @@ OPTIONS:
4242
Whether remote stylesheets should be loaded or not.
4343
4444
--extra-css
45-
Additional CSS to inline."#
46-
);
45+
Additional CSS to inline.
46+
"#
47+
)
48+
.as_bytes();
4749

4850
struct Args {
49-
help: bool,
50-
version: bool,
5151
inline_style_tags: bool,
5252
remove_style_tags: bool,
5353
base_url: Option<String>,
@@ -66,24 +66,21 @@ fn parse_url(url: Option<String>) -> Result<Option<url::Url>, url::ParseError> {
6666

6767
fn main() -> Result<(), Box<dyn Error>> {
6868
let mut args = pico_args::Arguments::from_env();
69-
let args = Args {
70-
help: args.contains(["-h", "--help"]),
71-
version: args.contains(["-v", "--version"]),
72-
inline_style_tags: args
73-
.opt_value_from_str("--inline-style-tags")?
74-
.unwrap_or(true),
75-
remove_style_tags: args.contains("--remove-style-tags"),
76-
base_url: args.opt_value_from_str("--base-url")?,
77-
extra_css: args.opt_value_from_str("--extra-css")?,
78-
load_remote_stylesheets: args.contains("--load-remote-stylesheets"),
79-
files: args.free()?,
80-
};
81-
82-
if args.help {
83-
println!("{}", HELP_MESSAGE)
84-
} else if args.version {
85-
println!("css-inline {}", VERSION)
69+
if args.contains(["-h", "--help"]) {
70+
io::stdout().write_all(HELP_MESSAGE)?;
71+
} else if args.contains(["-v", "--version"]) {
72+
io::stdout().write_all(VERSION_MESSAGE)?;
8673
} else {
74+
let args = Args {
75+
inline_style_tags: args
76+
.opt_value_from_str("--inline-style-tags")?
77+
.unwrap_or(true),
78+
remove_style_tags: args.contains("--remove-style-tags"),
79+
base_url: args.opt_value_from_str("--base-url")?,
80+
extra_css: args.opt_value_from_str("--extra-css")?,
81+
load_remote_stylesheets: args.contains("--load-remote-stylesheets"),
82+
files: args.free()?,
83+
};
8784
let options = InlineOptions {
8885
inline_style_tags: args.inline_style_tags,
8986
remove_style_tags: args.remove_style_tags,

0 commit comments

Comments
 (0)