@@ -1,14 +1,15 @@
1−use std::io::{self, stdout, Write};
2−use std::path::PathBuf;
3−
41use crossterm::{
52 cursor,
63 cursor::position,
7− event::{self, read, Event, KeyCode, KeyEvent},
4+ event::{self, read, Event, EventStream, KeyCode, KeyEvent},
85 execute, queue, style,
96 terminal::{self, disable_raw_mode, enable_raw_mode},
107 Result,
118};
9+use futures::{future::FutureExt, select, StreamExt};
10+use std::io::{self, stdout, Write};
11+use std::path::PathBuf;
12+use std::time::Duration;
1213
1314const HELP: &str = r#"
1415 - Use q to quit
@@ -20,40 +21,40 @@ pub struct Editor {
2021}
2122
2223impl Editor {
23− pub fn read_char() -> Result<char> {
24− loop {
25− if let Ok(Event::Key(KeyEvent {
26− code: KeyCode::Char(c),
27− ..
28− })) = event::read()
29− {
30− return Ok(c);
31− }
32− }
33− }
34−
35− pub async fn print_events() -> Result<()> {
24+ pub async fn print_events() {
25+ let mut reader = EventStream::new();
3626 loop {
3727 // Handle key events
38− match Editor::read_char()? {
39− 'h' => execute!(io::stdout(), cursor::MoveLeft(1))?,
40− 'j' => execute!(io::stdout(), cursor::MoveDown(1))?,
41− 'k' => execute!(io::stdout(), cursor::MoveUp(1))?,
42− 'l' => execute!(io::stdout(), cursor::MoveRight(1))?,
43− 'q' => {
44− execute!(
45− io::stdout(),
46− style::ResetColor,
47− cursor::Show,
48− terminal::LeaveAlternateScreen
49− )?;
50− break;
28+ let mut event = reader.next().await;
29+ match event {
30+ Some(Ok(x)) => {
31+ if let Event::Key(KeyEvent {
32+ code: KeyCode::Char(c),
33+ ..
34+ }) = x
35+ {
36+ match c {
37+ 'h' => execute!(io::stdout(), cursor::MoveLeft(1)).unwrap(),
38+ 'j' => execute!(io::stdout(), cursor::MoveDown(1)).unwrap(),
39+ 'k' => execute!(io::stdout(), cursor::MoveUp(1)).unwrap(),
40+ 'l' => execute!(io::stdout(), cursor::MoveRight(1)).unwrap(),
41+ 'q' => {
42+ execute!(
43+ io::stdout(),
44+ style::ResetColor,
45+ cursor::Show,
46+ terminal::LeaveAlternateScreen
47+ );
48+ break;
49+ }
50+ _ => println!("{:?}", x),
51+ }
52+ }
5153 }
52− _ => println!("use 'q' to quit."),
54+ Some(Err(x)) => panic!(x),
55+ None => break,
5356 }
5457 }
55−
56− Ok(())
5758 }
5859
5960 pub fn run() -> Result<()> {
@@ -85,6 +86,8 @@ impl Editor {
8586 // Send a shutdown signal.
8687 drop(s);
8788
89+ execute!(stdout, terminal::LeaveAlternateScreen)?;
90+
8891 // Wait for threads to finish.
8992 for t in threads {
9093 t.join().unwrap();