Expand pwd command

Expand functionality of the pwd command to better handle the different
types of shells (e.g. FilesystemShell, ValueShell, etc.).
This commit is contained in:
Jonathan Rothberg 2019-09-07 15:31:16 -07:00
parent 90b358d60b
commit 7913ae76f8
7 changed files with 50 additions and 13 deletions

View File

@ -1,7 +1,7 @@
use crate::context::{SourceMap, SpanSource}; use crate::context::{SourceMap, SpanSource};
use crate::data::Value;
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::evaluate::Scope; use crate::evaluate::Scope;
use crate::data::Value;
use crate::parser::hir; use crate::parser::hir;
use crate::parser::{registry, ConfigDeserializer}; use crate::parser::{registry, ConfigDeserializer};
use crate::prelude::*; use crate::prelude::*;

View File

@ -1,9 +1,9 @@
use crate::commands::WholeStreamCommand; use crate::commands::WholeStreamCommand;
// use crate::data::{Dictionary, Value};
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::data::{Dictionary, Value};
use crate::parser::registry::Signature; use crate::parser::registry::Signature;
use crate::prelude::*; use crate::prelude::*;
use indexmap::IndexMap; // use indexmap::IndexMap;
pub struct PWD; pub struct PWD;
@ -30,15 +30,7 @@ impl WholeStreamCommand for PWD {
} }
pub fn pwd(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> { pub fn pwd(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let shell_manager = args.shell_manager.clone();
let args = args.evaluate_once(registry)?; let args = args.evaluate_once(registry)?;
let span = args.call_info.name_span; shell_manager.pwd(args)
let mut indexmap = IndexMap::new();
indexmap.insert(
"name".to_string(),
Tagged::from_simple_spanned_item(Value::string(std::env::current_dir()?.to_string_lossy()), span),
);
let value = Tagged::from_simple_spanned_item(Value::Row(Dictionary::from(indexmap)), span);
Ok(OutputStream::one(value))
} }

View File

@ -946,6 +946,28 @@ impl Shell for FilesystemShell {
self.path.clone() self.path.clone()
} }
fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
let path = PathBuf::from(self.path());
let p = match dunce::canonicalize(path.as_path()) {
Ok(p) => p,
Err(_) => {
return Err(ShellError::labeled_error(
"unable to show current directory",
"pwd command failed",
args.call_info.name_span,
));
}
};
let mut stream = VecDeque::new();
stream.push_back(ReturnSuccess::value(
Value::Primitive(Primitive::String(p.to_string_lossy().to_string()))
.simple_spanned(args.call_info.name_span),
));
Ok(stream.into())
}
fn set_path(&mut self, path: String) { fn set_path(&mut self, path: String) {
let pathbuf = PathBuf::from(&path); let pathbuf = PathBuf::from(&path);
let path = match dunce::canonicalize(pathbuf.as_path()) { let path = match dunce::canonicalize(pathbuf.as_path()) {

View File

@ -117,6 +117,10 @@ impl Shell for HelpShell {
self.path.clone() self.path.clone()
} }
fn pwd(&self, _: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::empty())
}
fn set_path(&mut self, path: String) { fn set_path(&mut self, path: String) {
let _ = std::env::set_current_dir(&path); let _ = std::env::set_current_dir(&path);
self.path = path.clone(); self.path = path.clone();

View File

@ -20,6 +20,7 @@ pub trait Shell: std::fmt::Debug {
fn mv(&self, args: MoveArgs, name: Span, path: &str) -> Result<OutputStream, ShellError>; fn mv(&self, args: MoveArgs, name: Span, path: &str) -> Result<OutputStream, ShellError>;
fn rm(&self, args: RemoveArgs, name: Span, path: &str) -> Result<OutputStream, ShellError>; fn rm(&self, args: RemoveArgs, name: Span, path: &str) -> Result<OutputStream, ShellError>;
fn path(&self) -> String; fn path(&self) -> String;
fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError>;
fn set_path(&mut self, path: String); fn set_path(&mut self, path: String);
fn complete( fn complete(

View File

@ -62,6 +62,12 @@ impl ShellManager {
self.shells.lock().unwrap()[self.current_shell].path() self.shells.lock().unwrap()[self.current_shell].path()
} }
pub fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
let env = self.shells.lock().unwrap();
env[self.current_shell].pwd(args)
}
pub fn set_path(&mut self, path: String) { pub fn set_path(&mut self, path: String) {
self.shells.lock().unwrap()[self.current_shell].set_path(path) self.shells.lock().unwrap()[self.current_shell].set_path(path)
} }

View File

@ -199,6 +199,18 @@ impl Shell for ValueShell {
self.path.clone() self.path.clone()
} }
fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
let path = PathBuf::from(&self.path());
let mut stream = VecDeque::new();
stream.push_back(ReturnSuccess::value(
Value::Primitive(Primitive::String(path.to_string_lossy().to_string()))
.simple_spanned(args.call_info.name_span),
));
Ok(stream.into())
}
fn set_path(&mut self, path: String) { fn set_path(&mut self, path: String) {
let _ = std::env::set_current_dir(&path); let _ = std::env::set_current_dir(&path);
self.path = path.clone(); self.path = path.clone();