diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 61 |
1 files changed, 29 insertions, 32 deletions
diff --git a/src/main.rs b/src/main.rs index d7a2aff..935155c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,15 @@ +#![forbid(unsafe_code)] use crate::prelude::*; + +use crate::result::Result; +use crate::terminal::{Input, Terminal}; + +use async_std::io; use std::collections::HashMap; use std::collections::HashSet; -use std::io; -use std::io::prelude::*; +use std::env; use std::os::unix::fs::PermissionsExt; -use std::process::Command; +use std::process::{self, Command}; #[macro_use] extern crate lalrpop_util; @@ -16,14 +21,9 @@ pub mod result; pub mod terminal; -pub enum Input { - String(String), - End, -} - - fn main() -> Result<()> { - std::process::exit(match repl() { + let result = async_std::task::block_on(async { repl().await }); + process::exit(match result { Ok(()) => 0, Err(ref e) => { eprintln!("{}", e); @@ -33,45 +33,42 @@ fn main() -> Result<()> { } -fn repl() -> Result<()> { +async fn repl() -> Result<()> { println!("Hello, terminal!"); + let mut terminal = Terminal::init(io::stdin())?; + loop { - prompt()?; + prompt().await?; + + let input = terminal.handle_input().await?; - terminal::handle_input_terminal(io::stdin())?; - let input = read()?; match input { - Input::String(string) => execute(&string)?, + Input::String(string) => { + println!("{:?} {}", string, string.len()); + execute(&string).await? + }, Input::End => break, } + + break; } + terminal.cleanup()?; + Ok(()) } -fn prompt() -> Result<()> { +async fn prompt() -> Result<()> { print!("\n$ "); - io::stdout().flush()?; + io::stdout().flush().await?; Ok(()) } -fn read() -> Result<Input> { - let mut input = String::new(); - let n_bytes = io::stdin().read_line(&mut input)?; - - if n_bytes == 0 { - Ok(Input::End) - } else { - Ok(Input::String(input)) - } -} - - -fn execute(input: &str) -> Result<()> { +async fn execute(input: &str) -> Result<()> { let invocation = commandline::InvocationParser::new().parse(input)?; match invocation.as_slice() { @@ -114,12 +111,12 @@ fn execute(input: &str) -> Result<()> { fn read_environment() -> Result<HashMap<String,String>> { - Ok(std::env::vars().collect()) + Ok(env::vars().collect()) } fn get_environment(variable_name: &str) -> Result<Option<String>> { - Ok(std::env::vars() + Ok(env::vars() .find(|(key, _)| key == variable_name) .map(|(_, value)| value)) } |