fix: Fix panic when using fish-style pwd with unicode symbols (#672)

This commit is contained in:
Raidou 2019-11-29 13:02:22 +08:00 committed by Matan Kushner
parent 4ba1383373
commit 2a75a18eb1
1 changed files with 16 additions and 5 deletions

View File

@ -1,5 +1,6 @@
use path_slash::PathExt; use path_slash::PathExt;
use std::path::Path; use std::path::Path;
use unicode_segmentation::UnicodeSegmentation;
use super::{Context, Module}; use super::{Context, Module};
@ -182,11 +183,14 @@ fn to_fish_style(pwd_dir_length: usize, dir_string: String, truncated_dir_string
components components
.into_iter() .into_iter()
.map(|word| match word { .map(|word| -> String {
"" => "", let chars = UnicodeSegmentation::graphemes(word, true).collect::<Vec<&str>>();
_ if word.len() <= pwd_dir_length => word, match word {
_ if word.starts_with('.') => &word[..=pwd_dir_length], "" => "".to_string(),
_ => &word[..pwd_dir_length], _ if chars.len() <= pwd_dir_length => word.to_string(),
_ if word.starts_with('.') => chars[..=pwd_dir_length].join(""),
_ => chars[..pwd_dir_length].join(""),
}
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("/") .join("/")
@ -332,4 +336,11 @@ mod tests {
let output = to_fish_style(1, path.to_string(), "C++"); let output = to_fish_style(1, path.to_string(), "C++");
assert_eq!(output, "~/s/t/C/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̐/");
}
} }