nushell/src/commands/cd.rs

41 lines
979 B
Rust
Raw Normal View History

2019-05-11 09:00:33 +02:00
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{DirEntry, ShellObject, Value};
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
#[derive(new)]
2019-05-11 10:08:21 +02:00
pub struct CdBlueprint;
2019-05-11 09:00:33 +02:00
2019-05-11 10:08:21 +02:00
impl crate::CommandBlueprint for CdBlueprint {
fn create(
&self,
2019-05-11 09:00:33 +02:00
args: Vec<String>,
2019-05-11 10:08:21 +02:00
host: &dyn crate::Host,
2019-05-11 09:00:33 +02:00
env: &mut crate::Environment,
2019-05-11 10:08:21 +02:00
) -> 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(),
)?)],
})
2019-05-11 09:00:33 +02:00
}
}