2018-06-14 05:02:09 +10:00
|
|
|
extern crate crossterm;
|
|
|
|
|
2018-07-04 06:48:04 +10:00
|
|
|
use crossterm::style::Color;
|
2018-06-14 05:02:09 +10:00
|
|
|
use crossterm::cursor::cursor;
|
2018-07-02 06:43:43 +10:00
|
|
|
use crossterm::screen::AlternateScreen;
|
2018-06-14 05:02:09 +10:00
|
|
|
use crossterm::terminal::{self, ClearType};
|
2018-07-02 06:43:43 +10:00
|
|
|
use crossterm::Context;
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-07-02 06:43:43 +10:00
|
|
|
use std::io::{stdout, Write};
|
2018-06-23 05:04:30 +10:00
|
|
|
use std::rc::Rc;
|
2018-07-02 06:43:43 +10:00
|
|
|
use std::{thread, time};
|
2018-07-02 06:40:07 +10:00
|
|
|
|
2018-07-02 06:43:43 +10:00
|
|
|
fn print_wait_screen(context: Rc<Context>) {
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut terminal = terminal::terminal(&context);
|
2018-07-02 06:40:07 +10:00
|
|
|
terminal.clear(ClearType::All);
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut cursor = cursor(&context);
|
2018-07-02 06:43:43 +10:00
|
|
|
cursor.goto(0, 0);
|
2018-07-02 06:40:07 +10:00
|
|
|
cursor.hide();
|
2018-06-14 05:02:09 +10:00
|
|
|
|
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: ",
|
|
|
|
);
|
2018-06-14 05:02:09 +10:00
|
|
|
|
|
|
|
// print some progress example.
|
2018-07-02 06:43:43 +10:00
|
|
|
for i in 1..5 {
|
2018-06-14 05:02:09 +10:00
|
|
|
// 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-07-04 06:48:04 +10:00
|
|
|
.print(terminal.paint(format!("{} of the 5 items processed", i)).with(Color::Red).on(Color::Blue));
|
2018-06-14 05:02:09 +10:00
|
|
|
|
|
|
|
// 1 second delay
|
|
|
|
thread::sleep(time::Duration::from_secs(1));
|
|
|
|
}
|
2018-07-04 06:48:04 +10:00
|
|
|
|
|
|
|
stdout().flush();
|
2018-06-14 05:02:09 +10:00
|
|
|
}
|
|
|
|
|
2018-07-13 03:48:13 +10:00
|
|
|
/// print wait screen on alternate screen, then swich back.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn print_wait_screen_on_alternate_window(context: Rc<Context>) {
|
2018-06-14 05:02:09 +10:00
|
|
|
// create scope. If this scope ends the screen will be switched back to mainscreen.
|
|
|
|
// because `AlternateScreen` switches back to main screen when switching back.
|
|
|
|
{
|
|
|
|
// create new alternate screen instance and switch to the alternate screen.
|
2018-06-23 05:04:30 +10:00
|
|
|
let mut screen = AlternateScreen::from(context.clone());
|
2018-06-14 05:02:09 +10:00
|
|
|
|
2018-07-04 06:48:04 +10:00
|
|
|
write!(screen, "test");
|
|
|
|
println!();
|
2018-06-14 05:02:09 +10:00
|
|
|
// Print the wait screen.
|
2018-06-23 05:04:30 +10:00
|
|
|
print_wait_screen(context.clone());
|
2018-07-13 03:48:13 +10:00
|
|
|
}
|
2018-06-14 05:02:09 +10:00
|
|
|
}
|
|
|
|
|
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-06-17 04:10:51 +10:00
|
|
|
let context = Context::new();
|
2018-07-19 06:32:17 +10:00
|
|
|
let mut cursor = cursor(&context);
|
2018-06-14 05:02:09 +10:00
|
|
|
|
|
|
|
{
|
|
|
|
// create new alternate screen instance and switch to the alternate screen.
|
2018-06-23 05:04:30 +10:00
|
|
|
let mut screen = AlternateScreen::from(context.clone());
|
2018-07-02 06:43:43 +10:00
|
|
|
cursor.goto(0, 0);
|
2018-06-14 05:02:09 +10:00
|
|
|
write!(screen, "we are at the alternate screen!");
|
|
|
|
screen.flush();
|
|
|
|
thread::sleep(time::Duration::from_secs(3));
|
|
|
|
|
|
|
|
screen.to_main();
|
|
|
|
write!(screen, "we are at the main screen!");
|
|
|
|
screen.flush();
|
|
|
|
thread::sleep(time::Duration::from_secs(3));
|
|
|
|
|
|
|
|
screen.to_alternate();
|
|
|
|
write!(screen, "we are at the alternate screen!");
|
|
|
|
screen.flush();
|
|
|
|
thread::sleep(time::Duration::from_secs(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Whe are back at the main screen");
|
2018-07-02 06:43:43 +10:00
|
|
|
}
|