From 2a75a18eb17aba57db6d6de0331607719573e42f Mon Sep 17 00:00:00 2001 From: Raidou Date: Fri, 29 Nov 2019 13:02:22 +0800 Subject: [PATCH] fix: Fix panic when using fish-style pwd with unicode symbols (#672) --- src/modules/directory.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/modules/directory.rs b/src/modules/directory.rs index a3f73af59..e6f3c5974 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -1,5 +1,6 @@ use path_slash::PathExt; use std::path::Path; +use unicode_segmentation::UnicodeSegmentation; use super::{Context, Module}; @@ -182,11 +183,14 @@ fn to_fish_style(pwd_dir_length: usize, dir_string: String, truncated_dir_string components .into_iter() - .map(|word| match word { - "" => "", - _ if word.len() <= pwd_dir_length => word, - _ if word.starts_with('.') => &word[..=pwd_dir_length], - _ => &word[..pwd_dir_length], + .map(|word| -> String { + let chars = UnicodeSegmentation::graphemes(word, true).collect::>(); + match word { + "" => "".to_string(), + _ if chars.len() <= pwd_dir_length => word.to_string(), + _ if word.starts_with('.') => chars[..=pwd_dir_length].join(""), + _ => chars[..pwd_dir_length].join(""), + } }) .collect::>() .join("/") @@ -332,4 +336,11 @@ mod tests { let output = to_fish_style(1, path.to_string(), "C++"); assert_eq!(output, "~/s/t/C/C/"); } + + #[test] + fn fish_style_with_unicode() { + let path = "~/starship/tmp/目录/a̐éö̲/目录"; + let output = to_fish_style(1, path.to_string(), "目录"); + assert_eq!(output, "~/s/t/目/a̐/"); + } }