-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from wadeking98/feat-simplify-code
Feat simplify code
- Loading branch information
Showing
8 changed files
with
364 additions
and
468 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::{io::stdin, sync::Arc}; | ||
|
||
use rustyline::{history::MemHistory, Editor}; | ||
use termion::{ | ||
event::{Event, Key}, | ||
input::TermReadEventsAndRaw, | ||
}; | ||
use tokio::{ | ||
sync::{ | ||
oneshot::{self, error::RecvError}, | ||
Mutex, | ||
}, | ||
task, | ||
}; | ||
|
||
pub async fn read_line( | ||
rl: Arc<Mutex<Editor<(), MemHistory>>>, | ||
prompt: Option<&str>, | ||
) -> Result<String, RecvError> { | ||
let (tx, rx) = oneshot::channel::<String>(); | ||
let input_prompt = match prompt { | ||
Some(s) => String::from(s), | ||
None => String::new(), | ||
}; | ||
tokio::spawn(async move { | ||
let mut reader = rl.lock().await; | ||
|
||
let raw_content = reader.readline(&input_prompt); | ||
|
||
let content = match raw_content { | ||
Ok(line) => line, | ||
Err(_) => String::from(""), | ||
}; | ||
reader.add_history_entry(content.clone()).unwrap(); | ||
tx.send(content + "\n") | ||
.unwrap_or_else(|err| eprintln!("Error from readline handler: {err}")); | ||
}); | ||
rx.await | ||
} | ||
|
||
pub async fn handle_key_input() -> Result<Option<(Key, Vec<u8>)>, RecvError> { | ||
let (tx, rx) = oneshot::channel::<Option<(Key, Vec<u8>)>>(); | ||
task::spawn(async move { | ||
let key_input = stdin().events_and_raw().next(); | ||
let cleaned = match key_input { | ||
Some(key) => match key { | ||
Ok((Event::Key(k), raw)) => Some((k, raw)), | ||
Err(_) => None, | ||
_ => None, | ||
}, | ||
|
||
None => None, | ||
}; | ||
tx.send(cleaned) | ||
.unwrap_or_else(|_| eprintln!("Error from raw readline handler")); | ||
}); | ||
rx.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod input; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.