@@ -1,10 +1,11 @@
1use std::io::{self, stdout, Write};
1use std::io::{self, stdout, Write};
2+use std::path::PathBuf;
3use crossterm::{
4use crossterm::{
5 cursor::position,
6 cursor::position,
6 event::{self, read, Event, KeyCode, KeyEvent},
7 event::{self, read, Event, KeyCode, KeyEvent},
7− execute, style,
8+ execute, queue, style,
8 terminal::{self, disable_raw_mode, enable_raw_mode},
9 terminal::{self, disable_raw_mode, enable_raw_mode},
@@ -14,7 +15,9 @@ const HELP: &str = r#"
14 - Move cursor with h, j, k, l
15 - Move cursor with h, j, k, l
17−pub struct Editor {}
18+pub struct Editor {
19impl Editor {
22impl Editor {
20 pub fn read_char() -> Result<char> {
23 pub fn read_char() -> Result<char> {
@@ -29,7 +32,7 @@ impl Editor {
32− pub fn print_events() -> Result<()> {
35+ pub async fn print_events() -> Result<()> {
34 // Handle key events
37 // Handle key events
35 match Editor::read_char()? {
38 match Editor::read_char()? {
@@ -56,12 +59,35 @@ impl Editor {
56 pub fn run() -> Result<()> {
59 pub fn run() -> Result<()> {
57 enable_raw_mode()?;
60 enable_raw_mode()?;
59− // used for clearing the screen
60− execute!(io::stdout(), terminal::EnterAlternateScreen)?;
61− println!("{}", HELP);
62 let mut stdout = stdout();
62 let mut stdout = stdout();
64+ execute!(stdout, terminal::EnterAlternateScreen)?;
68+ // Same number of threads as there are CPU cores.
69+ let num_threads = num_cpus::get().max(1);
71+ // A channel that sends the shutdown signal.
72+ let (s, r) = piper::chan::<()>(0);
73+ let mut threads = Vec::new();
75+ // Create an executor thread pool.
76+ for _ in 0..num_threads {
77+ // Spawn an executor thread that waits for the shutdown signal.
79+ threads.push(thread::spawn(move || smol::run(r.recv())));
82+ // No need to `run()`, now we can just block on the main future.
63− if let Err(e) = Editor::print_events() {
83+ smol::block_on(Editor::print_events());
64− println!("Error: {:?}\r", e);
84+
85+ // Send a shutdown signal.
88+ // Wait for threads to finish.
67 disable_raw_mode()
93 disable_raw_mode()