movin stuff into a shell module

parse-tree
Jordan Orelli 2 years ago
parent 37224f63c5
commit 891ed46af9

@ -4,128 +4,17 @@ mod key;
mod line; mod line;
mod log; mod log;
mod output; mod output;
mod shell;
mod prompt; mod prompt;
use line::Line; use line::Line;
use prompt::Prompt; use prompt::Prompt;
use std::fs::File; use std::io::Write;
use std::io::{Read, Seek, SeekFrom, Write};
use anyhow::Result; use anyhow::Result;
use crate::log::*; use crate::log::*;
fn eval(output: &mut output::Writer, cmd: String, args: Vec<&str>) -> Result<bool> {
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<u8> = 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<()> { fn main() -> Result<()> {
match Log::file("C:\\Users\\JordanOrelli\\wash.log") { match Log::file("C:\\Users\\JordanOrelli\\wash.log") {
Ok(f) => { Ok(f) => {
@ -169,7 +58,7 @@ fn main() -> Result<()> {
vec![] vec![]
}; };
debug!("◇ {} {}", cmd.clone(), args.join(" ")); 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(true) => info!("▷ {} {}", cmd, args.join(" ")),
Ok(false) => warn!("▷ {} {}", cmd, args.join(" ")), Ok(false) => warn!("▷ {} {}", cmd, args.join(" ")),
Err(e) => { Err(e) => {

@ -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<bool> {
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<u8> = 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);
}
}
}
Loading…
Cancel
Save