Skip to content

Commit

Permalink
Replace colored by anstream and anstyle (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Oct 18, 2024
1 parent 2f8e07b commit d9ad41f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 118 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## 2024-10-18 - [0.1.5]

### Enhancements

- Reduce distributed binary sizes ~80%.
- Drop `clap-derive` dependency.
- Drop `colored` dependency.

## 2024-09-29 - [0.1.4]

### New features
Expand Down Expand Up @@ -34,6 +42,7 @@

First beta release

[0.1.5]: https://github.com/mondeja/hledger-fmt/compare/v0.1.4...master
[0.1.4]: https://github.com/mondeja/hledger-fmt/compare/v0.1.3...v0.1.4
[0.1.3]: https://github.com/mondeja/hledger-fmt/compare/v0.1.2...v0.1.3
[0.1.2]: https://github.com/mondeja/hledger-fmt/compare/v0.1.1...v0.1.2
Expand Down
105 changes: 12 additions & 93 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hledger-fmt"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
description = "An opinionated hledger's journal files formatter."
repository = "https://github.com/mondeja/hledger-fmt"
Expand Down Expand Up @@ -29,11 +29,12 @@ name = "hledger-fmt"
clap = { version = "4", default-features = false, features = ["std", "help"] }
walkdir = "2"
similar = "2"
colored = { version = "2", optional = true }
anstream = { version = "0.6", optional = true }
anstyle = { version = "1", optional = true }

[features]
default = ["color"]
color = ["dep:colored", "clap/color"]
color = ["dep:anstream", "dep:anstyle", "clap/color"]

[profile.release]
strip = true
Expand Down
57 changes: 35 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
#[cfg(feature = "color")]
use colored::Colorize;
use similar::{ChangeTag, TextDiff};
use std::ffi::OsStr;
use walkdir::WalkDir;
Expand Down Expand Up @@ -209,28 +207,43 @@ fn main() {
let diff = TextDiff::from_lines(&content, &formatted);
for change in diff.iter_all_changes() {
#[cfg(feature = "color")]
let line = if no_color {
match change.tag() {
ChangeTag::Delete => format!("- {change}").normal(),
ChangeTag::Insert => format!("+ {change}").normal(),
ChangeTag::Equal => format!(" {change}").normal(),
}
} else {
match change.tag() {
ChangeTag::Delete => format!("- {change}").bright_red(),
ChangeTag::Insert => format!("+ {change}").bright_green(),
ChangeTag::Equal => format!(" {change}").dimmed(),
}
};
{
let line = if no_color {
match change.tag() {
ChangeTag::Delete => format!("- {change}"),
ChangeTag::Insert => format!("+ {change}"),
ChangeTag::Equal => format!(" {change}"),
}
} else {
match change.tag() {
ChangeTag::Delete => {
let bright_red = anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::BrightRed.into()));
format!("{bright_red}- {change}{bright_red:#}")
}
ChangeTag::Insert => {
let bright_green = anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::BrightGreen.into()));
format!("{bright_green}+ {change}{bright_green:#}")
}
ChangeTag::Equal => {
let dimmed = anstyle::Style::new().dimmed();
format!("{dimmed} {change}{dimmed:#}")
}
}
};
anstream::eprint!("{line}");
}

#[cfg(not(feature = "color"))]
let line = match change.tag() {
ChangeTag::Delete => format!("- {change}"),
ChangeTag::Insert => format!("+ {change}"),
ChangeTag::Equal => format!(" {change}"),
};

eprint!("{line}");
{
let line = match change.tag() {
ChangeTag::Delete => format!("- {change}"),
ChangeTag::Insert => format!("+ {change}"),
ChangeTag::Equal => format!(" {change}"),
};
eprint!("{line}");
}
}
}
}
Expand Down

0 comments on commit d9ad41f

Please sign in to comment.