use tau_cli_term_raw::{Color, Span, Style, StyledBlock, StyledText};
use tau_themes::{ResolvedSpan, StyleName, Theme, ThemeStyle, ThemedText};
pub fn resolve(theme: &Theme, name: &str) -> Style {
convert_style(theme.resolve_style(&StyleName::new(name)))
}
pub fn convert_style(ts: ThemeStyle) -> Style {
Style {
fg: ts.fg.map(convert_color),
bg: ts.bg.map(convert_color),
bold: ts.bold,
underline: ts.underline,
italic: ts.italic,
strikethrough: ts.strikethrough,
}
}
pub fn themed_text(theme: &Theme, text: &ThemedText) -> StyledText {
StyledText::from(
theme
.resolve(text)
.into_iter()
.map(resolved_span)
.collect::<Vec<_>>(),
)
}
fn resolved_span(span: ResolvedSpan<'_>) -> Span {
Span::new(span.text.to_owned(), convert_style(span.style))
}
pub fn themed_block(theme: &Theme, name: &str, text: impl Into<String>) -> StyledBlock {
let mut themed = ThemedText::new();
let style = themed.add_style(name);
themed.push(style, text);
let ts = theme.resolve_style(&StyleName::new(name));
let mut block = StyledBlock::new(themed_text(theme, &themed));
if let Some(bg) = ts.bg {
block = block.bg(convert_color(bg));
}
block
}
pub fn convert_color(c: tau_themes::Color) -> Color {
use tau_themes::Color as TC;
match c {
TC::Black => Color::Black,
TC::DarkRed => Color::DarkRed,
TC::DarkGreen => Color::DarkGreen,
TC::DarkYellow => Color::DarkYellow,
TC::DarkBlue => Color::DarkBlue,
TC::DarkMagenta => Color::DarkMagenta,
TC::DarkCyan => Color::DarkCyan,
TC::DarkGrey => Color::DarkGrey,
TC::Red => Color::Red,
TC::Green => Color::Green,
TC::Yellow => Color::Yellow,
TC::Blue => Color::Blue,
TC::Magenta => Color::Magenta,
TC::Cyan => Color::Cyan,
TC::White => Color::White,
TC::Grey => Color::Grey,
TC::Rgb { r, g, b } => Color::Rgb { r, g, b },
}
}
#[cfg(test)]
mod tests;