use cursive::{theme, utils::markup};
use crate::{AnsiColor, AnsiMode, Color, Effect, Style, StyledStr, StyledString};
impl From<Color> for theme::Color {
fn from(color: Color) -> theme::Color {
match color {
Color::Ansi { color, mode } => match mode {
AnsiMode::Dark => theme::Color::Dark(color.into()),
AnsiMode::Light => theme::Color::Light(color.into()),
},
Color::Rgb { r, g, b } => theme::Color::Rgb(r, g, b),
}
}
}
impl From<AnsiColor> for theme::BaseColor {
fn from(color: AnsiColor) -> theme::BaseColor {
match color {
AnsiColor::Black => theme::BaseColor::Black,
AnsiColor::Red => theme::BaseColor::Red,
AnsiColor::Green => theme::BaseColor::Green,
AnsiColor::Yellow => theme::BaseColor::Yellow,
AnsiColor::Blue => theme::BaseColor::Blue,
AnsiColor::Magenta => theme::BaseColor::Magenta,
AnsiColor::Cyan => theme::BaseColor::Cyan,
AnsiColor::White => theme::BaseColor::White,
}
}
}
impl From<Effect> for theme::Effect {
fn from(effect: Effect) -> theme::Effect {
match effect {
Effect::Bold => theme::Effect::Bold,
Effect::Italic => theme::Effect::Italic,
Effect::Underline => theme::Effect::Underline,
}
}
}
impl From<Style> for theme::Style {
fn from(style: Style) -> theme::Style {
theme::Style {
effects: style.effects.into_iter().map(theme::Effect::from).collect(),
color: get_color_style(style.fg, style.bg),
}
}
}
fn get_color_style(fg: Option<Color>, bg: Option<Color>) -> Option<theme::ColorStyle> {
let mut front = theme::Color::TerminalDefault;
let mut back = theme::Color::TerminalDefault;
if let Some(fg) = fg {
front = fg.into();
}
if let Some(bg) = bg {
back = bg.into();
}
if front == theme::Color::TerminalDefault && back == theme::Color::TerminalDefault {
None
} else {
Some(theme::ColorStyle::new(front, back))
}
}
impl<'a, 'b> From<&'b StyledStr<'a>> for markup::StyledString {
fn from(s: &'b StyledStr<'a>) -> markup::StyledString {
if let Some(style) = s.style {
markup::StyledString::styled(s.s.to_owned(), style)
} else {
markup::StyledString::plain(s.s.to_owned())
}
}
}
impl<'a> From<StyledStr<'a>> for markup::StyledString {
fn from(s: StyledStr<'a>) -> markup::StyledString {
if let Some(style) = s.style {
markup::StyledString::styled(s.s.to_owned(), style)
} else {
markup::StyledString::plain(s.s.to_owned())
}
}
}
impl From<StyledString> for markup::StyledString {
fn from(s: StyledString) -> markup::StyledString {
if let Some(style) = s.style {
markup::StyledString::styled(s.s, style)
} else {
markup::StyledString::plain(s.s)
}
}
}