From 93275f84caba691db395675ef8ad77e0a1a1a7d4 Mon Sep 17 00:00:00 2001 From: printfn Date: Thu, 28 Dec 2023 02:08:22 +0000 Subject: [PATCH] Remove `console` dependency from CLI --- Cargo.lock | 85 ---------------------------------- cli/Cargo.toml | 1 - cli/src/color/base.rs | 14 ------ cli/src/color/output_colors.rs | 2 +- cli/src/color/style.rs | 36 ++++++++++---- cli/src/main.rs | 3 +- 6 files changed, 29 insertions(+), 112 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d71bd510..5e4a80e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,18 +50,6 @@ dependencies = [ "error-code", ] -[[package]] -name = "console" -version = "0.15.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8" -dependencies = [ - "encode_unicode", - "lazy_static", - "libc", - "windows-sys 0.45.0", -] - [[package]] name = "core-foundation" version = "0.9.4" @@ -88,12 +76,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "encode_unicode" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" - [[package]] name = "endian-type" version = "0.1.2" @@ -143,7 +125,6 @@ dependencies = [ name = "fend" version = "1.4.0" dependencies = [ - "console", "ctrlc", "fend-core", "home", @@ -866,15 +847,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-sys" -version = "0.45.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" -dependencies = [ - "windows-targets 0.42.2", -] - [[package]] name = "windows-sys" version = "0.48.0" @@ -893,21 +865,6 @@ dependencies = [ "windows-targets 0.52.0", ] -[[package]] -name = "windows-targets" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" -dependencies = [ - "windows_aarch64_gnullvm 0.42.2", - "windows_aarch64_msvc 0.42.2", - "windows_i686_gnu 0.42.2", - "windows_i686_msvc 0.42.2", - "windows_x86_64_gnu 0.42.2", - "windows_x86_64_gnullvm 0.42.2", - "windows_x86_64_msvc 0.42.2", -] - [[package]] name = "windows-targets" version = "0.48.5" @@ -938,12 +895,6 @@ dependencies = [ "windows_x86_64_msvc 0.52.0", ] -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" - [[package]] name = "windows_aarch64_gnullvm" version = "0.48.5" @@ -956,12 +907,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" -[[package]] -name = "windows_aarch64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" - [[package]] name = "windows_aarch64_msvc" version = "0.48.5" @@ -974,12 +919,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" -[[package]] -name = "windows_i686_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" - [[package]] name = "windows_i686_gnu" version = "0.48.5" @@ -992,12 +931,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" -[[package]] -name = "windows_i686_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" - [[package]] name = "windows_i686_msvc" version = "0.48.5" @@ -1010,12 +943,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" -[[package]] -name = "windows_x86_64_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" - [[package]] name = "windows_x86_64_gnu" version = "0.48.5" @@ -1028,12 +955,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" - [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" @@ -1046,12 +967,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" -[[package]] -name = "windows_x86_64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" - [[package]] name = "windows_x86_64_msvc" version = "0.48.5" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index d319b3d7..1e2f2915 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -11,7 +11,6 @@ license.workspace = true readme = "../README.md" [dependencies] -console = { version = "0.15.7", default-features = false } ctrlc = "3.4.2" fend-core.workspace = true home = "0.5.9" diff --git a/cli/src/color/base.rs b/cli/src/color/base.rs index 74a88d00..73230839 100644 --- a/cli/src/color/base.rs +++ b/cli/src/color/base.rs @@ -53,20 +53,6 @@ impl<'de> serde::Deserialize<'de> for Base { } impl Base { - pub fn as_ansi(&self) -> console::Color { - match self { - Self::Black => console::Color::Black, - Self::Red => console::Color::Red, - Self::Green => console::Color::Green, - Self::Yellow => console::Color::Yellow, - Self::Blue => console::Color::Blue, - Self::Magenta => console::Color::Magenta, - Self::Cyan => console::Color::Cyan, - Self::White | Self::Unknown(_) => console::Color::White, - Self::Color256(n) => console::Color::Color256(*n), - } - } - pub fn warn_about_unknown_colors(&self) { if let Self::Unknown(name) = self { eprintln!("Warning: ignoring unknown color `{name}`"); diff --git a/cli/src/color/output_colors.rs b/cli/src/color/output_colors.rs index 9dfa8953..39069ff6 100644 --- a/cli/src/color/output_colors.rs +++ b/cli/src/color/output_colors.rs @@ -58,7 +58,7 @@ impl OutputColors { } } - pub fn get_color(&self, kind: fend_core::SpanKind) -> console::Style { + pub fn get_color(&self, kind: fend_core::SpanKind) -> String { use fend_core::SpanKind; match kind { diff --git a/cli/src/color/style.rs b/cli/src/color/style.rs index b953976a..61099cd1 100644 --- a/cli/src/color/style.rs +++ b/cli/src/color/style.rs @@ -1,5 +1,5 @@ use super::base::Base; -use std::fmt; +use std::fmt::{self, Write}; #[derive(Default, Clone, Debug, Eq, PartialEq)] pub struct Color { @@ -83,18 +83,36 @@ impl Color { } } - pub fn to_ansi(&self) -> console::Style { - let mut style = console::Style::default(); - if let Some(foreground) = &self.foreground { - style = style.fg(foreground.as_ansi()); - } + pub fn to_ansi(&self) -> String { + let mut result = "\x1b[".to_string(); if self.underline { - style = style.underlined(); + result.push_str("4;"); } if self.bold { - style = style.bold(); + result.push_str("1;"); + } + if let Some(foreground) = &self.foreground { + match foreground { + Base::Black => result.push_str("30"), + Base::Red => result.push_str("31"), + Base::Green => result.push_str("32"), + Base::Yellow => result.push_str("33"), + Base::Blue => result.push_str("34"), + Base::Magenta => result.push_str("35"), + Base::Cyan => result.push_str("36"), + Base::White => result.push_str("37"), + Base::Color256(n) => { + result.push_str("38;5;"); + write!(result, "{n}").unwrap(); + result.push_str(&n.to_string()); + } + Base::Unknown(_) => result.push_str("39"), + } + } else { + result.push_str("39"); } - style + result.push('m'); + result } pub fn print_warnings_about_unknown_keys(&self, style_assignment: &str) { diff --git a/cli/src/main.rs b/cli/src/main.rs index f539da50..6b25eef0 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -33,8 +33,7 @@ fn print_spans(spans: Vec>, config: &config::Config) -> S let mut result = String::new(); for span in spans { let style = config.colors.get_color(span.kind()); - let styled_str = style.force_styling(true).apply_to(span.string()); - write!(result, "{styled_str}").unwrap(); + write!(result, "{style}{}\x1b[0m", span.string()).unwrap(); } result }