use super::*;
use std::io;
use Screen;
pub struct TerminalColor<'stdout> {
color: Box<ITerminalColor + Sync + Send>,
stdout: Option<&'stdout Arc<TerminalOutput>>,
}
impl<'stdout> TerminalColor<'stdout> {
pub fn new() -> TerminalColor<'stdout> {
#[cfg(target_os = "windows")]
let color = functions::get_module::<Box<ITerminalColor + Sync + Send>>(
Box::from(WinApiColor::new()),
Box::from(AnsiColor::new()),
).unwrap();
#[cfg(not(target_os = "windows"))]
let color = Box::from(AnsiColor::new()) as Box<ITerminalColor + Sync + Send>;
TerminalColor {
color,
stdout: None,
}
}
pub fn from_output(stdout: &'stdout Arc<TerminalOutput>) -> TerminalColor<'stdout> {
#[cfg(target_os = "windows")]
let color = functions::get_module::<Box<ITerminalColor + Sync + Send>>(
Box::from(WinApiColor::new()),
Box::from(AnsiColor::new()),
).unwrap();
#[cfg(not(target_os = "windows"))]
let color = Box::from(AnsiColor::new()) as Box<ITerminalColor + Sync + Send>;
TerminalColor {
color,
stdout: Some(stdout),
}
}
pub fn set_fg(&self, color: Color) {
self.color.set_fg(color, &self.stdout);
}
pub fn set_bg(&self, color: Color) {
self.color.set_bg(color, &self.stdout);
}
pub fn reset(&self) {
self.color.reset(&self.stdout);
}
pub fn get_available_color_count(&self) -> io::Result<u16> {
use std::env;
Ok(match env::var_os("TERM") {
Some(val) => {
if val.to_str().unwrap_or("").contains("256color") {
256
} else {
8
}
}
None => 8,
})
}
}
pub fn color<'stdout>() -> TerminalColor<'stdout> {
TerminalColor::new()
}
pub fn from_screen(screen: &Screen) -> TerminalColor {
TerminalColor::from_output(&screen.stdout)
}