Skip to content

Commit

Permalink
refactor: replace owo-colors with anstream/anstyle
Browse files Browse the repository at this point in the history
The anstream and anstyle crates look more promising and additionally
allow to expose the on the API if needed, without restricting to a
specific terminal coloring crate.

Also, the owo-colors crate contained some unmaintained dependencies.
  • Loading branch information
dnaka91 committed Jan 8, 2024
1 parent 8b79f6f commit e831c05
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 70 deletions.
43 changes: 6 additions & 37 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pedantic = "warn"
clone_on_ref_ptr = "warn"

[workspace.dependencies]
anstream = "0.6.5"
anstyle = "1.0.4"
anyhow = "1.0.79"
clap = { version = "4.4.12", features = ["derive", "wrap_help"] }
color-eyre = { version = "0.6.2", default-features = false }
Expand All @@ -31,7 +33,6 @@ indoc = "2.0.4"
insta = { version = "1.34.0", features = ["glob"] }
miette = "5.10.0"
mimalloc = "0.1.39"
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
proc-macro2 = { version = "1.0.75", default-features = false }
quote = { version = "1.0.35", default-features = false }
serde = { version = "1.0.195", features = ["derive"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/mabo-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ repository.workspace = true
license.workspace = true

[dependencies]
anstream.workspace = true
anstyle.workspace = true
miette.workspace = true
owo-colors.workspace = true
schemars = { version = "0.8.16", optional = true }
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
Expand Down
35 changes: 22 additions & 13 deletions crates/mabo-compiler/src/highlight.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
use std::fmt::Display;

use owo_colors::OwoColorize;
use anstream::ColorChoice;
use anstyle::{AnsiColor, Color, Style};

pub fn sample(value: impl Display) -> String {
if cfg!(feature = "debug") {
format!("❬B❭{value}❬B❭")
} else {
value.bright_blue().to_string()
}
render(value, 'B', AnsiColor::BrightBlue)
}

pub fn value(value: impl Display) -> String {
if cfg!(feature = "debug") {
format!("❬Y❭{value}❬Y❭")
} else {
value.bright_yellow().to_string()
}
render(value, 'Y', AnsiColor::BrightYellow)
}

pub fn focus(value: impl Display) -> String {
render(value, 'W', AnsiColor::BrightWhite)
}

#[inline]
fn render(value: impl Display, debug_char: char, color: AnsiColor) -> String {
if cfg!(feature = "debug") {
format!("❬W❭{value}❬W❭")
format!("❬{debug_char}❭{value}❬{debug_char}❭")
} else if use_ansi() {
let style = Style::new().fg_color(Some(Color::Ansi(color)));
format!("{}{value}{}", style.render(), style.render_reset())
} else {
value.bright_white().to_string()
value.to_string()
}
}

fn use_ansi() -> bool {
match anstream::stderr().current_choice() {
ColorChoice::Auto => anstream::stderr().is_terminal(),
ColorChoice::AlwaysAnsi | ColorChoice::Always => true,
ColorChoice::Never => false,
}
}
3 changes: 2 additions & 1 deletion crates/mabo-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ license.workspace = true
rustc-args = ["--cfg", "docsrs"]

[dependencies]
anstream.workspace = true
anstyle.workspace = true
miette.workspace = true
owo-colors.workspace = true
mabo-derive = { path = "../mabo-derive" }
winnow = "0.5.32"

Expand Down
33 changes: 21 additions & 12 deletions crates/mabo-parser/src/highlight.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
use std::fmt::Display;

use owo_colors::{OwoColorize, Stream};
use anstream::ColorChoice;
use anstyle::{AnsiColor, Color, Style};

pub fn sample(value: impl Display) -> String {
if cfg!(feature = "debug") {
format!("❬B❭{value}❬B❭")
} else {
value
.if_supports_color(Stream::Stderr, OwoColorize::bright_blue)
.to_string()
}
render(value, 'B', AnsiColor::BrightBlue)
}

pub fn value(value: impl Display) -> String {
render(value, 'Y', AnsiColor::BrightYellow)
}

#[inline]
fn render(value: impl Display, debug_char: char, color: AnsiColor) -> String {
if cfg!(feature = "debug") {
format!("❬Y❭{value}❬Y❭")
format!("❬{debug_char}❭{value}❬{debug_char}❭")
} else if use_ansi() {
let style = Style::new().fg_color(Some(Color::Ansi(color)));
format!("{}{value}{}", style.render(), style.render_reset())
} else {
value
.if_supports_color(Stream::Stderr, OwoColorize::bright_yellow)
.to_string()
value.to_string()
}
}

fn use_ansi() -> bool {
match anstream::stderr().current_choice() {
ColorChoice::Auto => anstream::stderr().is_terminal(),
ColorChoice::AlwaysAnsi | ColorChoice::Always => true,
ColorChoice::Never => false,
}
}
5 changes: 0 additions & 5 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
all-features = true

[advisories]
ignore = ["RUSTSEC-2021-0145"]

[licenses]
allow-osi-fsf-free = "both"
exceptions = [
Expand All @@ -13,8 +10,6 @@ exceptions = [
[bans]
skip = [
{ name = "bitflags", version = "1" },
{ name = "hermit-abi", version = "0.1" },
{ name = "supports-color", version = "1" },
{ name = "syn", version = "1" },
{ name = "terminal_size", version = "0.1" },
]
Expand Down

0 comments on commit e831c05

Please sign in to comment.