minicrossterm/src/modules/terminal/terminal.rs

158 lines
4.7 KiB
Rust
Raw Normal View History

//! 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.
use super::*;
use std::fmt;
2018-07-10 03:37:07 +10:00
/// Struct that stores an specific platform implementation for terminal related actions.
2018-07-30 05:30:09 +10:00
///
2018-08-15 07:02:25 +10:00
/// Check `/examples/terminal` in the library for more specific examples.
2018-07-30 05:30:09 +10:00
///
/// ```rust
2018-08-22 02:05:53 +10:00
/// use crossterm::terminal;
///
/// let screen = Screen::default();
/// let term = terminal(&screen);
2018-07-30 05:30:09 +10:00
///
/// term.scroll_down(5);
/// term.scroll_up(4);
/// let (with, height) = term.terminal_size();
///
/// ```
pub struct Terminal<'stdout> {
2018-08-22 05:17:53 +10:00
terminal: Box<ITerminal + Sync + Send>,
2018-08-20 07:14:45 +10:00
screen: &'stdout Arc<TerminalOutput>,
}
impl<'stdout> Terminal<'stdout> {
/// Create new terminal instance whereon terminal related actions can be performed.
2018-08-20 07:14:45 +10:00
pub fn new(screen: &'stdout Arc<TerminalOutput>) -> Terminal<'stdout> {
2018-03-04 01:40:51 +11:00
#[cfg(target_os = "windows")]
2018-08-22 05:17:53 +10:00
let terminal = functions::get_module::<Box<ITerminal + Sync + Send>>(
2018-07-28 17:54:05 +10:00
Box::new(WinApiTerminal::new()),
Box::new(AnsiTerminal::new()),
).unwrap();
2018-03-04 01:40:51 +11:00
#[cfg(not(target_os = "windows"))]
2018-08-22 05:17:53 +10:00
let terminal = Box::from(AnsiTerminal::new()) as Box<ITerminal + Sync + Send>;
2018-07-02 06:43:43 +10:00
Terminal {
terminal,
screen: screen,
2018-07-02 06:43:43 +10:00
}
}
2018-08-15 07:02:25 +10:00
/// Clear the current cursor by specifying the clear type.
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
2018-07-02 06:43:43 +10:00
///
/// // clear all cells in terminal.
/// term.clear(terminal::ClearType::All);
/// // clear all cells from the cursor position downwards in terminal.
/// term.clear(terminal::ClearType::FromCursorDown);
/// // clear all cells from the cursor position upwards in terminal.
/// term.clear(terminal::ClearType::FromCursorUp);
/// // clear current line cells in terminal.
/// term.clear(terminal::ClearType::CurrentLine);
/// // clear all cells from cursor position until new line in terminal.
/// term.clear(terminal::ClearType::UntilNewLine);
/// ```
pub fn clear(&self, clear_type: ClearType) {
self.terminal.clear(clear_type, &self.screen);
}
/// Get the terminal size (x,y).
2018-07-02 06:43:43 +10:00
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
///
/// let size = term.terminal_size();
/// println!("{:?}", size);
2018-07-02 06:43:43 +10:00
///
/// ```
pub fn terminal_size(&self) -> (u16, u16) {
return self.terminal.terminal_size(&self.screen);
}
/// Scroll `n` lines up in the current terminal.
2018-07-02 06:43:43 +10:00
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
2018-07-02 06:43:43 +10:00
///
/// // scroll up by 5 lines
/// let size = term.scroll_up(5);
2018-07-02 06:43:43 +10:00
///
/// ```
pub fn scroll_up(&self, count: i16) {
self.terminal.scroll_up(count, &self.screen);
}
/// Scroll `n` lines up in the current terminal.
2018-07-02 06:43:43 +10:00
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
2018-07-02 06:43:43 +10:00
///
/// // scroll down by 5 lines
/// let size = term.scroll_down(5);
2018-07-02 06:43:43 +10:00
///
/// ```
pub fn scroll_down(&self, count: i16) {
self.terminal.scroll_down(count, &self.screen);
}
/// Set the terminal size. Note that not all terminals can be set to a very small scale.
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
///
/// // Set of the size to X: 10 and Y: 10
/// let size = term.set_size(10,10);
2018-07-02 06:43:43 +10:00
///
/// ```
pub fn set_size(&self, width: i16, height: i16) {
self.terminal.set_size(width, height, &self.screen);
}
/// Exit the current process.
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
///
/// let size = term.exit();
///
/// ```
2018-07-02 06:43:43 +10:00
pub fn exit(&self) {
self.terminal.exit(&self.screen);
2018-06-27 04:21:47 +10:00
}
/// Write any displayable content to the current terminal screen.
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
///
/// let size = term.write("Some text \n Some text on new line");
///
/// ```
pub fn write<D: fmt::Display>(&self, value: D) {
use std::fmt::Write;
2018-07-28 17:54:05 +10:00
let mut string = String::new();
write!(string, "{}", value).unwrap();
self.screen.write_string(string);
}
}
/// Get an terminal implementation whereon terminal related actions could performed.
/// Pass the reference to any screen you want this type to perform actions on.
pub fn terminal<'stdout>(screen: &'stdout Screen) -> Terminal<'stdout> {
Terminal::new(&screen.stdout)
}