use super::commands::{self, IAlternateScreenCommand};
use super::{RawScreen, Screen, TerminalOutput};
use common::functions;
use std::convert::From;
use std::io;
pub struct AlternateScreen {
command: Box<IAlternateScreenCommand + Sync + Send>,
pub screen: Screen,
}
impl AlternateScreen {
pub fn new(command: Box<IAlternateScreenCommand + Sync + Send>, screen: Screen) -> Self {
AlternateScreen { command, screen }
}
pub fn to_alternate_screen(
stdout: TerminalOutput,
raw_mode: bool,
) -> io::Result<AlternateScreen> {
#[cfg(target_os = "windows")]
let command =
functions::get_module::<Box<commands::IAlternateScreenCommand + Sync + Send>>(
Box::from(commands::win_commands::ToAlternateScreenCommand::new()),
Box::from(commands::shared_commands::ToAlternateScreenCommand::new()),
).unwrap();
#[cfg(not(target_os = "windows"))]
let command = Box::from(commands::shared_commands::ToAlternateScreenCommand::new());
let mut stdout = stdout;
command.enable(&mut stdout)?;
let screen = Screen::from(stdout);
if raw_mode {
RawScreen::into_raw_mode()?;
}
Ok(AlternateScreen::new(command, screen))
}
pub fn to_main_screen(&self) -> io::Result<()> {
self.command.disable(&self.screen.stdout)?;
Ok(())
}
}
impl Drop for AlternateScreen {
fn drop(&mut self) {
self.to_main_screen();
}
}