From 7c941af11a6511a2f6118984e909c0d0cc7424c4 Mon Sep 17 00:00:00 2001 From: David Knaack Date: Tue, 6 Oct 2020 09:44:27 +0200 Subject: [PATCH] feat(directory): strip Microsoft.PowerShell.Core\FileSystem:: prefix on windows (#1732) --- src/modules/directory.rs | 59 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/modules/directory.rs b/src/modules/directory.rs index ac923e7e..4f13177c 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -13,6 +13,7 @@ use super::{Context, Module}; use super::utils::directory::truncate; use crate::config::RootModuleConfig; use crate::configs::directory::DirectoryConfig; +use crate::context::Shell; use crate::formatter::StringFormatter; const HOME_SYMBOL: &str = "~"; @@ -123,7 +124,17 @@ fn get_current_dir(context: &Context, config: &DirectoryConfig) -> PathBuf { // If this is None for any reason, we fall back to reading the os-provided path let physical_current_dir = if config.use_logical_path { match context.get_env("PWD") { - Some(x) => Some(PathBuf::from(x)), + Some(mut x) => { + // Prevent Powershell from prepending "Microsoft.PowerShell.Core\FileSystem::" to some paths + if cfg!(windows) && context.shell == Shell::PowerShell { + if let Some(no_prefix) = + x.strip_prefix(r"Microsoft.PowerShell.Core\FileSystem::") + { + x = no_prefix.to_string(); + } + } + Some(PathBuf::from(x)) + } None => { log::debug!("Error getting PWD environment variable!"); None @@ -426,6 +437,52 @@ mod tests { Ok((dir, path)) } + #[test] + fn windows_strip_prefix() { + let with_prefix = r"Microsoft.PowerShell.Core\FileSystem::/path"; + let without_prefix = r"/path"; + + let actual = ModuleRenderer::new("directory") + // use a different physical path here as a sentinel value + .path("/") + .env("PWD", with_prefix) + .shell(Shell::PowerShell) + .config(toml::toml! { + [directory] + format = "$path" + truncation_length = 100 + }) + .collect() + .unwrap(); + let expected = if cfg!(windows) { + without_prefix + } else { + with_prefix + }; + let expected = Path::new(expected).to_slash().unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn windows_strip_prefix_no_pwsh() { + let with_prefix = r"Microsoft.PowerShell.Core\FileSystem::/path"; + + let actual = ModuleRenderer::new("directory") + // use a different physical path here as a sentinel value + .path("/") + .env("PWD", with_prefix) + .shell(Shell::Bash) + .config(toml::toml! { + [directory] + format = "$path" + truncation_length = 100 + }) + .collect() + .unwrap(); + let expected = Path::new(with_prefix).to_slash().unwrap(); + assert_eq!(actual, expected); + } + #[cfg(not(target_os = "windows"))] mod linux { use super::*;