const CSI: &str = "\u{001b}[";
const BACKGROUND_8BIT: &str = "48;5";
const FOREGROUND_8BIT: &str = "38;5";
const BACKGROUND_24BIT: &str = "48;2";
const FOREGROUND_24BIT: &str = "38;2";
const MESSAGE: &str = "${}";
const END_SGR: char = 'm';
const RESET: char = '0';
const DELIMITER: char = ';';
const BLINK: char = '5';
const UNDERLINE: char = '4';
pub struct StyleBuilder {
message: String,
}
impl StyleBuilder {
pub fn new() -> Self {
Self {
message: "".to_owned(),
}
}
pub fn underline(mut self) -> StyleBuilder {
self.message.push(UNDERLINE);
self
}
pub fn blink(mut self) -> StyleBuilder {
self.message.push(BLINK);
self
}
pub fn csi(mut self) -> StyleBuilder {
self.message.push_str(CSI);
self
}
pub fn background_8bit(mut self) -> StyleBuilder {
self.message.push_str(BACKGROUND_8BIT);
self
}
pub fn color(mut self, color: usize) -> StyleBuilder {
self.message.push_str(color.to_string().as_ref());
self
}
pub fn foreground_8bit(mut self) -> StyleBuilder {
self.message.push_str(FOREGROUND_8BIT);
self
}
pub fn background_24bit(mut self) -> StyleBuilder {
self.message.push_str(BACKGROUND_24BIT);
self
}
pub fn foreground_24bit(mut self) -> StyleBuilder {
self.message.push_str(FOREGROUND_24BIT);
self
}
pub fn reset(mut self) -> StyleBuilder {
self.message.push(RESET);
self
}
pub fn delimiter(mut self) -> StyleBuilder {
self.message.push(DELIMITER);
self
}
pub fn end_sgr(mut self) -> StyleBuilder {
self.message.push(END_SGR);
self
}
pub fn message(mut self) -> StyleBuilder {
self.message.push_str(MESSAGE);
self
}
pub fn build(self) -> Style {
Style {
message: self.message,
}
}
}
pub struct Style {
message: String,
}
impl Style {
pub fn render(&self, message: &str) -> String {
self.message.replace(MESSAGE, message)
}
}