From e831c05697ee7cdca426f4596dd1bbceb99d4b4c Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Mon, 8 Jan 2024 13:08:21 +0900 Subject: [PATCH] refactor: replace owo-colors with anstream/anstyle 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. --- Cargo.lock | 43 ++++----------------------- Cargo.toml | 3 +- crates/mabo-compiler/Cargo.toml | 3 +- crates/mabo-compiler/src/highlight.rs | 35 ++++++++++++++-------- crates/mabo-parser/Cargo.toml | 3 +- crates/mabo-parser/src/highlight.rs | 33 ++++++++++++-------- deny.toml | 5 ---- 7 files changed, 55 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5569b7b..96032c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -126,17 +126,6 @@ dependencies = [ "nom", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -518,15 +507,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.3.3" @@ -602,7 +582,7 @@ version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" dependencies = [ - "hermit-abi 0.3.3", + "hermit-abi", "rustix", "windows-sys 0.52.0", ] @@ -779,10 +759,11 @@ dependencies = [ name = "mabo-compiler" version = "0.1.0" dependencies = [ + "anstream", + "anstyle", "insta", "mabo-parser", "miette", - "owo-colors", "schemars", "serde", "serde_json", @@ -858,11 +839,12 @@ dependencies = [ name = "mabo-parser" version = "0.1.0" dependencies = [ + "anstream", + "anstyle", "indoc", "insta", "mabo-derive", "miette", - "owo-colors", "winnow", ] @@ -902,7 +884,7 @@ dependencies = [ "miette-derive", "once_cell", "owo-colors", - "supports-color 2.1.0", + "supports-color", "supports-hyperlinks", "supports-unicode", "terminal_size 0.1.17", @@ -1038,9 +1020,6 @@ name = "owo-colors" version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" -dependencies = [ - "supports-color 1.3.1", -] [[package]] name = "parking_lot" @@ -1379,16 +1358,6 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" -[[package]] -name = "supports-color" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ba6faf2ca7ee42fdd458f4347ae0a9bd6bcc445ad7cb57ad82b383f18870d6f" -dependencies = [ - "atty", - "is_ci", -] - [[package]] name = "supports-color" version = "2.1.0" diff --git a/Cargo.toml b/Cargo.toml index 01fc592..13fdde9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } @@ -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"] } diff --git a/crates/mabo-compiler/Cargo.toml b/crates/mabo-compiler/Cargo.toml index 46ec69d..bfa1b0b 100644 --- a/crates/mabo-compiler/Cargo.toml +++ b/crates/mabo-compiler/Cargo.toml @@ -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 } diff --git a/crates/mabo-compiler/src/highlight.rs b/crates/mabo-compiler/src/highlight.rs index 68465a8..65dcd76 100644 --- a/crates/mabo-compiler/src/highlight.rs +++ b/crates/mabo-compiler/src/highlight.rs @@ -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, } } diff --git a/crates/mabo-parser/Cargo.toml b/crates/mabo-parser/Cargo.toml index 04240ca..8e117ea 100644 --- a/crates/mabo-parser/Cargo.toml +++ b/crates/mabo-parser/Cargo.toml @@ -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" diff --git a/crates/mabo-parser/src/highlight.rs b/crates/mabo-parser/src/highlight.rs index c4e2efc..d309e28 100644 --- a/crates/mabo-parser/src/highlight.rs +++ b/crates/mabo-parser/src/highlight.rs @@ -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, } } diff --git a/deny.toml b/deny.toml index 4390ff2..98fb02d 100644 --- a/deny.toml +++ b/deny.toml @@ -1,8 +1,5 @@ all-features = true -[advisories] -ignore = ["RUSTSEC-2021-0145"] - [licenses] allow-osi-fsf-free = "both" exceptions = [ @@ -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" }, ]