use super::super::shared::functions;
use super::super::style;
use super::*;
use Context;
use std::fmt;
use std::io::Write;
use std::rc::Rc;
pub struct Terminal {
terminal: Box<ITerminal>,
context: Rc<Context>,
}
impl Terminal {
pub fn new(context: Rc<Context>) -> Terminal {
#[cfg(target_os = "windows")]
let terminal = functions::get_module::<Box<ITerminal>>(
WinApiTerminal::new(context.clone()),
AnsiTerminal::new(context.clone()),
).unwrap();
#[cfg(not(target_os = "windows"))]
let terminal = AnsiTerminal::new(context.clone()) as Box<ITerminal>;
Terminal {
terminal,
context: context,
}
}
pub fn clear(&self, clear_type: ClearType) {
self.terminal.clear(clear_type);
}
pub fn terminal_size(&self) -> (u16, u16) {
return self.terminal.terminal_size();
}
pub fn scroll_up(&self, count: i16) {
self.terminal.scroll_up(count);
}
pub fn scroll_down(&self, count: i16) {
self.terminal.scroll_down(count);
}
pub fn set_size(&self, width: i16, height: i16) {
self.terminal.set_size(width, height);
}
pub fn paint<D>(&self, val: D) -> style::StyledObject<D> where D: fmt::Display
{
style::ObjectStyle::new().apply_to(val, self.context.clone())
}
pub fn exit(&self) {
self.terminal.exit();
}
pub fn write<D: fmt::Display>(&self, value: D) {
let mut mutex = &self.context.screen_manager;
{
let mut screen_manager = mutex.lock().unwrap();
use std::fmt::Write;
let mut string = String::new();
write!(string, "{}", value).unwrap();
screen_manager.write_string(string);
screen_manager.flush();
}
}
}
pub fn terminal(context: &Rc<Context>) -> Box<Terminal> {
Box::from(Terminal::new(context.clone()))
}