use super::super::shared::functions;
use super::*;
use std::io::Write;
use Context;
use std::fmt::Display;
use std::rc::Rc;
pub struct TerminalCursor {
context: Rc<Context>,
terminal_cursor: Box<ITerminalCursor>,
}
impl TerminalCursor {
pub fn new(context: Rc<Context>) -> TerminalCursor {
#[cfg(target_os = "windows")]
let cursor = functions::get_module::<Box<ITerminalCursor>>(
WinApiCursor::new(context.screen_manager.clone()),
AnsiCursor::new(context.clone()),
).unwrap();
#[cfg(not(target_os = "windows"))]
let cursor = AnsiCursor::new(context.clone()) as Box<ITerminalCursor>;
TerminalCursor {
terminal_cursor: cursor,
context,
}
}
pub fn goto(&mut self, x: u16, y: u16) -> &mut TerminalCursor {
self.terminal_cursor.goto(x, y);
self
}
pub fn pos(&mut self) -> (u16, u16) {
self.terminal_cursor.pos()
}
pub fn move_up(&mut self, count: u16) -> &mut TerminalCursor {
self.terminal_cursor.move_up(count);
self
}
pub fn move_right(&mut self, count: u16) -> &mut TerminalCursor {
self.terminal_cursor.move_right(count);
self
}
pub fn move_down(&mut self, count: u16) -> &mut TerminalCursor {
self.terminal_cursor.move_down(count);
self
}
pub fn move_left(&mut self, count: u16) -> &mut TerminalCursor {
self.terminal_cursor.move_left(count);
self
}
pub fn print<D: Display>(&mut self, value: D) -> &mut TerminalCursor {
{
use std::fmt::Write;
let mut string = String::new();
write!(string, "{}", value).unwrap();
let mut mutex = &self.context.screen_manager;
{
let mut screen_manager = mutex.lock().unwrap();
screen_manager.write_string(string);
screen_manager.flush();
}
}
self
}
pub fn save_position(&self) {
self.terminal_cursor.save_position();
}
pub fn reset_position(&self) {
self.terminal_cursor.reset_position();
}
pub fn hide(&self) {
self.terminal_cursor.hide();
}
pub fn show(&self) {
self.terminal_cursor.show();
}
pub fn blink(&self, blink: bool) {
self.terminal_cursor.blink(blink);
}
}
pub fn cursor(context: &Rc<Context>) -> Box<TerminalCursor> {
Box::from(TerminalCursor::new(context.clone()))
}