Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add owo-colors as an ansi backend #83

Merged
merged 4 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ gnu_legacy = ["nu-ansi-term/gnu_legacy"]
ansi_term = { version = "0.12", optional = true }
nu-ansi-term = { version = "0.50", optional = true }
crossterm = { version = "0.27", optional = true }
owo-colors = { version = "4.0", optional = true }

[dev-dependencies]
tempfile = "^3"
Expand Down
13 changes: 11 additions & 2 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ use lscolors::{LsColors, Style};
not(feature = "nu-ansi-term"),
not(feature = "gnu_legacy"),
not(feature = "ansi_term"),
not(feature = "crossterm")
not(feature = "crossterm"),
not(feature = "owo-colors")
))]
compile_error!("one feature must be enabled: ansi_term, nu-ansi-term, crossterm, gnu_legacy");
compile_error!(
"one feature must be enabled: ansi_term, nu-ansi-term, crossterm, gnu_legacy, owo-colors"
);

fn print_path(handle: &mut dyn Write, ls_colors: &LsColors, path: &str) -> io::Result<()> {
for (component, style) in ls_colors.style_for_path_components(Path::new(path)) {
Expand All @@ -32,6 +35,12 @@ fn print_path(handle: &mut dyn Write, ls_colors: &LsColors, path: &str) -> io::R
let ansi_style = style.map(Style::to_crossterm_style).unwrap_or_default();
write!(handle, "{}", ansi_style.apply(component.to_string_lossy()))?;
}
#[cfg(feature = "owo-colors")]
{
use owo_colors::OwoColorize;
let ansi_style = style.map(Style::to_owo_colors_style).unwrap_or_default();
write!(handle, "{}", component.to_string_lossy().style(ansi_style))?;
}
}
writeln!(handle)?;

Expand Down
95 changes: 92 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::VecDeque;

/// A `Color` can be one of the pre-defined ANSI colors (`Red`, `Green`, ..),
/// a 8-bit ANSI color (`Fixed(u8)`) or a 24-bit color (`RGB(u8, u8, u8)`).
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Color {
Black,
Red,
Expand Down Expand Up @@ -115,10 +115,41 @@ impl Color {
Color::BrightWhite => crossterm::style::Color::White,
}
}

/// Convert to a `owo-colors::DynColors` (if the `owo-colors` feature is enabled).
#[cfg(feature = "owo-colors")]
pub fn to_owo_color(&self) -> owo_colors::DynColors {
match self {
Color::RGB(r, g, b) => owo_colors::DynColors::Rgb(*r, *g, *b),
Color::Fixed(n) => owo_colors::DynColors::Xterm(owo_colors::XtermColors::from(*n)),

Color::Black => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Black),
Color::Red => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Red),
Color::Green => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Green),
Color::Yellow => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Yellow),
Color::Blue => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Blue),
Color::Magenta => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Magenta),
Color::Cyan => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Cyan),
Color::White => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::White),

Color::BrightBlack => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightBlack),
Color::BrightRed => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightRed),
Color::BrightGreen => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightGreen),
Color::BrightYellow => {
owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightYellow)
}
Color::BrightBlue => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightBlue),
Color::BrightMagenta => {
owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightMagenta)
}
Color::BrightCyan => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightCyan),
Color::BrightWhite => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightWhite),
}
}
}

/// Font-style attributes.
#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct FontStyle {
pub bold: bool,
pub dimmed: bool, // a.k.a. faint
Expand Down Expand Up @@ -231,7 +262,7 @@ impl FontStyle {
}

/// A foreground color, background color and font-style.
#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Style {
pub foreground: Option<Color>,
pub background: Option<Color>,
Expand Down Expand Up @@ -474,6 +505,48 @@ impl Style {
underline_color: self.underline.as_ref().map(Color::to_crossterm_color),
}
}

/// Convert to a `owo_colors::Style` (if the `owo-colors` feature is enabled).
#[cfg(feature = "owo-colors")]
pub fn to_owo_colors_style(&self) -> owo_colors::Style {
let mut style = owo_colors::Style::new();
if let Some(ref c) = self.foreground {
style = style.color(c.to_owo_color())
}
if let Some(ref b) = self.background {
style = style.on_color(b.to_owo_color())
}
if self.font_style.bold {
style = style.bold()
}
if self.font_style.dimmed {
style = style.dimmed()
}
if self.font_style.hidden {
style = style.hidden()
}
if self.font_style.italic {
style = style.italic()
}
if self.font_style.rapid_blink {
style = style.blink_fast()
}
if self.font_style.reverse {
style = style.reversed()
}
if self.font_style.slow_blink {
style = style.blink()
}
if self.font_style.strikethrough {
style = style.strikethrough()
}
if self.font_style.underline {
style = style.underline()
}

// TODO: Implement colored underline. owo-colors does not support it at the time of writing.
style
}
}

#[cfg(test)]
Expand Down Expand Up @@ -782,4 +855,20 @@ mod tests {
cross.apply("wow").to_string()
);
}

#[cfg(all(feature = "owo-colors"))]
#[test]
fn coloring_owo_colors() {
use owo_colors::OwoColorize;
let style = Style {
font_style: FontStyle {
bold: true,
..Default::default()
},
foreground: Some(Color::Blue),
..Default::default()
};
let owo = style.to_owo_colors_style();
assert_eq!("\x1b[34;1mwow\x1b[0m", "wow".style(owo).to_string());
}
}
Loading