use std::fmt;
use crossterm::{Command, style};
use owo_colors::{Style, Styled};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StyledChar {
pub style: Style,
pub char: char,
}
impl StyledChar {
pub const BLANK: Self = Self { style: Style::new(), char: ' ' };
pub const fn new(c: char, s: Style) -> Self {
Self { style: s, char: c }
}
pub const fn plain(c: char) -> Self {
Self { style: Style::new(), char: c }
}
pub const fn from_owo(o: Styled<char>) -> Self {
Self { style: o.style, char: *o.inner() }
}
}
impl Default for StyledChar {
fn default() -> Self {
Self::BLANK
}
}
impl From<Styled<char>> for StyledChar {
fn from(value: Styled<char>) -> Self {
Self::from_owo(value)
}
}
impl fmt::Display for StyledChar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.style.is_plain() {
style::ResetColor.write_ansi(f)?;
write!(f, "{}", self.char)
} else {
write!(f, "{}{}", self.style.prefix_formatter(), self.char)
}
}
}