Commit 96d5b5d2 authored by Sag0Sag0's avatar Sag0Sag0 Committed by Michael Aaron Murphy

Fix issue #699 (#701)

* Fix issue #699 cd pushes to directory stack

* Fix various issues

* Add mmstick's stuff
parent 48a00d4d
......@@ -166,26 +166,24 @@ pub fn builtin_cd(args: &[&str], shell: &mut Shell) -> i32 {
match shell.directory_stack.cd(args, &shell.variables) {
Ok(()) => {
env::current_dir().ok().map_or_else(
|| env::set_var("PWD", "?"),
|path| {
match env::current_dir() {
Ok(cwd) => {
let pwd = shell.get_var_or_empty("PWD");
let pwd: &str = &pwd;
let current_dir = path.to_str().unwrap_or("?");
let pwd = &pwd;
let current_dir = cwd.to_str().unwrap_or("?");
if pwd != current_dir {
env::set_var("OLDPWD", pwd);
env::set_var("PWD", current_dir);
shell.set_var("OLDPWD", pwd);
shell.set_var("PWD", current_dir);
}
fork_function(shell, "CD_CHANGE", &["ion"]);
},
);
Err(_) => env::set_var("PWD", "?"),
};
SUCCESS
}
Err(why) => {
let stderr = io::stderr();
let mut stderr = stderr.lock();
let _ = stderr.write_all(why.as_bytes());
eprintln!("{}", why);
FAILURE
}
}
......
......@@ -101,8 +101,7 @@ impl DirectoryStack {
)));
}
// Manually update $PWD
variables.set_var("PWD", current_dir().unwrap().to_str().unwrap());
self.update_env_variables(variables);
self.print_dirs();
Ok(())
}
......@@ -167,8 +166,7 @@ impl DirectoryStack {
}
};
// Manually update $PWD
variables.set_var("PWD", current_dir().unwrap().to_str().unwrap());
self.update_env_variables(variables);
self.print_dirs();
Ok(())
}
......@@ -187,13 +185,31 @@ impl DirectoryStack {
if dir == "-" {
self.switch_to_previous_directory(variables)
} else {
self.change_and_push_dir(dir, variables)
let res = self.change_and_push_dir(dir, variables);
self.dirs.remove(1);
res
}
}
None => self.switch_to_home_directory(variables),
}
}
fn update_env_variables(&mut self, variables: &mut Variables) {
// Update $OLDPWD
let old_pwd = variables.get_var_or_empty("PWD");
if old_pwd.is_empty() {
variables.set_var("OLDPWD", "?");
} else {
variables.set_var("OLDPWD", &old_pwd);
}
// Update $PWD
match current_dir() {
Ok(current_dir) => variables.set_var("PWD", current_dir.to_str().unwrap_or("?")),
Err(_) => variables.set_var("PWD", "?"),
}
}
fn switch_to_home_directory(&mut self, variables: &Variables) -> Result<(), Cow<'static, str>> {
home_dir().map_or(
Err(Cow::Borrowed("ion: failed to get home directory")),
......@@ -212,22 +228,23 @@ impl DirectoryStack {
&mut self,
variables: &Variables,
) -> Result<(), Cow<'static, str>> {
self.get_previous_dir().cloned().map_or_else(
|| Err(Cow::Borrowed("ion: no previous directory to switch to")),
|prev| {
self.dirs.remove(1);
let prev = prev.to_string_lossy().to_string();
match self.get_previous_dir(variables) {
Some(prev) => {
self.dirs.remove(0);
let prev = prev.to_string();
println!("{}", prev);
self.change_and_push_dir(&prev, variables)
},
)
None => Err(Cow::Borrowed("ion: no previous directory to switch to")),
}
}
fn get_previous_dir(&self) -> Option<&PathBuf> {
if self.dirs.len() < 2 {
fn get_previous_dir(&self, variables: &Variables) -> Option<String> {
let previous_pwd = variables.get_var_or_empty("OLDPWD");
if previous_pwd == "?" || previous_pwd == "" {
None
} else {
self.dirs.get(1)
Some(previous_pwd)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment