@@ -1,14 +1,15 @@
1−use std::io::{self, stdout, Write};
2−use std::path::PathBuf;
4use crossterm::{
1use crossterm::{
6 cursor::position,
3 cursor::position,
7− event::{self, read, Event, KeyCode, KeyEvent},
4+ event::{self, read, Event, EventStream, KeyCode, KeyEvent},
8 execute, queue, style,
5 execute, queue, style,
9 terminal::{self, disable_raw_mode, enable_raw_mode},
6 terminal::{self, disable_raw_mode, enable_raw_mode},
9+use futures::{future::FutureExt, select, StreamExt};
10+use std::io::{self, stdout, Write};
11+use std::path::PathBuf;
12+use std::time::Duration;
13const HELP: &str = r#"
14const HELP: &str = r#"
14 - Use q to quit
15 - Use q to quit
@@ -20,40 +21,40 @@ pub struct Editor {
22impl Editor {
23impl Editor {
23− pub fn read_char() -> Result<char> {
24+ pub async fn print_events() {
24− loop {
25+ let mut reader = EventStream::new();
25− if let Ok(Event::Key(KeyEvent {
26− code: KeyCode::Char(c),
35− pub async fn print_events() -> Result<()> {
37 // Handle key events
27 // Handle key events
38− match Editor::read_char()? {
28+ let mut event = reader.next().await;
31+ if let Event::Key(KeyEvent {
32+ code: KeyCode::Char(c),
39− 'h' => execute!(io::stdout(), cursor::MoveLeft(1))?,
37+ 'h' => execute!(io::stdout(), cursor::MoveLeft(1)).unwrap(),
40− 'j' => execute!(io::stdout(), cursor::MoveDown(1))?,
38+ 'j' => execute!(io::stdout(), cursor::MoveDown(1)).unwrap(),
41− 'k' => execute!(io::stdout(), cursor::MoveUp(1))?,
39+ 'k' => execute!(io::stdout(), cursor::MoveUp(1)).unwrap(),
42− 'l' => execute!(io::stdout(), cursor::MoveRight(1))?,
40+ 'l' => execute!(io::stdout(), cursor::MoveRight(1)).unwrap(),
44− execute!(
42+ execute!(
45− io::stdout(),
43+ io::stdout(),
46− style::ResetColor,
44+ style::ResetColor,
47− cursor::Show,
45+ cursor::Show,
48− terminal::LeaveAlternateScreen
46+ terminal::LeaveAlternateScreen
50+ _ => println!("{:?}", x),
52− _ => println!("use 'q' to quit."),
54+ Some(Err(x)) => panic!(x),
59 pub fn run() -> Result<()> {
60 pub fn run() -> Result<()> {
@@ -85,6 +86,8 @@ impl Editor {
85 // Send a shutdown signal.
86 // Send a shutdown signal.
89+ execute!(stdout, terminal::LeaveAlternateScreen)?;
88 // Wait for threads to finish.
91 // Wait for threads to finish.
89 for t in threads {
92 for t in threads {
90 t.join().unwrap();
93 t.join().unwrap();