Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: bump path-slash from 0.1.4 to 0.2.0 #4155

Merged
merged 5 commits into from Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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"
Expand Down
51 changes: 36 additions & 15 deletions src/modules/directory.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
""
)))
));
Expand Down Expand Up @@ -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")
)))
));

Expand Down Expand Up @@ -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(), "")
)))
));

Expand Down Expand Up @@ -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(),
""
)
)))
));

Expand Down Expand Up @@ -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(), "")
)))
));

Expand Down Expand Up @@ -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(),
""
)
)))
));

Expand Down