minicrossterm/examples/raw_mode.rs

54 lines
1.4 KiB
Rust
Raw Normal View History

2019-04-13 22:44:31 +10:00
use std::io::{self, stdout, Write};
2018-07-02 06:43:43 +10:00
use std::{thread, time};
2019-09-16 21:34:08 +10:00
use crossterm::{style, AlternateScreen, ClearType, Color, Crossterm};
2019-04-13 22:44:31 +10:00
fn print_wait_screen() -> io::Result<()> {
let crossterm = Crossterm::new();
let terminal = crossterm.terminal();
let cursor = crossterm.cursor();
2018-08-03 20:01:04 +10:00
2019-04-13 22:44:31 +10:00
terminal.clear(ClearType::All)?;
2019-04-13 22:44:31 +10:00
cursor.hide()?;
cursor.goto(0, 0)?;
println!("Welcome to the wait screen.");
2019-04-13 22:44:31 +10:00
cursor.goto(0, 1)?;
println!("Please wait a few seconds until we arrive back at the main screen.");
2019-04-13 22:44:31 +10:00
cursor.goto(0, 2)?;
println!("Progress:");
2019-04-13 22:44:31 +10:00
cursor.goto(0, 3)?;
// 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}`
2019-04-13 22:44:31 +10:00
cursor.goto(10, 2)?;
print!(
"{}",
style(format!("{} of the 5 items processed", i))
.with(Color::Red)
.on(Color::Blue)
);
2019-04-13 22:44:31 +10:00
stdout().flush()?;
// 1 second delay
thread::sleep(time::Duration::from_secs(1));
}
2019-04-13 22:44:31 +10:00
Ok(())
}
2019-04-13 22:44:31 +10:00
pub fn print_wait_screen_on_alternate_window() -> io::Result<()> {
// by passing in 'true' the alternate screen will be in raw modes.
2019-04-13 22:44:31 +10:00
if let Ok(_alternate) = AlternateScreen::to_alternate(true) {
print_wait_screen()?;
} // <- drop alternate screen; this will cause the alternate screen to drop.
2019-04-13 22:44:31 +10:00
Ok(())
2018-07-02 06:43:43 +10:00
}
fn main() {
2019-04-13 22:44:31 +10:00
print_wait_screen_on_alternate_window().unwrap();
}