Skip to content

Commit 76f9a62

Browse files
authored
Add color to error and mismatch messages (#42)
1 parent 71c3d42 commit 76f9a62

File tree

7 files changed

+118
-21
lines changed

7 files changed

+118
-21
lines changed

Cargo.lock

Lines changed: 23 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ clap = { version = "4.5.16", features = ["derive"] }
1919
clap_mangen = "0.2.23"
2020
indicatif = "0.17.8"
2121
lexiclean = "0.0.1"
22+
owo-colors = "4"
2223
serde = { version = "1.0.209", features = ["derive"] }
2324
serde_json = "1.0.127"
2425
snafu = "0.8.4"

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(crate) enum Error {
7575
backtrace: Option<Backtrace>,
7676
path: DisplayPath,
7777
},
78-
#[snafu(context(false))]
78+
#[snafu(transparent)]
7979
WalkDir {
8080
backtrace: Option<Backtrace>,
8181
source: walkdir::Error,

src/main.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ use {
22
self::{
33
arguments::Arguments, display_path::DisplayPath, entry::Entry, error::Error, hash::Hash,
44
lint::Lint, lint_group::LintGroup, list::List, manifest::Manifest, options::Options,
5-
relative_path::RelativePath, subcommand::Subcommand,
5+
owo_colorize_ext::OwoColorizeExt, relative_path::RelativePath, style::Style,
6+
subcommand::Subcommand,
67
},
78
blake3::Hasher,
89
camino::{Utf8Component, Utf8Path, Utf8PathBuf},
910
clap::{Parser, ValueEnum},
1011
indicatif::{ProgressBar, ProgressStyle},
1112
lexiclean::Lexiclean,
13+
owo_colors::Styled,
1214
serde::{Deserialize, Deserializer, Serialize, Serializer},
1315
snafu::{ensure, ErrorCompat, OptionExt, ResultExt, Snafu},
1416
std::{
@@ -17,7 +19,7 @@ use {
1719
env,
1820
fmt::{self, Display, Formatter},
1921
fs::{self, File},
20-
io,
22+
io::{self, IsTerminal},
2123
path::{Path, PathBuf},
2224
process,
2325
str::{self, FromStr},
@@ -35,15 +37,22 @@ mod lint_group;
3537
mod list;
3638
mod manifest;
3739
mod options;
40+
mod owo_colorize_ext;
3841
mod progress_bar;
3942
mod relative_path;
43+
mod style;
4044
mod subcommand;
4145

4246
type Result<T = (), E = Error> = std::result::Result<T, E>;
4347

4448
fn main() {
4549
if let Err(err) = Arguments::parse().run() {
46-
eprintln!("error: {err}");
50+
let style = Style::stderr();
51+
eprintln!(
52+
"{}: {}",
53+
"error".style(style.error()),
54+
err.style(style.message()),
55+
);
4756

4857
let causes = err.iter_chain().skip(1).count();
4958

src/owo_colorize_ext.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use super::*;
2+
3+
pub(crate) trait OwoColorizeExt {
4+
fn style(&self, style: Style) -> Styled<&Self>;
5+
}
6+
7+
impl<T: owo_colors::OwoColorize> OwoColorizeExt for T {
8+
fn style(&self, style: Style) -> Styled<&Self> {
9+
self.style(style.effective_style())
10+
}
11+
}

src/style.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use super::*;
2+
3+
#[derive(Clone, Copy)]
4+
pub(crate) struct Style {
5+
is_terminal: bool,
6+
inner: owo_colors::Style,
7+
}
8+
9+
impl Style {
10+
pub(crate) fn bad(self) -> Self {
11+
Self {
12+
inner: self.inner.red(),
13+
..self
14+
}
15+
}
16+
17+
pub(crate) fn error(self) -> Self {
18+
Self {
19+
inner: self.inner.red(),
20+
..self
21+
}
22+
}
23+
24+
pub(crate) fn good(self) -> Self {
25+
Self {
26+
inner: self.inner.green(),
27+
..self
28+
}
29+
}
30+
31+
pub(crate) fn effective_style(self) -> owo_colors::Style {
32+
if self.is_terminal {
33+
self.inner
34+
} else {
35+
owo_colors::Style::default()
36+
}
37+
}
38+
39+
pub(crate) fn message(self) -> Self {
40+
Self {
41+
inner: self.inner.bold(),
42+
..self
43+
}
44+
}
45+
46+
pub(crate) fn stderr() -> Self {
47+
Self {
48+
is_terminal: io::stderr().is_terminal(),
49+
inner: owo_colors::Style::default(),
50+
}
51+
}
52+
}

src/subcommand/verify.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,29 @@ impl Verify {
7070

7171
if !mismatches.is_empty() {
7272
for (path, (actual, expected)) in &mismatches {
73+
let style = Style::stderr();
74+
75+
let hash_style = if expected.hash == actual.hash {
76+
style.good()
77+
} else {
78+
style.bad()
79+
};
80+
81+
let size_style = if expected.size == actual.size {
82+
style.good()
83+
} else {
84+
style.bad()
85+
};
86+
7387
eprintln!(
7488
"\
7589
mismatched file: `{path}`
7690
manifest: {} ({} bytes)
7791
file: {} ({} bytes)",
78-
expected.hash, expected.size, actual.hash, actual.size,
92+
expected.hash.style(style.good()),
93+
expected.size.style(style.good()),
94+
actual.hash.style(hash_style),
95+
actual.size.style(size_style),
7996
);
8097
}
8198

0 commit comments

Comments
 (0)