Basic pipelining is working!

This commit is contained in:
Yehuda Katz
2019-05-15 11:12:38 -05:00
parent 975ff7c2fb
commit 3040638881
18 changed files with 405 additions and 239 deletions

View File

@ -12,10 +12,12 @@ pub trait CommandBlueprint {
) -> Result<Box<dyn Command>, ShellError>;
}
#[derive(Debug)]
pub enum CommandAction {
ChangeCwd(PathBuf),
}
#[derive(Debug)]
pub enum ReturnValue {
Value(Value),
Action(CommandAction),

View File

@ -9,9 +9,7 @@ use std::rc::Rc;
use sysinfo::SystemExt;
#[derive(new)]
pub struct PsBlueprint {
system: Rc<RefCell<sysinfo::System>>,
}
pub struct PsBlueprint;
impl crate::CommandBlueprint for PsBlueprint {
fn create(
@ -20,18 +18,16 @@ impl crate::CommandBlueprint for PsBlueprint {
host: &dyn crate::Host,
env: &mut crate::Environment,
) -> Result<Box<dyn Command>, ShellError> {
Ok(Box::new(Ps::new(self.system.clone())))
Ok(Box::new(Ps::new()))
}
}
#[derive(new)]
pub struct Ps {
system: Rc<RefCell<sysinfo::System>>,
}
pub struct Ps;
impl crate::Command for Ps {
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
let mut system = self.system.borrow_mut();
let mut system = sysinfo::System::new();
system.refresh_all();
let list = system.get_process_list();
@ -41,7 +37,6 @@ impl crate::Command for Ps {
.map(|(_, process)| {
ReturnValue::Value(Value::Object(Box::new(Process::new(process.clone()))))
})
.take(5)
.collect::<VecDeque<_>>();
Ok(list)

51
src/commands/take.rs Normal file
View File

@ -0,0 +1,51 @@
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{DirEntry, ShellObject, Value};
use crate::prelude::*;
use crate::Args;
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
#[derive(new)]
pub struct TakeBlueprint;
impl crate::CommandBlueprint for TakeBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn Host,
env: &mut Environment,
) -> Result<Box<dyn Command>, ShellError> {
if args.is_empty() {
return Err(ShellError::string("take requires an integer"));
}
let amount = args[0].as_int()?;
Ok(Box::new(Take { amount }))
}
}
#[derive(new)]
pub struct Take {
amount: i64,
}
impl crate::Command for Take {
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
let amount = if stream.len() > self.amount as usize {
self.amount as usize
} else {
stream.len()
};
let out: VecDeque<ReturnValue> = stream
.into_iter()
.take(amount)
.map(|v| ReturnValue::Value(v))
.collect();
Ok(out)
}
}

View File

@ -30,3 +30,10 @@ impl crate::Command for ToArray {
Ok(ReturnValue::single(Value::List(out)))
}
}
crate fn to_array(stream: VecDeque<Value>) -> VecDeque<Value> {
let out = Value::List(stream.into_iter().collect());
let mut stream = VecDeque::new();
stream.push_back(out);
stream
}