From 3c8cf1efdc4aa625d48d469185de126127aaeb4d Mon Sep 17 00:00:00 2001 From: Michael Murphy <mmstickman@gmail.com> Date: Fri, 22 Jun 2018 20:36:23 -0600 Subject: [PATCH] Fix issue with the PWD variable --- src/lib/builtins/mod.rs | 4 ++-- src/lib/shell/directory_stack.rs | 5 +++-- src/lib/shell/variables/mod.rs | 19 +++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lib/builtins/mod.rs b/src/lib/builtins/mod.rs index 27cf0e3f..abe61c8d 100644 --- a/src/lib/builtins/mod.rs +++ b/src/lib/builtins/mod.rs @@ -158,13 +158,13 @@ pub fn builtin_cd(args: &[String], shell: &mut Shell) -> i32 { Ok(()) => { match env::current_dir() { Ok(cwd) => { - let pwd = shell.get_var_or_empty("PWD"); + let pwd = env::var("PWD").ok().unwrap_or_else(String::default); let pwd = &pwd; let current_dir = cwd.to_str().unwrap_or("?"); if pwd != current_dir { shell.set_var("OLDPWD", pwd); - shell.set_var("PWD", current_dir); + env::set_var("PWD", current_dir); } fork_function(shell, "CD_CHANGE", &["ion"]); } diff --git a/src/lib/shell/directory_stack.rs b/src/lib/shell/directory_stack.rs index e6da29f6..1c7f7cc4 100644 --- a/src/lib/shell/directory_stack.rs +++ b/src/lib/shell/directory_stack.rs @@ -3,6 +3,7 @@ use super::{ }; use std::{ borrow::Cow, collections::VecDeque, env::{current_dir, home_dir, set_current_dir}, + env, path::PathBuf, }; @@ -234,8 +235,8 @@ impl DirectoryStack { // Update $PWD match current_dir() { - Ok(current_dir) => variables.set_var("PWD", current_dir.to_str().unwrap_or("?")), - Err(_) => variables.set_var("PWD", "?"), + Ok(current_dir) => env::set_var("PWD", current_dir.to_str().unwrap_or("?")), + Err(_) => env::set_var("PWD", "?"), } } diff --git a/src/lib/shell/variables/mod.rs b/src/lib/shell/variables/mod.rs index 8a5cc047..63e2653f 100644 --- a/src/lib/shell/variables/mod.rs +++ b/src/lib/shell/variables/mod.rs @@ -266,11 +266,10 @@ impl Variables { "" => if let Some(home) = env::home_dir() { return Some(home.to_string_lossy().to_string() + remainder); }, - "+" => if let Some(pwd) = self.get_var("PWD") { - return Some(pwd.to_string() + remainder); - } else if let Ok(pwd) = env::current_dir() { - return Some(pwd.to_string_lossy().to_string() + remainder); - }, + "+" => return Some(match env::var("PWD") { + Ok(var) => var + remainder, + _ => ["?", remainder].concat() + }), "-" => if let Some(oldpwd) = self.get_var("OLDPWD") { return Some(oldpwd.to_string() + remainder); }, @@ -520,7 +519,7 @@ impl Variables { /// Useful for getting smaller prompts, this will produce a simplified variant of the /// working directory which the leading `HOME` prefix replaced with a tilde character. fn get_simplified_directory(&self) -> Value { - self.get_var("PWD") + env::var("PWD") .unwrap() .replace(&self.get_var("HOME").unwrap(), "~") } @@ -670,8 +669,8 @@ mod tests { #[test] fn minimal_directory_var_should_compact_path() { - let mut variables = Variables::default(); - variables.set_var("PWD", "/var/log/nix"); + let variables = Variables::default(); + env::set_var("PWD", "/var/log/nix"); assert_eq!( "v/l/nix", variables.get_var("MWD").expect("no value returned") @@ -680,8 +679,8 @@ mod tests { #[test] fn minimal_directory_var_shouldnt_compact_path() { - let mut variables = Variables::default(); - variables.set_var("PWD", "/var/log"); + let variables = Variables::default(); + env::set_var("PWD", "/var/log"); assert_eq!( "/var/log", variables.get_var("MWD").expect("no value returned") -- GitLab