use super::{Color, ObjectStyle};
use Screen;
use std::fmt::Display;
#[cfg(unix)]
use super::Attribute;
pub struct StyledObject<D: Display> {
pub object_style: ObjectStyle,
pub content: D,
}
impl<D: Display> StyledObject<D> {
pub fn with(mut self, foreground_color: Color) -> StyledObject<D> {
self.object_style = self.object_style.fg(foreground_color);
self
}
pub fn on(mut self, background_color: Color) -> StyledObject<D> {
self.object_style = self.object_style.bg(background_color);
self
}
#[cfg(unix)]
pub fn attr(mut self, attr: Attribute) -> StyledObject<D> {
&self.object_style.add_attr(attr);
self
}
#[cfg(unix)]
#[inline(always)]
pub fn bold(self) -> StyledObject<D> {
self.attr(Attribute::Bold)
}
#[cfg(unix)]
#[inline(always)]
pub fn dim(self) -> StyledObject<D> {
self.attr(Attribute::Dim)
}
#[cfg(unix)]
#[inline(always)]
pub fn italic(self) -> StyledObject<D> {
self.attr(Attribute::Italic)
}
#[cfg(unix)]
#[inline(always)]
pub fn underlined(self) -> StyledObject<D> {
self.attr(Attribute::Underlined)
}
#[cfg(unix)]
#[inline(always)]
pub fn slow_blink(self) -> StyledObject<D> {
self.attr(Attribute::SlowBlink)
}
#[cfg(unix)]
#[inline(always)]
pub fn rapid_blink(self) -> StyledObject<D> {
self.attr(Attribute::RapidBlink)
}
#[cfg(unix)]
#[inline(always)]
pub fn reverse(self) -> StyledObject<D> {
self.attr(Attribute::Reverse)
}
#[cfg(unix)]
#[inline(always)]
pub fn hidden(self) -> StyledObject<D> {
self.attr(Attribute::Hidden)
}
#[cfg(unix)]
#[inline(always)]
pub fn crossed_out(self) -> StyledObject<D> {
self.attr(Attribute::CrossedOut)
}
pub fn paint(&self, screen: &Screen)
{
let colored_terminal = ::color(&screen);
let mut reset = true;
if let Some(bg) = self.object_style.bg_color {
colored_terminal.set_bg(bg);
reset = true;
}
if let Some(fg) = self.object_style.fg_color {
colored_terminal.set_fg(fg);
reset = true;
}
#[cfg(unix)]
for attr in self.object_style.attrs.iter() {
screen.stdout.write_string(format!(csi!("{}m"), *attr as i16));
reset = true;
}
use std::fmt::Write;
let mut content = String::new();
write!(content, "{}", self.content).unwrap();
screen.stdout.write_string(content);
screen.stdout.flush();
if reset {
colored_terminal.reset();
}
}
}