summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorfilip <filipbachul@gmail.com>2022-07-12 11:31:43 +0200
committerGitHub <noreply@github.com>2022-07-12 11:31:43 +0200
commitf56955084b5bce5ca71704499a7aadc399bdba12 (patch)
tree02b2243c2319e76684b79f54a2d411c8f2307812 /src
parent784d671d5ea2b8d7da6d1a8f04e61023cc638981 (diff)
build: bump path-slash from 0.1.4 to 0.2.0 (#4155)
* build: bump path-slash from 0.1.4 to 0.2.0 Bumps [path-slash](https://github.com/rhysd/path-slash) from 0.1.4 to 0.2.0. - [Release notes](https://github.com/rhysd/path-slash/releases) - [Changelog](https://github.com/rhysd/path-slash/blob/master/CHANGELOG.md) - [Commits](https://github.com/rhysd/path-slash/compare/v0.1.4...v0.2.0) --- updated-dependencies: - dependency-name: path-slash dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * fix errors in code after path-slash bump to 0.2.0 * fmt * refactor contract_path to return Cow<str> * update contract_windows_style_root_directory test Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Diffstat (limited to 'src')
-rw-r--r--src/modules/directory.rs51
1 files changed, 36 insertions, 15 deletions
diff --git a/src/modules/directory.rs b/src/modules/directory.rs
index 873dfde95..109a9176c 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<Module<'a>> {
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<Module<'a>> {
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(),
+ ""
+ )
)))
));