mirror of
https://github.com/nushell/nushell.git
synced 2025-08-17 17:31:06 +02:00
Basic pipelining is working!
This commit is contained in:
@ -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),
|
||||
|
@ -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
51
src/commands/take.rs
Normal 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)
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user