mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 14:33:23 +02:00
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:
@ -946,6 +946,28 @@ impl Shell for FilesystemShell {
|
||||
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) {
|
||||
let pathbuf = PathBuf::from(&path);
|
||||
let path = match dunce::canonicalize(pathbuf.as_path()) {
|
||||
|
@ -117,6 +117,10 @@ impl Shell for HelpShell {
|
||||
self.path.clone()
|
||||
}
|
||||
|
||||
fn pwd(&self, _: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
|
||||
Ok(OutputStream::empty())
|
||||
}
|
||||
|
||||
fn set_path(&mut self, path: String) {
|
||||
let _ = std::env::set_current_dir(&path);
|
||||
self.path = path.clone();
|
||||
|
@ -20,6 +20,7 @@ pub trait Shell: std::fmt::Debug {
|
||||
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 path(&self) -> String;
|
||||
fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError>;
|
||||
fn set_path(&mut self, path: String);
|
||||
|
||||
fn complete(
|
||||
|
@ -62,6 +62,12 @@ impl ShellManager {
|
||||
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) {
|
||||
self.shells.lock().unwrap()[self.current_shell].set_path(path)
|
||||
}
|
||||
|
@ -199,6 +199,18 @@ impl Shell for ValueShell {
|
||||
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) {
|
||||
let _ = std::env::set_current_dir(&path);
|
||||
self.path = path.clone();
|
||||
|
Reference in New Issue
Block a user