minicrossterm/examples/terminal.rs

126 lines
2.8 KiB
Rust
Raw Normal View History

//!
//! Terminal Examples
//!
2019-09-19 03:31:12 +10:00
#![allow(dead_code)]
2019-09-19 03:31:12 +10:00
use crossterm::{cursor, terminal, ClearType, Result};
2018-07-02 06:43:43 +10:00
fn print_test_data() {
for i in 0..100 {
2018-07-02 06:43:43 +10:00
println!("Test data to test terminal: {}", i);
}
}
/// Clear all lines in terminal | demonstration
2019-09-19 03:31:12 +10:00
fn clear_all_lines() -> Result<()> {
let terminal = terminal();
print_test_data();
// Clear all lines in terminal;
2019-09-19 03:31:12 +10:00
terminal.clear(ClearType::All)
}
/// 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<()> {
let terminal = terminal();
print_test_data();
// Set terminal cursor position (see example for more info).
2019-04-13 22:44:31 +10:00
cursor().goto(4, 8)?;
// Clear all cells from current cursor position down.
2019-09-19 03:31:12 +10:00
terminal.clear(ClearType::FromCursorDown)
}
/// 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<()> {
let terminal = terminal();
print_test_data();
// Set terminal cursor position (see example for more info).
2019-04-13 22:44:31 +10:00
cursor().goto(4, 4)?;
// Clear all cells from current cursor position down.
2019-09-19 03:31:12 +10:00
terminal.clear(ClearType::FromCursorUp)
}
/// 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<()> {
let terminal = terminal();
print_test_data();
// Set terminal cursor position (see example for more info).
2019-04-13 22:44:31 +10:00
cursor().goto(4, 3)?;
// Clear current line cells.
2019-09-19 03:31:12 +10:00
terminal.clear(ClearType::CurrentLine)
}
/// 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<()> {
let terminal = terminal();
print_test_data();
// Set terminal cursor position (see example for more info).
2019-04-13 22:44:31 +10:00
cursor().goto(4, 20)?;
// Clear all the cells until next line.
2019-09-19 03:31:12 +10:00
terminal.clear(ClearType::UntilNewLine)
}
/// Print the the current terminal size | demonstration.
2019-09-19 21:12:16 +10:00
fn print_terminal_size() -> Result<()> {
let terminal = terminal();
// Get terminal size
2019-09-19 21:12:16 +10:00
let (width, height) = terminal.size()?;
2018-08-03 20:01:04 +10: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(())
}
/// Set the terminal size to width 10, height: 10 | demonstration.
2019-09-19 03:31:12 +10:00
fn set_terminal_size() -> Result<()> {
let terminal = terminal();
2019-09-19 03:31:12 +10:00
terminal.set_size(10, 10)
}
/// Scroll down 10 lines | demonstration.
2019-09-19 03:31:12 +10:00
fn scroll_down() -> Result<()> {
let terminal = terminal();
print_test_data();
2018-07-19 06:32:17 +10:00
// Scroll down 10 lines.
2019-09-19 03:31:12 +10:00
terminal.scroll_down(10)
}
/// Scroll down 10 lines | demonstration.
2019-09-19 03:31:12 +10:00
fn scroll_up() -> Result<()> {
let terminal = terminal();
print_test_data();
// Scroll up 10 lines.
2019-09-19 03:31:12 +10:00
terminal.scroll_up(10)
}
2018-06-27 04:56:34 +10:00
/// exit the current proccess.
2019-09-19 03:31:12 +10:00
fn exit() {
let terminal = terminal();
2018-06-27 04:56:34 +10:00
terminal.exit();
}
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()
}