mirror of
https://github.com/nushell/nushell.git
synced 2025-08-16 20:41:45 +02:00
objectshell initial commit
This commit is contained in:
10
src/commands/command.rs
Normal file
10
src/commands/command.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use crate::errors::ShellError;
|
||||
use crate::object::Value;
|
||||
|
||||
pub trait Command {
|
||||
fn run(
|
||||
&mut self,
|
||||
host: &dyn crate::Host,
|
||||
env: &mut crate::Environment,
|
||||
) -> Result<Value, ShellError>;
|
||||
}
|
28
src/commands/ls.rs
Normal file
28
src/commands/ls.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use crate::errors::ShellError;
|
||||
use crate::object::process::Process;
|
||||
use crate::object::{DirEntry, ShellObject, Value};
|
||||
use derive_new::new;
|
||||
use sysinfo::SystemExt;
|
||||
|
||||
#[derive(new)]
|
||||
pub struct Ls;
|
||||
|
||||
impl crate::Command for Ls {
|
||||
fn run(
|
||||
&mut self,
|
||||
_host: &dyn crate::Host,
|
||||
env: &mut crate::Environment,
|
||||
) -> Result<Value, ShellError> {
|
||||
let entries =
|
||||
std::fs::read_dir(env.cwd()).map_err((|e| ShellError::new(format!("{:?}", e))))?;
|
||||
|
||||
let mut shell_entries = vec![];
|
||||
|
||||
for entry in entries {
|
||||
let value = Value::object(DirEntry::new(entry?)?);
|
||||
shell_entries.push(value)
|
||||
}
|
||||
|
||||
Ok(Value::list(shell_entries))
|
||||
}
|
||||
}
|
30
src/commands/ps.rs
Normal file
30
src/commands/ps.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use crate::errors::ShellError;
|
||||
use crate::object::process::Process;
|
||||
use crate::object::{ShellObject, Value};
|
||||
use derive_new::new;
|
||||
use sysinfo::SystemExt;
|
||||
|
||||
#[derive(new)]
|
||||
pub struct Ps {
|
||||
system: sysinfo::System,
|
||||
}
|
||||
|
||||
impl crate::Command for Ps {
|
||||
fn run(
|
||||
&mut self,
|
||||
_host: &dyn crate::Host,
|
||||
_env: &mut crate::Environment,
|
||||
) -> Result<Value, ShellError> {
|
||||
self.system.refresh_all();
|
||||
|
||||
let list = self.system.get_process_list();
|
||||
|
||||
let list = list
|
||||
.into_iter()
|
||||
.map(|(_, process)| Value::Object(Box::new(Process::new(process.clone()))))
|
||||
.take(5)
|
||||
.collect();
|
||||
|
||||
Ok(Value::List(list))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user