use ratatui::style::{Color, Modifier, Style};
use ratatui::text::Span;
pub struct ThemedSpan<'a> {
text: std::borrow::Cow<'a, str>,
fg: Color,
bg: Option<Color>,
modifiers: Modifier,
}
impl<'a> ThemedSpan<'a> {
#[must_use]
pub fn with_color(text: impl Into<std::borrow::Cow<'a, str>>, fg: Color) -> Self {
Self {
text: text.into(),
fg,
bg: None,
modifiers: Modifier::empty(),
}
}
pub(crate) fn new(text: impl Into<std::borrow::Cow<'a, str>>, fg: Color) -> Self {
Self::with_color(text, fg)
}
#[must_use]
pub fn bold(mut self) -> Self {
self.modifiers |= Modifier::BOLD;
self
}
#[must_use]
pub fn italic(mut self) -> Self {
self.modifiers |= Modifier::ITALIC;
self
}
#[must_use]
pub fn dimmed(mut self) -> Self {
self.modifiers |= Modifier::DIM;
self
}
#[must_use]
pub fn on(mut self, bg: Color) -> Self {
self.bg = Some(bg);
self
}
#[must_use]
pub fn build(self) -> Span<'a> {
let mut style = Style::default().fg(self.fg);
if let Some(bg) = self.bg {
style = style.bg(bg);
}
if !self.modifiers.is_empty() {
style = style.add_modifier(self.modifiers);
}
Span::styled(self.text, style)
}
}
impl<'a> From<ThemedSpan<'a>> for Span<'a> {
fn from(ts: ThemedSpan<'a>) -> Self {
ts.build()
}
}