on master help sign in
loop is now async
2985398Jan Hrastnik
helix-term/src/editor.rs+34−8
@@ -1,10 +1,11 @@
1use std::io::{self, stdout, Write};
1use std::io::{self, stdout, Write};
2+use std::path::PathBuf;
2
3
3use crossterm::{
4use crossterm::{
4 cursor,
5 cursor,
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},
9 Result,
10 Result,
10};
11};
@@ -14,7 +15,9 @@ const HELP: &str = r#"
14 - Move cursor with h, j, k, l
15 - Move cursor with h, j, k, l
15"#;
16"#;
16
17
17−pub struct Editor {}
18+pub struct Editor {
19+ file: PathBuf,
20+}
18
21
19impl Editor {
22impl Editor {
20 pub fn read_char() -> Result<char> {
23 pub fn read_char() -> Result<char> {
@@ -29,7 +32,7 @@ impl Editor {
29 }
32 }
30 }
33 }
31
34
32− pub fn print_events() -> Result<()> {
35+ pub async fn print_events() -> Result<()> {
33 loop {
36 loop {
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()?;
58
61
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();
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.
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.
86+ drop(s);
87+
88+ // Wait for threads to finish.
89+ for t in threads {
90+ t.join().unwrap();
65 }
91 }
66
92
67 disable_raw_mode()
93 disable_raw_mode()
helix-term/src/main.rs+1
@@ -20,6 +20,7 @@ pub struct Args {
20
20
21fn main() -> Result<(), Error> {
21fn main() -> Result<(), Error> {
22 let args: Args = argh::from_env();
22 let args: Args = argh::from_env();
23+ println!("{:?}", args.files);
23
24
24 editor::Editor::run()?;
25 editor::Editor::run()?;
25
26