From 10efe3e320097c81b96be39dc18aaf68c0d2cb41 Mon Sep 17 00:00:00 2001 From: Keith Wade Date: Mon, 14 Oct 2019 10:12:43 -0500 Subject: [PATCH] fix: Show leading slash when truncating from root (#526) --- src/modules/directory.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/modules/directory.rs b/src/modules/directory.rs index fd9e74fd..46968b6d 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::>(); + let mut components = dir_string.split('/').collect::>(); + + // 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; } @@ -273,6 +279,20 @@ mod tests { 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] fn fish_style_with_user_home_contracted_path() { let path = "~/starship/engines/booster/rocket";