@@ -1,10 +1,11 @@
11use std::io::{self, stdout, Write};
2+use std::path::PathBuf;
23
34use crossterm::{
45 cursor,
56 cursor::position,
67 event::{self, read, Event, KeyCode, KeyEvent},
7− execute, style,
8+ execute, queue, style,
89 terminal::{self, disable_raw_mode, enable_raw_mode},
910 Result,
1011};
@@ -14,7 +15,9 @@ const HELP: &str = r#"
1415 - Move cursor with h, j, k, l
1516"#;
1617
17−pub struct Editor {}
18+pub struct Editor {
19+ file: PathBuf,
20+}
1821
1922impl Editor {
2023 pub fn read_char() -> Result<char> {
@@ -29,7 +32,7 @@ impl Editor {
2932 }
3033 }
3134
32− pub fn print_events() -> Result<()> {
35+ pub async fn print_events() -> Result<()> {
3336 loop {
3437 // Handle key events
3538 match Editor::read_char()? {
@@ -56,12 +59,35 @@ impl Editor {
5659 pub fn run() -> Result<()> {
5760 enable_raw_mode()?;
5861
59− // used for clearing the screen
60− execute!(io::stdout(), terminal::EnterAlternateScreen)?;
61− println!("{}", HELP);
6262 let mut stdout = stdout();
63− if let Err(e) = Editor::print_events() {
64− println!("Error: {:?}\r", e);
63+
64+ execute!(stdout, terminal::EnterAlternateScreen)?;
65+
66+ use std::thread;
67+
68+ // Same number of threads as there are CPU cores.
69+ let num_threads = num_cpus::get().max(1);
70+
71+ // A channel that sends the shutdown signal.
72+ let (s, r) = piper::chan::<()>(0);
73+ let mut threads = Vec::new();
74+
75+ // Create an executor thread pool.
76+ for _ in 0..num_threads {
77+ // Spawn an executor thread that waits for the shutdown signal.
78+ let r = r.clone();
79+ threads.push(thread::spawn(move || smol::run(r.recv())));
80+ }
81+
82+ // No need to `run()`, now we can just block on the main future.
83+ smol::block_on(Editor::print_events());
84+
85+ // Send a shutdown signal.
86+ drop(s);
87+
88+ // Wait for threads to finish.
89+ for t in threads {
90+ t.join().unwrap();
6591 }
6692
6793 disable_raw_mode()