2018-01-18 09:06:45 +11:00
|
|
|
//! With this module you can perform actions that are terminal related.
|
|
|
|
//! Like clearing and scrolling in the terminal or getting the size of the terminal.
|
2018-03-11 03:33:06 +11:00
|
|
|
|
2018-03-06 08:06:43 +11:00
|
|
|
use super::*;
|
|
|
|
use shared::functions;
|
2018-03-04 01:40:51 +11:00
|
|
|
use {Construct, Context};
|
2018-01-18 09:06:45 +11:00
|
|
|
|
2018-03-06 08:06:43 +11:00
|
|
|
use std::ops::Drop;
|
2018-01-18 09:06:45 +11:00
|
|
|
|
|
|
|
/// Struct that stores an specific platform implementation for terminal related actions.
|
|
|
|
pub struct Terminal {
|
|
|
|
terminal: Option<Box<ITerminal>>,
|
|
|
|
}
|
|
|
|
|
2018-03-04 01:40:51 +11:00
|
|
|
impl Terminal {
|
2018-01-31 07:20:11 +11:00
|
|
|
/// Create new terminal instance whereon terminal related actions can be performed.
|
|
|
|
pub fn new() -> Terminal {
|
2018-03-04 01:40:51 +11:00
|
|
|
#[cfg(target_os = "windows")]
|
2018-03-11 03:33:06 +11:00
|
|
|
let terminal = functions::get_module::<Box<ITerminal>>(WinApiTerminal::new(), AnsiTerminal::new());
|
2018-02-04 03:17:28 +11:00
|
|
|
|
2018-03-04 01:40:51 +11:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2018-03-06 08:06:43 +11:00
|
|
|
let terminal = Some(AnsiTerminal::new() as Box<ITerminal>);
|
2018-01-31 07:20:11 +11:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
Terminal { terminal: terminal }
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear the current cursor by specifying the clear type
|
|
|
|
///
|
|
|
|
/// #Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// extern crate crossterm;
|
2018-03-11 03:33:06 +11:00
|
|
|
/// use crossterm::terminal;
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
2018-03-11 03:33:06 +11:00
|
|
|
/// let mut term = terminal::terminal();
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
|
|
|
/// // clear all cells in terminal.
|
2018-03-11 03:33:06 +11:00
|
|
|
/// term.clear(terminal::ClearType::All);
|
2018-01-31 07:20:11 +11:00
|
|
|
/// // clear all cells from the cursor position downwards in terminal.
|
2018-03-11 03:33:06 +11:00
|
|
|
/// term.clear(terminal::ClearType::FromCursorDown);
|
2018-01-31 07:20:11 +11:00
|
|
|
/// // clear all cells from the cursor position upwards in terminal.
|
2018-03-11 03:33:06 +11:00
|
|
|
/// term.clear(terminal::ClearType::FromCursorUp);
|
2018-01-18 09:06:45 +11:00
|
|
|
/// // clear current line cells in terminal.
|
2018-03-11 03:33:06 +11:00
|
|
|
/// term.clear(terminal::ClearType::CurrentLine);
|
2018-01-18 09:06:45 +11:00
|
|
|
/// // clear all cells from cursor position until new line in terminal.
|
2018-03-11 03:33:06 +11:00
|
|
|
/// term.clear(terminal::ClearType::UntilNewLine);
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
pub fn clear(&mut self, clear_type: ClearType) {
|
|
|
|
if let Some(ref terminal) = self.terminal {
|
|
|
|
terminal.clear(clear_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the terminal size (x,y).
|
|
|
|
///
|
|
|
|
/// #Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// extern crate crossterm;
|
2018-03-11 03:33:06 +11:00
|
|
|
/// use crossterm::terminal;
|
|
|
|
///
|
|
|
|
/// let mut term = terminal::terminal();
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
|
|
|
/// let size = term.terminal_size();
|
|
|
|
/// println!("{:?}", size);
|
|
|
|
///
|
|
|
|
/// ```
|
2018-02-04 03:17:28 +11:00
|
|
|
pub fn terminal_size(&mut self) -> (u16, u16) {
|
2018-01-18 09:06:45 +11:00
|
|
|
if let Some(ref terminal) = self.terminal {
|
2018-02-04 03:17:28 +11:00
|
|
|
return terminal.terminal_size()
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
2018-02-04 03:17:28 +11:00
|
|
|
(0,0)
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll `n` lines up in the current terminal.
|
|
|
|
///
|
|
|
|
/// #Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// extern crate crossterm;
|
2018-03-11 03:33:06 +11:00
|
|
|
/// use crossterm::terminal;
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
2018-03-11 03:33:06 +11:00
|
|
|
/// let mut term = terminal::terminal();
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
|
|
|
/// // scroll up by 5 lines
|
|
|
|
/// let size = term.scroll_up(5);
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
pub fn scroll_up(&mut self, count: i16) {
|
|
|
|
if let Some(ref terminal) = self.terminal {
|
|
|
|
terminal.scroll_up(count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll `n` lines up in the current terminal.
|
|
|
|
///
|
|
|
|
/// #Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// extern crate crossterm;
|
2018-03-11 03:33:06 +11:00
|
|
|
/// use crossterm::terminal;
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
2018-03-11 03:33:06 +11:00
|
|
|
/// let mut term = terminal::terminal();
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
|
|
|
/// // scroll down by 5 lines
|
|
|
|
/// let size = term.scroll_down(5);
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
pub fn scroll_down(&mut self, count: i16) {
|
|
|
|
if let Some(ref terminal) = self.terminal {
|
|
|
|
terminal.scroll_down(count);
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 04:26:08 +11:00
|
|
|
|
2018-01-28 04:26:08 +11:00
|
|
|
/// Set the terminal size. Note that not all terminals can be set to a very small scale.
|
|
|
|
///
|
|
|
|
/// #Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// extern crate crossterm;
|
2018-03-11 03:33:06 +11:00
|
|
|
/// use crossterm::terminal;
|
2018-01-28 04:26:08 +11:00
|
|
|
///
|
2018-03-11 03:33:06 +11:00
|
|
|
/// let mut term = terminal::terminal();
|
2018-01-28 04:26:08 +11:00
|
|
|
///
|
|
|
|
/// // Set of the size to X: 10 and Y: 10
|
|
|
|
/// let size = term.set_size(10,10);
|
|
|
|
///
|
|
|
|
/// ```
|
2018-01-26 04:26:08 +11:00
|
|
|
pub fn set_size(&mut self, width: i16, height: i16)
|
|
|
|
{
|
2018-01-31 07:20:11 +11:00
|
|
|
if let Some (ref terminal) = self.terminal {
|
2018-01-26 04:26:08 +11:00
|
|
|
terminal.set_size(width,height);
|
|
|
|
}
|
|
|
|
}
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an Terminal implementation whereon terminal related actions can be performed.
|
|
|
|
///
|
|
|
|
/// Check `/examples/terminal` in the libary for more spesific examples.
|
|
|
|
///
|
|
|
|
/// #Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// extern crate crossterm;
|
2018-03-11 03:33:06 +11:00
|
|
|
/// use crossterm::terminal;
|
2018-01-18 09:06:45 +11:00
|
|
|
///
|
2018-03-11 03:33:06 +11:00
|
|
|
/// let mut term = terminal::terminal();
|
2018-01-31 07:20:11 +11:00
|
|
|
///
|
2018-01-18 09:06:45 +11:00
|
|
|
/// // scroll down by 5 lines
|
|
|
|
/// let size = term.scroll_down(5);
|
2018-01-31 07:20:11 +11:00
|
|
|
///
|
2018-01-18 09:06:45 +11:00
|
|
|
/// ```
|
2018-01-31 07:20:11 +11:00
|
|
|
///
|
2018-03-04 01:40:51 +11:00
|
|
|
pub fn terminal() -> Box<Terminal>
|
|
|
|
{
|
2018-01-31 07:20:11 +11:00
|
|
|
Box::from(Terminal::new())
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|