Skip to content

Commit

Permalink
Remove console dependency from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
printfn committed Dec 28, 2023
1 parent 4688c5b commit 93275f8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 112 deletions.
85 changes: 0 additions & 85 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 0 additions & 14 deletions cli/src/color/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`");
Expand Down
2 changes: 1 addition & 1 deletion cli/src/color/output_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 27 additions & 9 deletions cli/src/color/style.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ fn print_spans(spans: Vec<fend_core::SpanRef<'_>>, 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
}
Expand Down

0 comments on commit 93275f8

Please sign in to comment.