Skip to content

Commit 196253b

Browse files
REALERvolker1sharkdp
authored andcommitted
Add library functions
1 parent d62da09 commit 196253b

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/style.rs

+73
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,37 @@ impl Color {
115115
Color::BrightWhite => crossterm::style::Color::White,
116116
}
117117
}
118+
119+
/// Convert to a `owo-colors::DynColors` (if the `owo-colors` feature is enabled).
120+
#[cfg(feature = "owo-colors")]
121+
pub fn to_owo_color(&self) -> owo_colors::DynColors {
122+
match self {
123+
Color::RGB(r, g, b) => owo_colors::DynColors::Rgb(*r, *g, *b),
124+
Color::Fixed(n) => owo_colors::DynColors::Xterm(owo_colors::XtermColors::from(*n)),
125+
126+
Color::Black => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Black),
127+
Color::Red => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Red),
128+
Color::Green => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Green),
129+
Color::Yellow => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Yellow),
130+
Color::Blue => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Blue),
131+
Color::Magenta => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Magenta),
132+
Color::Cyan => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::Cyan),
133+
Color::White => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::White),
134+
135+
Color::BrightBlack => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightBlack),
136+
Color::BrightRed => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightRed),
137+
Color::BrightGreen => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightGreen),
138+
Color::BrightYellow => {
139+
owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightYellow)
140+
}
141+
Color::BrightBlue => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightBlue),
142+
Color::BrightMagenta => {
143+
owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightMagenta)
144+
}
145+
Color::BrightCyan => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightCyan),
146+
Color::BrightWhite => owo_colors::DynColors::Ansi(owo_colors::AnsiColors::BrightWhite),
147+
}
148+
}
118149
}
119150

120151
/// Font-style attributes.
@@ -474,6 +505,48 @@ impl Style {
474505
underline_color: self.underline.as_ref().map(Color::to_crossterm_color),
475506
}
476507
}
508+
509+
/// Convert to a `owo_colors::Style` (if the `owo-colors` feature is enabled).
510+
#[cfg(feature = "owo-colors")]
511+
pub fn to_owo_colors_style(&self) -> owo_colors::Style {
512+
let mut style = owo_colors::Style::new();
513+
if let Some(ref c) = self.foreground {
514+
style = style.color(c.to_owo_color())
515+
}
516+
if let Some(ref b) = self.background {
517+
style = style.on_color(b.to_owo_color())
518+
}
519+
if self.font_style.bold {
520+
style = style.bold()
521+
}
522+
if self.font_style.dimmed {
523+
style = style.dimmed()
524+
}
525+
if self.font_style.hidden {
526+
style = style.hidden()
527+
}
528+
if self.font_style.italic {
529+
style = style.italic()
530+
}
531+
if self.font_style.rapid_blink {
532+
style = style.blink_fast()
533+
}
534+
if self.font_style.reverse {
535+
style = style.reversed()
536+
}
537+
if self.font_style.slow_blink {
538+
style = style.blink()
539+
}
540+
if self.font_style.strikethrough {
541+
style = style.strikethrough()
542+
}
543+
if self.font_style.underline {
544+
style = style.underline()
545+
}
546+
547+
// TODO: Implement colored underline. owo-colors does not support it at the time of writing.
548+
style
549+
}
477550
}
478551

479552
#[cfg(test)]

0 commit comments

Comments
 (0)