minicrossterm/examples/terminal/alternate_screen.rs

74 lines
2.0 KiB
Rust
Raw Normal View History

extern crate crossterm;
use crossterm::style::Color;
use crossterm::terminal::{self, ClearType};
2018-08-03 20:01:04 +10:00
use crossterm::Crossterm;
2018-07-02 06:43:43 +10:00
use std::io::{stdout, Write};
use std::{thread, time};
2018-08-03 20:01:04 +10:00
fn print_wait_screen(crossterm: &mut Crossterm) {
let mut terminal = crossterm.terminal();
let mut cursor = crossterm.cursor();
terminal.clear(ClearType::All);
2018-07-02 06:43:43 +10:00
cursor.goto(0, 0);
cursor.hide();
2018-07-02 06:43:43 +10:00
terminal.write(
"Welcome to the wait screen.\n\
Please wait a few seconds until we arrive back at the main screen.\n\
Progress: ",
);
// print some progress example.
2018-07-02 06:43:43 +10:00
for i in 1..5 {
// print the current counter at the line of `Seconds to Go: {counter}`
2018-07-02 06:43:43 +10:00
cursor
.goto(10, 2)
2018-08-03 20:01:04 +10:00
.print(crossterm.paint(format!("{} of the 5 items processed", i)).with(Color::Red).on(Color::Blue));
// 1 second delay
thread::sleep(time::Duration::from_secs(1));
}
stdout().flush();
}
2018-07-13 03:48:13 +10:00
/// print wait screen on alternate screen, then swich back.
2018-08-03 20:01:04 +10:00
pub fn print_wait_screen_on_alternate_window() {
2018-08-03 20:01:04 +10:00
let mut term = Crossterm::new();
term.to_alternate_screen();
term.write(b"test");
print_wait_screen(&mut term);
}
2018-07-13 03:48:13 +10:00
/// some stress test switch from and to alternate screen.
2018-07-02 06:43:43 +10:00
pub fn switch_between_main_and_alternate_screen() {
{
2018-08-03 20:01:04 +10:00
let mut term = Crossterm::new();
let mut cursor = term.cursor();
// create new alternate screen instance and switch to the alternate screen.
2018-08-03 20:01:04 +10:00
let alternate = term.to_alternate_screen();
{ cursor.goto(0, 0); }
write!(term, "we are at the alternate screen!");
thread::sleep(time::Duration::from_secs(3));
2018-08-03 20:01:04 +10:00
term.to_main_screen();
write!(term, "we are at the alternate screen!");
thread::sleep(time::Duration::from_secs(3));
2018-08-03 20:01:04 +10:00
term.to_alternate_screen();
write!(term, "we are at the alternate screen!");
thread::sleep(time::Duration::from_secs(3));
2018-08-03 20:01:04 +10:00
} // <- Crossterm goes out of scope.
println!("Whe are back at the main screen");
2018-07-02 06:43:43 +10:00
}