mirror of
https://github.com/nushell/nushell.git
synced 2025-08-16 03:57:52 +02:00
Prep for streaming objects
This commit is contained in:
6
src/commands/args.rs
Normal file
6
src/commands/args.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use crate::Value;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Args {
|
||||
args: Vec<Value>,
|
||||
}
|
@ -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<String>,
|
||||
_host: &dyn crate::Host,
|
||||
host: &dyn crate::Host,
|
||||
env: &mut crate::Environment,
|
||||
) -> Result<Value, ShellError> {
|
||||
env.cwd = dunce::canonicalize(env.cwd().join(&args[0]).as_path())?;
|
||||
Ok(Value::nothing())
|
||||
) -> Box<dyn crate::Command> {
|
||||
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<crate::CommandSuccess, ShellError> {
|
||||
Ok(crate::CommandSuccess {
|
||||
value: Value::nothing(),
|
||||
action: vec![crate::CommandAction::ChangeCwd(dunce::canonicalize(
|
||||
self.cwd.join(&self.target).as_path(),
|
||||
)?)],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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<String>,
|
||||
host: &dyn crate::Host,
|
||||
env: &mut crate::Environment,
|
||||
) -> Result<Value, ShellError>;
|
||||
) -> Box<dyn Command>;
|
||||
}
|
||||
|
||||
crate enum CommandAction {
|
||||
ChangeCwd(PathBuf),
|
||||
}
|
||||
|
||||
pub struct CommandSuccess {
|
||||
crate value: Value,
|
||||
crate action: Vec<CommandAction>,
|
||||
}
|
||||
|
||||
pub trait Command {
|
||||
fn begin(&mut self) -> Result<(), ShellError> {
|
||||
Ok(())
|
||||
}
|
||||
fn run(&mut self) -> Result<CommandSuccess, ShellError>;
|
||||
fn end(&mut self) -> Result<(), ShellError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -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<String>,
|
||||
host: &dyn crate::Host,
|
||||
env: &mut crate::Environment,
|
||||
) -> Box<dyn Command> {
|
||||
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<String>,
|
||||
_host: &dyn crate::Host,
|
||||
env: &mut crate::Environment,
|
||||
) -> Result<Value, ShellError> {
|
||||
fn run(&mut self) -> Result<CommandSuccess, ShellError> {
|
||||
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![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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<RefCell<sysinfo::System>>,
|
||||
}
|
||||
|
||||
impl crate::CommandBlueprint for PsBlueprint {
|
||||
fn create(
|
||||
&self,
|
||||
args: Vec<String>,
|
||||
host: &dyn crate::Host,
|
||||
env: &mut crate::Environment,
|
||||
) -> Box<dyn Command> {
|
||||
Box::new(Ps::new(self.system.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(new)]
|
||||
pub struct Ps {
|
||||
system: sysinfo::System,
|
||||
system: Rc<RefCell<sysinfo::System>>,
|
||||
}
|
||||
|
||||
impl crate::Command for Ps {
|
||||
fn run(
|
||||
&mut self,
|
||||
_args: Vec<String>,
|
||||
_host: &dyn crate::Host,
|
||||
_env: &mut crate::Environment,
|
||||
) -> Result<Value, ShellError> {
|
||||
self.system.refresh_all();
|
||||
fn run(&mut self) -> Result<crate::CommandSuccess, ShellError> {
|
||||
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![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user