fatui 0.1.0

a silly little framework for terminal user interfaces
Documentation
use std::fmt;

use crossterm::{Command, style};
use owo_colors::{Style, Styled};

/// a single character which has been styled
///
/// this is nearly identical to a [`owo_colors::Styled<char>`],
/// but that type is missing some common sense derives for no reason
/// so we make do.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StyledChar {
	/// the [`Style`] which is to be applied to this character
	pub style: Style,
	/// the character being styled
	pub char: char,
}

impl StyledChar {
	/// a blank character -- that is, a single space with no styling at all
	pub const BLANK: Self = Self { style: Style::new(), char: ' ' };

	/// create a new [`StyledChar`] with the given character and style
	pub const fn new(c: char, s: Style) -> Self {
		Self { style: s, char: c }
	}

	/// create a new [`StyledChar`] with no styling
	pub const fn plain(c: char) -> Self {
		Self { style: Style::new(), char: c }
	}

	/// convert an [`owo_colors::Styled<char>`] to a [`StyledChar`]
	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)
		}
	}
}