fix: Show leading slash when truncating from root (#526)

This commit is contained in:
Keith Wade 2019-10-14 10:12:43 -05:00 committed by Matan Kushner
parent b3275d8ddf
commit 10efe3e320
1 changed files with 21 additions and 1 deletions

View File

@ -142,7 +142,13 @@ fn truncate(dir_string: String, length: usize) -> String {
return dir_string; return dir_string;
} }
let components = dir_string.split('/').collect::<Vec<&str>>(); let mut components = dir_string.split('/').collect::<Vec<&str>>();
// If the first element is "" then there was a leading "/" and we should remove it so we can check the actual count of components
if (components[0] == "") {
components.remove(0);
}
if components.len() <= length { if components.len() <= length {
return dir_string; return dir_string;
} }
@ -273,6 +279,20 @@ mod tests {
assert_eq!(output, "engines/booster/rocket") assert_eq!(output, "engines/booster/rocket")
} }
#[test]
fn truncate_same_path_as_provided_length_from_root() {
let path = "/starship/engines/booster";
let output = truncate(path.to_string(), 3);
assert_eq!(output, "/starship/engines/booster");
}
#[test]
fn truncate_larger_path_than_provided_length_from_root() {
let path = "/starship/engines/booster/rocket";
let output = truncate(path.to_string(), 3);
assert_eq!(output, "engines/booster/rocket");
}
#[test] #[test]
fn fish_style_with_user_home_contracted_path() { fn fish_style_with_user_home_contracted_path() {
let path = "~/starship/engines/booster/rocket"; let path = "~/starship/engines/booster/rocket";