1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! A module that contains all the actions related to the terminal. like clearing, resizing, pausing and scrolling the terminal.

#[cfg(test)]
mod test;

mod terminal;

mod ansi_terminal;
#[cfg(target_os = "windows")]
mod winapi_terminal;

use self::ansi_terminal::AnsiTerminal;
#[cfg(target_os = "windows")]
use self::winapi_terminal::WinApiTerminal;

pub use self::terminal::{from_screen, terminal, Terminal};

use common::{error, functions};
use std::sync::Arc;
use {Screen, TerminalOutput};

/// Enum that specifies a way of clearing.
pub enum ClearType {
    All,
    FromCursorDown,
    FromCursorUp,
    CurrentLine,
    UntilNewLine,
}

/// This trait defines the actions that can be preformed with the terminal color.
/// This trait can be implemented so that an concrete implementation of the ITerminalColor can fulfill.
/// the wishes to work on an specific platform.
///
/// ## For example:
///
/// This trait is implemented for `WinApi` (Windows specific) and `ANSI` (Unix specific),
/// so that terminal related actions can be preformed on both Unix and Windows systems.
trait ITerminal {
    /// Clear the current cursor by specifying the clear type
    fn clear(
        &self,
        clear_type: ClearType,
        stdout: &Option<&Arc<TerminalOutput>>,
    ) -> error::Result<()>;
    /// Get the terminal size (x,y)
    fn terminal_size(&self, stdout: &Option<&Arc<TerminalOutput>>) -> (u16, u16);
    /// Scroll `n` lines up in the current terminal.
    fn scroll_up(&self, count: i16, stdout: &Option<&Arc<TerminalOutput>>) -> error::Result<()>;
    /// Scroll `n` lines down in the current terminal.
    fn scroll_down(&self, count: i16, stdout: &Option<&Arc<TerminalOutput>>) -> error::Result<()>;
    /// Resize terminal to the given width and height.
    fn set_size(
        &self,
        width: i16,
        height: i16,
        stdout: &Option<&Arc<TerminalOutput>>,
    ) -> error::Result<()>;
    /// Close the current terminal
    fn exit(&self, stdout: &Option<&Arc<TerminalOutput>>);
}