From 3c835ba34bb98dff237d04abefc228600df730d0 Mon Sep 17 00:00:00 2001 From: AppleTheGolden Date: Mon, 9 Dec 2019 18:59:02 +0100 Subject: fix: Truncate long paths in conda environment names (#694) Environment names created via conda create -p [path] tend to be too long for comfort, so this truncates them. --- src/modules/directory.rs | 67 +----------------------------------------------- 1 file changed, 1 insertion(+), 66 deletions(-) (limited to 'src/modules/directory.rs') diff --git a/src/modules/directory.rs b/src/modules/directory.rs index a221515e2..61484c17d 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -4,6 +4,7 @@ use unicode_segmentation::UnicodeSegmentation; use super::{Context, Module}; +use super::utils::directory::truncate; use crate::config::{RootModuleConfig, SegmentConfig}; use crate::configs::directory::DirectoryConfig; @@ -137,30 +138,6 @@ const fn replace_c_dir(path: String) -> String { path } -/// Truncate a path to only have a set number of path components -/// -/// Will truncate a path to only show the last `length` components in a path. -/// If a length of `0` is provided, the path will not be truncated. -fn truncate(dir_string: String, length: usize) -> String { - if length == 0 { - return dir_string; - } - - 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; - } - - let truncated_components = &components[components.len() - length..]; - truncated_components.join("/") -} - /// Takes part before contracted path and replaces it with fish style path /// /// Will take the first letter of each directory before the contracted path and @@ -258,48 +235,6 @@ mod tests { assert_eq!(output, "/c"); } - #[test] - fn truncate_smaller_path_than_provided_length() { - let path = "~/starship"; - let output = truncate(path.to_string(), 3); - assert_eq!(output, "~/starship") - } - - #[test] - fn truncate_same_path_as_provided_length() { - let path = "~/starship/engines"; - let output = truncate(path.to_string(), 3); - assert_eq!(output, "~/starship/engines") - } - - #[test] - fn truncate_slightly_larger_path_than_provided_length() { - 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() { - let path = "~/starship/engines/booster/rocket"; - let output = truncate(path.to_string(), 3); - 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"; -- cgit v1.2.3