2018-03-11 03:33:06 +11:00
|
|
|
//!
|
|
|
|
//! Terminal Examples
|
|
|
|
//!
|
|
|
|
|
2019-09-19 03:31:12 +10:00
|
|
|
#![allow(dead_code)]
|
2018-03-11 03:33:06 +11:00
|
|
|
|
2019-09-19 03:31:12 +10:00
|
|
|
use crossterm::{cursor, terminal, ClearType, Result};
|
2019-01-28 07:16:14 +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
|
2019-09-19 03:31:12 +10:00
|
|
|
fn clear_all_lines() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Clear all lines in terminal;
|
2019-09-19 03:31:12 +10:00
|
|
|
terminal.clear(ClearType::All)
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines from cursor position X:4, Y:4 down | demonstration
|
2019-09-19 03:31:12 +10:00
|
|
|
fn clear_from_cursor_down() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-11-22 03:48:22 +11:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Set terminal cursor position (see example for more info).
|
2019-04-13 22:44:31 +10:00
|
|
|
cursor().goto(4, 8)?;
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
// Clear all cells from current cursor position down.
|
2019-09-19 03:31:12 +10:00
|
|
|
terminal.clear(ClearType::FromCursorDown)
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
2019-09-19 03:31:12 +10:00
|
|
|
fn clear_from_cursor_up() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Set terminal cursor position (see example for more info).
|
2019-04-13 22:44:31 +10:00
|
|
|
cursor().goto(4, 4)?;
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
// Clear all cells from current cursor position down.
|
2019-09-19 03:31:12 +10:00
|
|
|
terminal.clear(ClearType::FromCursorUp)
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines from cursor position X:4, Y:4 up | demonstration
|
2019-09-19 03:31:12 +10:00
|
|
|
fn clear_current_line() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Set terminal cursor position (see example for more info).
|
2019-04-13 22:44:31 +10:00
|
|
|
cursor().goto(4, 3)?;
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
// Clear current line cells.
|
2019-09-19 03:31:12 +10:00
|
|
|
terminal.clear(ClearType::CurrentLine)
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all lines from cursor position X:4, Y:7 up | demonstration
|
2019-09-19 03:31:12 +10:00
|
|
|
fn clear_until_new_line() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Set terminal cursor position (see example for more info).
|
2019-04-13 22:44:31 +10:00
|
|
|
cursor().goto(4, 20)?;
|
2018-03-11 03:33:06 +11:00
|
|
|
|
|
|
|
// Clear all the cells until next line.
|
2019-09-19 03:31:12 +10:00
|
|
|
terminal.clear(ClearType::UntilNewLine)
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the the current terminal size | demonstration.
|
2019-09-19 21:12:16 +10:00
|
|
|
fn print_terminal_size() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Get terminal size
|
2019-09-19 21:12:16 +10:00
|
|
|
let (width, height) = terminal.size()?;
|
2018-08-03 20:01:04 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
// Print results
|
2018-08-03 20:01:04 +10:00
|
|
|
print!("X: {}, y: {}", width, height);
|
2019-09-19 21:12:16 +10:00
|
|
|
Ok(())
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the terminal size to width 10, height: 10 | demonstration.
|
2019-09-19 03:31:12 +10:00
|
|
|
fn set_terminal_size() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-03-11 03:33:06 +11:00
|
|
|
|
2019-09-19 03:31:12 +10:00
|
|
|
terminal.set_size(10, 10)
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll down 10 lines | demonstration.
|
2019-09-19 03:31:12 +10:00
|
|
|
fn scroll_down() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
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
|
|
|
// Scroll down 10 lines.
|
2019-09-19 03:31:12 +10:00
|
|
|
terminal.scroll_down(10)
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll down 10 lines | demonstration.
|
2019-09-19 03:31:12 +10:00
|
|
|
fn scroll_up() -> Result<()> {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-03-11 03:33:06 +11:00
|
|
|
print_test_data();
|
|
|
|
|
|
|
|
// Scroll up 10 lines.
|
2019-09-19 03:31:12 +10:00
|
|
|
terminal.scroll_up(10)
|
2018-03-11 03:33:06 +11:00
|
|
|
}
|
|
|
|
|
2018-06-27 04:56:34 +10:00
|
|
|
/// exit the current proccess.
|
2019-09-19 03:31:12 +10:00
|
|
|
fn exit() {
|
2019-01-01 05:55:48 +11:00
|
|
|
let terminal = terminal();
|
2018-06-27 04:56:34 +10:00
|
|
|
terminal.exit();
|
|
|
|
}
|
2019-01-28 07:16:14 +11:00
|
|
|
|
2019-09-17 18:50:39 +10:00
|
|
|
// cargo run --example terminal
|
2019-09-19 03:31:12 +10:00
|
|
|
fn main() -> Result<()> {
|
|
|
|
scroll_down()
|
2019-01-28 07:16:14 +11:00
|
|
|
}
|