use super::super::super::shared::functions;
use super::*;
use std::io;
use std::rc::Rc;
use std::sync::Mutex;
use style::Color;
use {Context, ScreenManager};
pub struct TerminalColor {
color: Box<ITerminalColor>,
screen_manager: Rc<Mutex<ScreenManager>>,
}
impl TerminalColor {
pub fn new(context: Rc<Context>) -> TerminalColor {
#[cfg(target_os = "windows")]
let color = functions::get_module::<Box<ITerminalColor>>(
WinApiColor::new(context.screen_manager.clone()),
AnsiColor::new(context.screen_manager.clone()),
).unwrap();
#[cfg(not(target_os = "windows"))]
let color = AnsiColor::new(context.screen_manager.clone()) as Box<ITerminalColor>;
TerminalColor {
color: color,
screen_manager: context.screen_manager.clone(),
}
}
pub fn set_fg(&self, color: Color) {
self.color.set_fg(color);
}
pub fn set_bg(&self, color: Color) {
self.color.set_bg(color);
}
pub fn reset(&self) {
self.color.reset();
}
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(context: &Rc<Context>) -> Box<TerminalColor> {
Box::from(TerminalColor::new(context.clone()))
}