use crate::style::{Style, Styled};
const NBSP: &str = "\u{00a0}";
const ZWSP: &str = "\u{200b}";
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
pub struct StyledGrapheme<'a> {
pub symbol: &'a str,
pub style: Style,
}
impl<'a> StyledGrapheme<'a> {
pub fn new<S: Into<Style>>(symbol: &'a str, style: S) -> Self {
Self {
symbol,
style: style.into(),
}
}
pub(crate) fn is_whitespace(&self) -> bool {
let symbol = self.symbol;
symbol == ZWSP || symbol.chars().all(char::is_whitespace) && symbol != NBSP
}
}
impl<'a> Styled for StyledGrapheme<'a> {
type Item = Self;
fn style(&self) -> Style {
self.style
}
fn set_style<S: Into<Style>>(mut self, style: S) -> Self::Item {
self.style = style.into();
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::style::Stylize;
#[test]
fn new() {
let style = Style::new().yellow();
let sg = StyledGrapheme::new("a", style);
assert_eq!(sg.symbol, "a");
assert_eq!(sg.style, style);
}
#[test]
fn style() {
let style = Style::new().yellow();
let sg = StyledGrapheme::new("a", style);
assert_eq!(sg.style(), style);
}
#[test]
fn set_style() {
let style = Style::new().yellow().on_red();
let style2 = Style::new().green();
let sg = StyledGrapheme::new("a", style).set_style(style2);
assert_eq!(sg.style, style2);
}
#[test]
fn stylize() {
let style = Style::new().yellow().on_red();
let sg = StyledGrapheme::new("a", style).green();
assert_eq!(sg.style, Style::new().green().on_red());
}
}