objectshell initial commit

This commit is contained in:
Yehuda Katz
2019-05-10 09:59:12 -07:00
commit 8f3b273337
26 changed files with 1490 additions and 0 deletions

10
src/commands/command.rs Normal file
View 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
View 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
View 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))
}
}