use crate::*;
impl<'a> Default for Text<'a> {
#[inline(always)]
fn default() -> Self {
Text {
text: "",
color: ColorType::default(),
bg_color: ColorType::default(),
bold: false,
endl: false,
}
}
}
impl<'a> Text<'a> {
#[inline(always)]
pub(crate) fn new_from(text: &Text<'a>) -> Self {
Self { ..*text }
}
pub(crate) fn get_display_str_cow(&self) -> Cow<'a, str> {
let text: &str = self.text;
let bold: bool = self.bold;
let adjusted_color: ColorType = if matches!(self.color, ColorType::Use(Color::Default)) {
ColorContrast::ensure_sufficient_contrast(&self.color, &self.bg_color)
} else {
self.color
};
let color: String = adjusted_color.to_string();
let bg_color: String = self.bg_color.get_str(DisplayType::Background);
let mut colored_text: String = if bold {
format!("{CSI}{SGR_BOLD}{SEMICOLON}{bg_color}{SEMICOLON}{color}{SGR}{text}{SGR_RESET}")
} else {
format!("{CSI}{bg_color}{SEMICOLON}{color}{SGR}{text}{SGR_RESET}")
};
if self.endl {
colored_text.push(LINE_FEED);
}
Cow::Owned(colored_text)
}
}