2018-03-11 03:33:06 +11:00
|
|
|
//!
|
|
|
|
//! Terminal Examples
|
|
|
|
//!
|
|
|
|
|
|
|
|
extern crate crossterm;
|
|
|
|
|
|
|
|
use crossterm::cursor;
|
2018-07-02 06:43:43 +10:00
|
|
|
use crossterm::terminal::{terminal, ClearType};
|
|
|
|
use crossterm::Context;
|
2018-03-11 03:33:06 +11:00
|
|
|
|
2018-07-02 06:43:43 +10:00
|
|
|
fn print_test_data() {
|
2018-03-11 03:33:06 +11:00
|
|
|
for i in 0..100 {
|
2018-07-02 06:43:43 +10:00
|
|
|
println!("Test data to test terminal: {}", i);
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines in terminal | demonstration
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn clear_all_lines() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Clear all lines in terminal;
|
|
|
|
terminal.clear(ClearType::All);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines from cursor position X:4, Y:4 down | demonstration
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn clear_from_cursor_down() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Set terminal cursor position (see example for more info).
|
2018-07-19 06:32:17 +10:00
|
|
|
cursor::cursor(&context).goto(4, 8);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
// Clear all cells from current cursor position down.
|
|
|
|
terminal.clear(ClearType::FromCursorDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn clear_from_cursor_up() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Set terminal cursor position (see example for more info).
|
2018-07-19 06:32:17 +10:00
|
|
|
cursor::cursor(&context).goto(4, 4);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
// Clear all cells from current cursor position down.
|
|
|
|
terminal.clear(ClearType::FromCursorUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn clear_current_line() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Set terminal cursor position (see example for more info).
|
2018-07-19 06:32:17 +10:00
|
|
|
cursor::cursor(&context).goto(4, 4);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
// Clear current line cells.
|
|
|
|
terminal.clear(ClearType::CurrentLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines from cursor position X:4, Y:7 up | demonstration
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn clear_until_new_line() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Set terminal cursor position (see example for more info).
|
2018-07-19 06:32:17 +10:00
|
|
|
cursor::cursor(&context).goto(4, 20);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
// Clear all the cells until next line.
|
|
|
|
terminal.clear(ClearType::UntilNewLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the the current terminal size | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn print_terminal_size() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal size
|
|
|
|
let terminal_size = terminal.terminal_size();
|
|
|
|
// Print results
|
|
|
|
print!("X: {}, y: {}", terminal_size.0, terminal_size.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the terminal size to width 10, height: 10 | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn set_terminal_size() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
|
2018-07-02 06:43:43 +10:00
|
|
|
terminal.set_size(10, 10);
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll down 10 lines | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn scroll_down() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
print_test_data();
|
2018-07-19 06:32:17 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
// Scroll down 10 lines.
|
|
|
|
terminal.scroll_down(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll down 10 lines | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn scroll_up() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-03-11 03:33:06 +11:00
|
|
|
// Scroll up 10 lines.
|
2018-07-19 06:32:17 +10:00
|
|
|
terminal.scroll_up(5);
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Resize the terminal to X: 10, Y: 10 | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn resize_terminal() {
|
2018-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal size
|
2018-07-02 06:43:43 +10:00
|
|
|
terminal.set_size(10, 10);
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
2018-06-27 04:56:34 +10:00
|
|
|
|
|
|
|
/// exit the current proccess.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn exit() {
|
2018-06-27 04:56:34 +10:00
|
|
|
let context = Context::new();
|
|
|
|
|
|
|
|
// Get terminal
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal(&context);
|
2018-06-27 04:56:34 +10:00
|
|
|
// Get terminal size
|
|
|
|
terminal.exit();
|
|
|
|
}
|