From f21c15b7458d581961b21561304f3dff2d251697 Mon Sep 17 00:00:00 2001 From: Jeff Zhao Date: Sun, 4 Jul 2021 20:53:35 -0400 Subject: fix cd not working with relative paths --- src/commands/change_directory.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index 0499011..b665d0c 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -1,7 +1,8 @@ +use std::io; use std::path; use crate::context::AppContext; -use crate::error::JoshutoResult; +use crate::error::{JoshutoError, JoshutoResult}; use crate::history::DirectoryHistory; pub fn cd(path: &path::Path, context: &mut AppContext) -> std::io::Result<()> { @@ -23,6 +24,31 @@ fn _change_directory(path: &path::Path, context: &mut AppContext) -> std::io::Re } pub fn change_directory(context: &mut AppContext, path: &path::Path) -> JoshutoResult<()> { - _change_directory(path, context)?; + let new_cwd = if path.is_absolute() { + let new_cwd = path.canonicalize()?; + if !new_cwd.exists() { + let err = io::Error::new( + io::ErrorKind::NotFound, + "No such file or directory".to_string(), + ); + let err = JoshutoError::from(err); + return Err(err); + } + new_cwd + } else { + let mut new_cwd = std::env::current_dir()?; + new_cwd.push(path.canonicalize()?); + if !new_cwd.exists() { + let err = io::Error::new( + io::ErrorKind::NotFound, + "No such file or directory".to_string(), + ); + let err = JoshutoError::from(err); + return Err(err); + } + new_cwd + }; + + _change_directory(new_cwd.as_path(), context)?; Ok(()) } -- cgit v1.2.3