use crate::{DESCRIPTOR_LEN, END, START, STYLE_LEN, Style};
use core::fmt;
use core::fmt::Write;
impl Style {
pub fn encode_desc_bytes(&self) -> [u8; DESCRIPTOR_LEN] {
[
self.foreground.to_byte(),
self.background.to_byte(),
self.attributes.to_byte(),
]
}
pub fn encode_desc(&self) -> [char; DESCRIPTOR_LEN] {
[
char::from(self.foreground.to_byte()),
char::from(self.background.to_byte()),
char::from(self.attributes.to_byte()),
]
}
pub fn style_to(&self, write: &mut dyn Write, string: &str) -> fmt::Result {
let [fg, bg, attr] = self.encode_desc();
write!(write, "{}", START)?;
write!(write, "{}", fg)?;
write!(write, "{}", bg)?;
write!(write, "{}", attr)?;
write!(write, "{}", string)?;
write!(write, "{}", END)?;
Ok(())
}
#[cfg(feature = "alloc")]
pub fn style(&self, string: &str) -> alloc::string::String {
let mut out = alloc::string::String::with_capacity(string.len() + STYLE_LEN);
self.style_to(&mut out, string)
.expect("failed to style string");
out
}
}