diff options
author | Keith Wade <keawade@users.noreply.github.com> | 2019-10-14 10:12:43 -0500 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-10-15 00:12:43 +0900 |
commit | 10efe3e320097c81b96be39dc18aaf68c0d2cb41 (patch) | |
tree | fb0f48a649348f2148da2dbbb63191155fd13cfa /src/modules/directory.rs | |
parent | b3275d8ddf4ab00a120a1f6edeb954987b549cc9 (diff) |
fix: Show leading slash when truncating from root (#526)
Diffstat (limited to 'src/modules/directory.rs')
-rw-r--r-- | src/modules/directory.rs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/modules/directory.rs b/src/modules/directory.rs index fd9e74fd1..46968b6d1 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -142,7 +142,13 @@ fn truncate(dir_string: String, length: usize) -> 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 { return dir_string; } @@ -274,6 +280,20 @@ mod tests { } #[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] fn fish_style_with_user_home_contracted_path() { let path = "~/starship/engines/booster/rocket"; let output = to_fish_style(1, path.to_string(), "engines/booster/rocket"); |