minicrossterm/examples/Crossterm 0.3.1/input/keyboard/async_input.rs

144 lines
3.9 KiB
Rust
Raw Normal View History

extern crate crossterm;
use self::crossterm::input::input;
use self::crossterm::Context;
use self::crossterm::Crossterm;
use crossterm::terminal::ClearType;
use crossterm::raw::IntoRawMode;
2018-07-22 22:55:14 +10:00
use std::{thread, time};
2018-07-22 22:55:14 +10:00
use std::io::{stdout, Read, Write};
use std::time::Duration;
/// this will capture the input until the given key.
2018-07-22 22:55:14 +10:00
pub fn read_async_until() {
2018-07-27 02:37:36 +10:00
let crossterm = Crossterm::new();
2018-07-27 02:37:36 +10:00
// init some modules we use for this demo
let input = crossterm.input();
let terminal = crossterm.terminal();
let mut cursor = crossterm.cursor();
let mut stdout = stdout().into_raw_mode(crossterm.context()).unwrap();
let mut stdin = input.read_until_async(b'\r').bytes();
2018-07-22 22:55:14 +10:00
for i in 0..100 {
2018-07-27 02:37:36 +10:00
terminal.clear(ClearType::All);
cursor.goto(1, 1);
let a = stdin.next();
println!("pressed key: {:?}", a);
if let Some(Ok(b'\r')) = a {
println!("The enter key is hit and program is not listening to input anymore.");
break;
}
if let Some(Ok(b'x')) = a {
println!("The key: x was pressed and program is terminated.");
break;
}
2018-07-27 02:37:36 +10:00
thread::sleep(time::Duration::from_millis(100));
}
}
/// this will read pressed characters async until `x` is typed .
2018-07-22 22:55:14 +10:00
pub fn read_async() {
let context = Context::new();
let input = input(&context);
let mut stdin = input.read_async().bytes();
2018-07-22 22:55:14 +10:00
for i in 0..100 {
let a = stdin.next();
println!("pressed key: {:?}", a);
if let Some(Ok(b'x')) = a {
println!("The key: `x` was pressed and program is terminated.");
break;
}
thread::sleep(time::Duration::from_millis(50));
}
}
2018-07-22 22:55:14 +10:00
pub fn read_async_demo() {
let crossterm = Crossterm::new();
// init some modules we use for this demo
let input = crossterm.input();
let terminal = crossterm.terminal();
let mut cursor = crossterm.cursor();
// put stdout in raw mode so that characters wil not be outputted.
let mut stdout = stdout().into_raw_mode(crossterm.context()).unwrap();
// this will setup the async reading.
let mut stdin = input.read_async().bytes();
// clear terminal and reset the cursor.
terminal.clear(ClearType::All);
cursor.goto(1, 1);
// loop until the enter key (\r) is pressed.
loop {
terminal.clear(ClearType::All);
cursor.goto(1, 1);
// get the next pressed key
let pressed_key = stdin.next();
2018-07-27 02:25:20 +10:00
terminal.write(format!("\r{:?} <- Character pressed", pressed_key));
// check if pressed key is enter (\r)
if let Some(Ok(b'\r')) = pressed_key {
break;
}
// wait 200 ms and reset cursor write
thread::sleep(Duration::from_millis(200));
}
}
2018-07-22 22:55:14 +10:00
pub fn async_reading_on_alternate_screen() {
use crossterm::screen::AlternateScreen;
let crossterm = Crossterm::new();
// init some modules we use for this demo
let input = crossterm.input();
let terminal = crossterm.terminal();
let mut cursor = crossterm.cursor();
// switch to alternate screen
let mut alternate_screen = AlternateScreen::from(crossterm.context());
// put alternate screen in raw mode so that characters wil not be outputted.
let mut raw_screen = alternate_screen.into_raw_mode(crossterm.context());
// this will setup the async reading.
let mut stdin = input.read_async().bytes();
// loop until the enter key (\r) is pressed.
loop {
terminal.clear(ClearType::All);
cursor.goto(1, 1);
// get the next pressed key
let pressed_key = stdin.next();
2018-07-27 02:25:20 +10:00
terminal.write(format!("\r{:?} <- Character pressed", pressed_key));
// check if pressed key is enter (\r)
if let Some(Ok(b'\r')) = pressed_key {
break;
}
// wait 200 ms and reset cursor write
thread::sleep(Duration::from_millis(200));
}
2018-07-22 22:55:14 +10:00
}