diff --git a/Cargo.lock b/Cargo.lock index 3bab152e..89b33102 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1356,9 +1356,9 @@ dependencies = [ [[package]] name = "path-slash" -version = "0.1.4" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cacbb3c4ff353b534a67fb8d7524d00229da4cb1dc8c79f4db96e375ab5b619" +checksum = "c54014ba3c1880122928735226f78b6f5bf5bd1fed15e41e92cf7aa20278ce28" [[package]] name = "pathdiff" diff --git a/Cargo.toml b/Cargo.toml index 57b7008d..5672c8c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ notify-rust = { version = "4.5.8", optional = true } once_cell = "1.13.0" open = "3.0.1" os_info = "3.4.0" -path-slash = "0.1.4" +path-slash = "0.2.0" pest = "2.1.3" pest_derive = "2.1.0" quick-xml = "0.23.0" diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 873dfde9..109a9176 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -5,6 +5,7 @@ use super::utils::directory_win as directory_utils; use super::utils::path::PathExt as SPathExt; use indexmap::IndexMap; use path_slash::{PathBufExt, PathExt}; +use std::borrow::Cow; use std::iter::FromIterator; use std::path::{Path, PathBuf}; use unicode_segmentation::UnicodeSegmentation; @@ -63,8 +64,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { let mut is_truncated = dir_string.is_some(); // the home directory if required. - let dir_string = - dir_string.unwrap_or_else(|| contract_path(display_dir, &home_dir, &home_symbol)); + let dir_string = dir_string + .unwrap_or_else(|| contract_path(display_dir, &home_dir, &home_symbol).to_string()); #[cfg(windows)] let dir_string = remove_extended_path_prefix(dir_string); @@ -89,7 +90,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { let contracted_home_dir = contract_path(display_dir, &home_dir, &home_symbol); to_fish_style( config.fish_style_pwd_dir_length as usize, - contracted_home_dir, + contracted_home_dir.to_string(), &dir_string, ) } else { @@ -204,13 +205,17 @@ fn is_readonly_dir(path: &Path) -> bool { /// /// Replaces the `top_level_path` in a given `full_path` with the provided /// `top_level_replacement`. -fn contract_path(full_path: &Path, top_level_path: &Path, top_level_replacement: &str) -> String { +fn contract_path<'a>( + full_path: &'a Path, + top_level_path: &'a Path, + top_level_replacement: &'a str, +) -> Cow<'a, str> { if !full_path.normalised_starts_with(top_level_path) { return full_path.to_slash_lossy(); } if full_path.normalised_equals(top_level_path) { - return top_level_replacement.to_string(); + return Cow::from(top_level_replacement); } // Because we've done a normalised path comparison above @@ -221,12 +226,12 @@ fn contract_path(full_path: &Path, top_level_path: &Path, top_level_replacement: .strip_prefix(top_level_path.without_prefix()) .unwrap_or(full_path); - format!( + Cow::from(format!( "{replacement}{separator}{path}", replacement = top_level_replacement, separator = "/", path = sub_path.to_slash_lossy() - ) + )) } /// Contract the root component of a path based on the real path @@ -424,7 +429,7 @@ mod tests { let top_level_path = Path::new("C:\\Users\\astronaut"); let output = contract_path(full_path, top_level_path, "~"); - assert_eq!(output, "C:"); + assert_eq!(output, "C:/"); } #[test] @@ -789,7 +794,7 @@ mod tests { }) .path(&dir) .collect(); - let dir_str = dir.to_slash_lossy(); + let dir_str = dir.to_slash_lossy().to_string(); let expected = Some(format!( "{} ", Color::Cyan.bold().paint(convert_path_sep( @@ -819,7 +824,7 @@ mod tests { "{} ", Color::Cyan.bold().paint(convert_path_sep(&to_fish_style( 100, - dir.to_slash_lossy(), + dir.to_slash_lossy().to_string(), "" ))) )); @@ -870,7 +875,7 @@ mod tests { "{} ", Color::Cyan.bold().paint(convert_path_sep(&format!( "{}/thrusters/rocket", - to_fish_style(1, dir.to_slash_lossy(), "/thrusters/rocket") + to_fish_style(1, dir.to_slash_lossy().to_string(), "/thrusters/rocket") ))) )); @@ -992,7 +997,7 @@ mod tests { "{} ", Color::Cyan.bold().paint(convert_path_sep(&format!( "{}/above-repo/rocket-controls/src/meters/fuel-gauge", - to_fish_style(1, tmp_dir.path().to_slash_lossy(), "") + to_fish_style(1, tmp_dir.path().to_slash_lossy().to_string(), "") ))) )); @@ -1023,7 +1028,15 @@ mod tests { "{} ", Color::Cyan.bold().paint(convert_path_sep(&format!( "{}/rocket-controls/src/meters/fuel-gauge", - to_fish_style(1, tmp_dir.path().join("above-repo").to_slash_lossy(), "") + to_fish_style( + 1, + tmp_dir + .path() + .join("above-repo") + .to_slash_lossy() + .to_string(), + "" + ) ))) )); @@ -1198,7 +1211,7 @@ mod tests { "{} ", Color::Cyan.bold().paint(convert_path_sep(&format!( "{}/above-repo/rocket-controls-symlink/src/meters/fuel-gauge", - to_fish_style(1, tmp_dir.path().to_slash_lossy(), "") + to_fish_style(1, tmp_dir.path().to_slash_lossy().to_string(), "") ))) )); @@ -1235,7 +1248,15 @@ mod tests { "{} ", Color::Cyan.bold().paint(convert_path_sep(&format!( "{}/rocket-controls-symlink/src/meters/fuel-gauge", - to_fish_style(1, tmp_dir.path().join("above-repo").to_slash_lossy(), "") + to_fish_style( + 1, + tmp_dir + .path() + .join("above-repo") + .to_slash_lossy() + .to_string(), + "" + ) ))) ));