From aa3fe0b0dbc776e91f06774563e430fd4c7c6e17 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sat, 11 May 2019 01:08:21 -0700 Subject: [PATCH] Prep for streaming objects --- history.txt | 17 +++++++++++++++++ src/commands/args.rs | 6 ++++++ src/commands/cd.rs | 35 +++++++++++++++++++++++++++-------- src/commands/command.rs | 28 ++++++++++++++++++++++++---- src/commands/ls.rs | 36 +++++++++++++++++++++++++++--------- src/commands/ps.rs | 38 ++++++++++++++++++++++++++++---------- src/main.rs | 26 ++++++++++++++++++-------- 7 files changed, 147 insertions(+), 39 deletions(-) create mode 100644 src/commands/args.rs diff --git a/history.txt b/history.txt index b2da39499..ad50e5da6 100644 --- a/history.txt +++ b/history.txt @@ -64,3 +64,20 @@ ls cargo cargo build cargo run +git status +git add . +git commit +git push +git status +git add . +git commit +git push +git config --global core.autocrlf input +git config +ls +cd target +ls +cd target +cd .. +cd target +ls diff --git a/src/commands/args.rs b/src/commands/args.rs new file mode 100644 index 000000000..51bf37e7e --- /dev/null +++ b/src/commands/args.rs @@ -0,0 +1,6 @@ +use crate::Value; + +#[derive(Debug, Clone)] +pub struct Args { + args: Vec, +} diff --git a/src/commands/cd.rs b/src/commands/cd.rs index 43f268372..be05608aa 100644 --- a/src/commands/cd.rs +++ b/src/commands/cd.rs @@ -6,16 +6,35 @@ use std::path::{Path, PathBuf}; use sysinfo::SystemExt; #[derive(new)] -pub struct Cd; +pub struct CdBlueprint; -impl crate::Command for Cd { - fn run( - &mut self, +impl crate::CommandBlueprint for CdBlueprint { + fn create( + &self, args: Vec, - _host: &dyn crate::Host, + host: &dyn crate::Host, env: &mut crate::Environment, - ) -> Result { - env.cwd = dunce::canonicalize(env.cwd().join(&args[0]).as_path())?; - Ok(Value::nothing()) + ) -> Box { + Box::new(Cd { + cwd: env.cwd().to_path_buf(), + target: args[0].clone(), + }) + } +} + +#[derive(new)] +pub struct Cd { + cwd: PathBuf, + target: String, +} + +impl crate::Command for Cd { + fn run(&mut self) -> Result { + Ok(crate::CommandSuccess { + value: Value::nothing(), + action: vec![crate::CommandAction::ChangeCwd(dunce::canonicalize( + self.cwd.join(&self.target).as_path(), + )?)], + }) } } diff --git a/src/commands/command.rs b/src/commands/command.rs index 55cf4c243..ebbddc4fa 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -1,11 +1,31 @@ use crate::errors::ShellError; use crate::object::Value; +use std::path::PathBuf; -pub trait Command { - fn run( - &mut self, +pub trait CommandBlueprint { + fn create( + &self, args: Vec, host: &dyn crate::Host, env: &mut crate::Environment, - ) -> Result; + ) -> Box; +} + +crate enum CommandAction { + ChangeCwd(PathBuf), +} + +pub struct CommandSuccess { + crate value: Value, + crate action: Vec, +} + +pub trait Command { + fn begin(&mut self) -> Result<(), ShellError> { + Ok(()) + } + fn run(&mut self) -> Result; + fn end(&mut self) -> Result<(), ShellError> { + Ok(()) + } } diff --git a/src/commands/ls.rs b/src/commands/ls.rs index 0d9f2e411..5a6461f1c 100644 --- a/src/commands/ls.rs +++ b/src/commands/ls.rs @@ -1,21 +1,36 @@ use crate::errors::ShellError; use crate::object::process::Process; use crate::object::{DirEntry, ShellObject, Value}; +use crate::{Command, CommandSuccess}; use derive_new::new; +use std::path::PathBuf; use sysinfo::SystemExt; #[derive(new)] -pub struct Ls; +pub struct LsBlueprint; + +impl crate::CommandBlueprint for LsBlueprint { + fn create( + &self, + args: Vec, + host: &dyn crate::Host, + env: &mut crate::Environment, + ) -> Box { + Box::new(Ls { + cwd: env.cwd().to_path_buf(), + }) + } +} + +#[derive(new)] +pub struct Ls { + cwd: PathBuf, +} impl crate::Command for Ls { - fn run( - &mut self, - _args: Vec, - _host: &dyn crate::Host, - env: &mut crate::Environment, - ) -> Result { + fn run(&mut self) -> Result { let entries = - std::fs::read_dir(env.cwd()).map_err((|e| ShellError::new(format!("{:?}", e))))?; + std::fs::read_dir(&self.cwd).map_err((|e| ShellError::new(format!("{:?}", e))))?; let mut shell_entries = vec![]; @@ -24,6 +39,9 @@ impl crate::Command for Ls { shell_entries.push(value) } - Ok(Value::list(shell_entries)) + Ok(CommandSuccess { + value: Value::list(shell_entries), + action: vec![], + }) } } diff --git a/src/commands/ps.rs b/src/commands/ps.rs index cb1f5adfa..2d8dfc919 100644 --- a/src/commands/ps.rs +++ b/src/commands/ps.rs @@ -1,24 +1,39 @@ use crate::errors::ShellError; use crate::object::process::Process; use crate::object::{ShellObject, Value}; +use crate::Command; use derive_new::new; +use std::cell::RefCell; +use std::rc::Rc; use sysinfo::SystemExt; +#[derive(new)] +pub struct PsBlueprint { + system: Rc>, +} + +impl crate::CommandBlueprint for PsBlueprint { + fn create( + &self, + args: Vec, + host: &dyn crate::Host, + env: &mut crate::Environment, + ) -> Box { + Box::new(Ps::new(self.system.clone())) + } +} + #[derive(new)] pub struct Ps { - system: sysinfo::System, + system: Rc>, } impl crate::Command for Ps { - fn run( - &mut self, - _args: Vec, - _host: &dyn crate::Host, - _env: &mut crate::Environment, - ) -> Result { - self.system.refresh_all(); + fn run(&mut self) -> Result { + let mut system = self.system.borrow_mut(); + system.refresh_all(); - let list = self.system.get_process_list(); + let list = system.get_process_list(); let list = list .into_iter() @@ -26,6 +41,9 @@ impl crate::Command for Ps { .take(5) .collect(); - Ok(Value::List(list)) + Ok(crate::CommandSuccess { + value: Value::List(list), + action: vec![], + }) } } diff --git a/src/main.rs b/src/main.rs index 2d940c475..6e76bef24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ mod format; mod object; mod parser; -crate use crate::commands::command::Command; +crate use crate::commands::command::{Command, CommandAction, CommandBlueprint, CommandSuccess}; crate use crate::env::{Environment, Host}; crate use crate::errors::ShellError; crate use crate::format::RenderView; @@ -19,8 +19,10 @@ use conch_parser::lexer::Lexer; use conch_parser::parse::DefaultParser; use rustyline::error::ReadlineError; use rustyline::Editor; +use std::cell::RefCell; use std::collections::BTreeMap; use std::error::Error; +use std::rc::Rc; use subprocess::Exec; use sysinfo::{self, SystemExt}; @@ -48,12 +50,12 @@ fn main() -> Result<(), Box> { let mut host = crate::env::host::BasicHost; let mut env = crate::Environment::basic()?; - let mut commands = BTreeMap::>::new(); + let mut commands = BTreeMap::>::new(); - 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; + let mut system = Rc::new(RefCell::new(sysinfo::System::new())); + let mut ps = crate::commands::ps::PsBlueprint::new(system); + let mut ls = crate::commands::ls::LsBlueprint; + let mut cd = crate::commands::cd::CdBlueprint; commands.insert("ps".to_string(), Box::new(ps)); commands.insert("ls".to_string(), Box::new(ls)); @@ -86,8 +88,16 @@ fn main() -> Result<(), Box> { match commands.get_mut(*command) { Some(command) => { - let result = command.run(args, &mut host, &mut env).unwrap(); - let view = result.to_generic_view(); + let mut instance = command.create(args, &mut host, &mut env); + let result = instance.run()?; + + for action in result.action { + match action { + crate::CommandAction::ChangeCwd(cwd) => env.cwd = cwd, + } + } + + let view = result.value.to_generic_view(); let rendered = view.render_view(&mut host); for line in rendered {