From 51e921b085c6021fd581c16dc214ddd585bc0088 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sat, 11 May 2019 00:00:33 -0700 Subject: [PATCH] A bit more infra --- Cargo.lock | 25 +++++++++++++++++++++++++ Cargo.toml | 2 ++ history.txt | 41 +++++++++++++++++++++++++++++++++++++++++ src/commands.rs | 1 + src/commands/cd.rs | 21 +++++++++++++++++++++ src/commands/command.rs | 1 + src/commands/ls.rs | 1 + src/commands/ps.rs | 1 + src/env/environment.rs | 3 ++- src/main.rs | 32 +++++++++++++++++++++----------- 10 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 src/commands/cd.rs diff --git a/Cargo.lock b/Cargo.lock index 1454300e56..7077cc6109 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -170,6 +170,11 @@ dependencies = [ "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crossbeam-utils" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "csv" version = "1.0.7" @@ -209,6 +214,11 @@ dependencies = [ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dunce" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "either" version = "1.5.2" @@ -361,10 +371,12 @@ dependencies = [ "chrono-tz 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "conch-parser 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "derive-new 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "dunce 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "nom 5.0.0-beta1 (registry+https://github.com/rust-lang/crates.io-index)", "prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustyline 4.0.0 (git+https://github.com/jonathandturner/rustyline.git)", + "subprocess 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "sysinfo 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -558,6 +570,16 @@ name = "static_assertions" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "subprocess" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.15.34" @@ -718,10 +740,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" "checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150" "checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9" +"checksum crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "677d453a17e8bd2b913fa38e8b9cf04bcdbb5be790aa294f2389661d72036015" "checksum csv 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9044e25afb0924b5a5fc5511689b0918629e85d68ea591e5e87fbf1e85ea1b3b" "checksum csv-core 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa5cdef62f37e6ffe7d1f07a381bc0db32b7a3ff1cac0de56cb0d81e71f53d65" "checksum derive-new 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6ca414e896ae072546f4d789f452daaecf60ddee4c9df5dc6d5936d769e3d87c" "checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" +"checksum dunce 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0ad6bf6a88548d1126045c413548df1453d9be094a8ab9fd59bf1fdd338da4f" "checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b" "checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" @@ -765,6 +789,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)" = "a72e9b96fa45ce22a4bc23da3858dfccfd60acd28a25bcd328a98fdd6bea43fd" "checksum stackvector 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c049c77bf85fbc036484c97b008276d539d9ebff9dfbde37b632ebcd5b8746b6" "checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5" +"checksum subprocess 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "28fc0f40f0c0da73339d347aa7d6d2b90341a95683a47722bc4eebed71ff3c00" "checksum syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)" = "a1393e4a97a19c01e900df2aec855a29f71cf02c402e2f443b8d2747c25c5dbe" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum sysinfo 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6d88e417391431019773011a31a6c967538da388782b7711f2f6fafd9e601e55" diff --git a/Cargo.toml b/Cargo.toml index cccfdf8d09..c2f7aae581 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,5 @@ itertools = "0.8.0" ansi_term = "0.11.0" conch-parser = "0.1.0" nom = "5.0.0-beta1" +subprocess = "0.1.18" +dunce = "1.0.0" diff --git a/history.txt b/history.txt index ae997c8ba9..b2da394994 100644 --- a/history.txt +++ b/history.txt @@ -23,3 +23,44 @@ ls | zomg ls soms ls something ls something | grep +dir +npm --help +cd target +dir +ls +cd target +cd .. +dir +cd target +cd .. +cd target +cd .. +ls +dir +cd target +ls +cd .. +ls +cd target +cd .. +cd target +cd .. +cd target +cd .. +cd target +cd .. +cd target +cd .. +cd target +cd .. +cd target +cd .. +ls +cd target +cd .. +cargo build +cargo build --verbose +ls +cargo +cargo build +cargo run diff --git a/src/commands.rs b/src/commands.rs index 817bce375d..9d3f4165f2 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,3 +1,4 @@ +crate mod cd; crate mod command; crate mod ls; crate mod ps; diff --git a/src/commands/cd.rs b/src/commands/cd.rs new file mode 100644 index 0000000000..43f268372f --- /dev/null +++ b/src/commands/cd.rs @@ -0,0 +1,21 @@ +use crate::errors::ShellError; +use crate::object::process::Process; +use crate::object::{DirEntry, ShellObject, Value}; +use derive_new::new; +use std::path::{Path, PathBuf}; +use sysinfo::SystemExt; + +#[derive(new)] +pub struct Cd; + +impl crate::Command for Cd { + fn run( + &mut self, + args: Vec, + _host: &dyn crate::Host, + env: &mut crate::Environment, + ) -> Result { + env.cwd = dunce::canonicalize(env.cwd().join(&args[0]).as_path())?; + Ok(Value::nothing()) + } +} diff --git a/src/commands/command.rs b/src/commands/command.rs index 269ab367c6..55cf4c2431 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -4,6 +4,7 @@ use crate::object::Value; pub trait Command { fn run( &mut self, + args: Vec, host: &dyn crate::Host, env: &mut crate::Environment, ) -> Result; diff --git a/src/commands/ls.rs b/src/commands/ls.rs index 0d95261d42..0d9f2e4112 100644 --- a/src/commands/ls.rs +++ b/src/commands/ls.rs @@ -10,6 +10,7 @@ pub struct Ls; impl crate::Command for Ls { fn run( &mut self, + _args: Vec, _host: &dyn crate::Host, env: &mut crate::Environment, ) -> Result { diff --git a/src/commands/ps.rs b/src/commands/ps.rs index 8e2af82740..cb1f5adfa0 100644 --- a/src/commands/ps.rs +++ b/src/commands/ps.rs @@ -12,6 +12,7 @@ pub struct Ps { impl crate::Command for Ps { fn run( &mut self, + _args: Vec, _host: &dyn crate::Host, _env: &mut crate::Environment, ) -> Result { diff --git a/src/env/environment.rs b/src/env/environment.rs index 2e3598edab..065574eb68 100644 --- a/src/env/environment.rs +++ b/src/env/environment.rs @@ -1,7 +1,8 @@ use std::path::{Path, PathBuf}; +#[derive(Debug, Clone)] pub struct Environment { - cwd: PathBuf, + crate cwd: PathBuf, } impl Environment { diff --git a/src/main.rs b/src/main.rs index a5986355be..2d940c475c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,18 +11,18 @@ mod parser; crate use crate::commands::command::Command; crate use crate::env::{Environment, Host}; -crate use crate::format::RenderView; crate use crate::errors::ShellError; +crate use crate::format::RenderView; use crate::object::base::{ToEntriesView, ToGenericView}; +use ansi_term::Color; +use conch_parser::lexer::Lexer; +use conch_parser::parse::DefaultParser; use rustyline::error::ReadlineError; use rustyline::Editor; use std::collections::BTreeMap; use std::error::Error; +use subprocess::Exec; use sysinfo::{self, SystemExt}; -use ansi_term::Color; -use conch_parser::lexer::Lexer; -use conch_parser::parse::DefaultParser; - #[derive(Debug)] pub enum MaybeOwned<'a, T> { @@ -53,16 +53,22 @@ fn main() -> Result<(), Box> { let mut system = sysinfo::System::new(); let mut ps = crate::commands::ps::Ps::new(system); let mut ls = crate::commands::ls::Ls; + let mut cd = crate::commands::cd::Cd; commands.insert("ps".to_string(), Box::new(ps)); commands.insert("ls".to_string(), Box::new(ls)); + commands.insert("cd".to_string(), Box::new(cd)); loop { - let readline = rl.readline(&format!("{}> ", Color::Green.paint(env.cwd().display().to_string()))); + let readline = rl.readline(&format!( + "{}> ", + Color::Green.paint(env.cwd().display().to_string()) + )); match readline { Ok(line) => { - let result = crate::parser::shell_parser(&line).map_err(|e| ShellError::new(format!("{:?}", e)))?; + let result = crate::parser::shell_parser(&line) + .map_err(|e| ShellError::new(format!("{:?}", e)))?; let parsed = result.1; @@ -72,13 +78,15 @@ fn main() -> Result<(), Box> { println!("Piping is not yet implemented"); } - println!("DEBUG: {:?}", parsed); - let command = &parsed[0][0].name(); + let args = parsed[0][1..] + .iter() + .map(|i| i.name().to_string()) + .collect(); match commands.get_mut(*command) { Some(command) => { - let result = command.run(&mut host, &mut env).unwrap(); + let result = command.run(args, &mut host, &mut env).unwrap(); let view = result.to_generic_view(); let rendered = view.render_view(&mut host); @@ -90,7 +98,9 @@ fn main() -> Result<(), Box> { } } - _ => println!("Saw: {}", line), + other => { + Exec::shell(line).cwd(env.cwd()).join().unwrap(); + } } } Err(ReadlineError::Interrupted) => {