diff --git a/src/main.rs b/src/main.rs index 82ae787..7277b43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,128 +4,17 @@ mod key; mod line; mod log; mod output; +mod shell; mod prompt; use line::Line; use prompt::Prompt; -use std::fs::File; -use std::io::{Read, Seek, SeekFrom, Write}; +use std::io::Write; use anyhow::Result; use crate::log::*; -fn eval(output: &mut output::Writer, cmd: String, args: Vec<&str>) -> Result { - match cmd.as_str() { - "pwd" => { - let pb = std::env::current_dir()?; - println!("{}", pb.as_path().as_os_str().to_str().unwrap()); - return Ok(true); - } - "cd" => { - let cwd = std::env::current_dir()?; - if args.len() > 0 { - let target = cwd.join(args[0]); - std::env::set_current_dir(target)?; - } - return Ok(true); - } - "printenv" => { - if args.len() > 0 { - let name = args[0]; - match std::env::var(name) { - Ok(val) => { - println!("{}", val); - return Ok(true); - } - Err(e) => { - println!("ERROR {}", e); - return Ok(false); - } - } - } else { - println!("which variable you fucking dork"); - return Ok(false); - } - } - "which" => { - if args.len() > 0 { - let path = std::env::var("path").unwrap(); - let dirs: Vec<&str> = path.split(";").collect(); - for d in dirs { - let dir = std::path::Path::new(d); - let fname = dir.join(args[0]).with_extension("exe"); - if fname.exists() && fname.is_file() { - println!("{}", fname.to_str().unwrap()); - return Ok(true); - } - } - println!("not found: {}", args[0]); - return Ok(false); - } else { - println!("what do you want to look for?"); - return Ok(false); - } - } - "tail" => { - if args.len() > 0 { - let fname = args[0]; - match File::options().read(true).open(fname) { - Ok(mut f) => { - _ = f.seek(SeekFrom::End(0)); - let mut one_byte: [u8; 1] = [0; 1]; - let mut buf: Vec = vec![]; - loop { - match f.read(&mut one_byte) { - Ok(n) => { - if n == 1 { - buf.push(one_byte[0]); - if let Ok(s) = std::str::from_utf8(&buf) { - output.write(s.as_bytes())?; - buf.clear(); - } - } - } - Err(_) => {} - } - } - } - Err(e) => { - println!("failed to open file: {}", e); - return Err(e.into()); - } - } - } else { - println!("need a file name"); - return Ok(false); - } - } - "echo" => { - println!("{}", args.join(" ")); - return Ok(true); - } - _ => { - let mut proc = std::process::Command::new(cmd); - if args.len() > 0 { - proc.args(args); - } - match proc.spawn() { - Ok(mut child) => { - if let Err(e) = child.wait() { - println!("error: {}", e); - return Err(e.into()); - } - } - Err(e) => { - println!("error: {}", e); - return Ok(false); - } - } - return Ok(true); - } - } -} - fn main() -> Result<()> { match Log::file("C:\\Users\\JordanOrelli\\wash.log") { Ok(f) => { @@ -169,7 +58,7 @@ fn main() -> Result<()> { vec![] }; debug!("◇ {} {}", cmd.clone(), args.join(" ")); - match eval(&mut output, cmd.clone(), args.clone()) { + match shell::eval(&mut output, cmd.clone(), args.clone()) { Ok(true) => info!("▷ {} {}", cmd, args.join(" ")), Ok(false) => warn!("▷ {} {}", cmd, args.join(" ")), Err(e) => { diff --git a/src/shell.rs b/src/shell.rs new file mode 100644 index 0000000..27a0c55 --- /dev/null +++ b/src/shell.rs @@ -0,0 +1,117 @@ +use std::fs::File; +use std::io::{Read, Seek, SeekFrom, Write}; +use crate::output; + +use anyhow::Result; + +pub fn eval(output: &mut output::Writer, cmd: String, args: Vec<&str>) -> Result { + match cmd.as_str() { + "pwd" => { + let pb = std::env::current_dir()?; + println!("{}", pb.as_path().as_os_str().to_str().unwrap()); + return Ok(true); + } + "cd" => { + let cwd = std::env::current_dir()?; + if args.len() > 0 { + let target = cwd.join(args[0]); + std::env::set_current_dir(target)?; + } + return Ok(true); + } + "printenv" => { + if args.len() > 0 { + let name = args[0]; + match std::env::var(name) { + Ok(val) => { + println!("{}", val); + return Ok(true); + } + Err(e) => { + println!("ERROR {}", e); + return Ok(false); + } + } + } else { + println!("which variable you fucking dork"); + return Ok(false); + } + } + "which" => { + if args.len() > 0 { + let path = std::env::var("path").unwrap(); + let dirs: Vec<&str> = path.split(";").collect(); + for d in dirs { + let dir = std::path::Path::new(d); + let fname = dir.join(args[0]).with_extension("exe"); + if fname.exists() && fname.is_file() { + println!("{}", fname.to_str().unwrap()); + return Ok(true); + } + } + println!("not found: {}", args[0]); + return Ok(false); + } else { + println!("what do you want to look for?"); + return Ok(false); + } + } + "tail" => { + if args.len() > 0 { + let fname = args[0]; + match File::options().read(true).open(fname) { + Ok(mut f) => { + _ = f.seek(SeekFrom::End(0)); + let mut one_byte: [u8; 1] = [0; 1]; + let mut buf: Vec = vec![]; + loop { + match f.read(&mut one_byte) { + Ok(n) => { + if n == 1 { + buf.push(one_byte[0]); + if let Ok(s) = std::str::from_utf8(&buf) { + output.write(s.as_bytes())?; + buf.clear(); + } + } + } + Err(_) => {} + } + } + } + Err(e) => { + println!("failed to open file: {}", e); + return Err(e.into()); + } + } + } else { + println!("need a file name"); + return Ok(false); + } + } + "echo" => { + println!("{}", args.join(" ")); + return Ok(true); + } + _ => { + let mut proc = std::process::Command::new(cmd); + if args.len() > 0 { + proc.args(args); + } + match proc.spawn() { + Ok(mut child) => { + if let Err(e) = child.wait() { + println!("error: {}", e); + return Err(e.into()); + } + } + Err(e) => { + println!("error: {}", e); + return Ok(false); + } + } + return Ok(true); + } + } +} +