From ca36d15acd398f5e3b033201555ef66517638879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Su=C3=A1rez?= Date: Sun, 24 Jan 2021 18:50:37 -0300 Subject: feat(directory): Add home directory symbol (#2198) * feat(directory): Add home directory symbol * Replace HOME_SYMBOL constant as a config variable --- docs/config/README.md | 1 + src/configs/directory.rs | 2 ++ src/modules/directory.rs | 52 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index a078e74c9..221fb7228 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -656,6 +656,7 @@ it would have been `nixpkgs/pkgs`. | `read_only` | `"🔒"` | The symbol indicating current directory is read only. | | `read_only_style` | `"red"` | The style for the read only symbol. | | `truncation_symbol` | `""` | The symbol to prefix to truncated paths. eg: "…/" | +| `home_symbol` | `"~"` | The symbol indicating home directory. |
This module has a few advanced configuration options that control how the directory is displayed. diff --git a/src/configs/directory.rs b/src/configs/directory.rs index 87e165992..12e905721 100644 --- a/src/configs/directory.rs +++ b/src/configs/directory.rs @@ -16,6 +16,7 @@ pub struct DirectoryConfig<'a> { pub read_only: &'a str, pub read_only_style: &'a str, pub truncation_symbol: &'a str, + pub home_symbol: &'a str, } impl<'a> RootModuleConfig<'a> for DirectoryConfig<'a> { @@ -32,6 +33,7 @@ impl<'a> RootModuleConfig<'a> for DirectoryConfig<'a> { read_only: "🔒", read_only_style: "red", truncation_symbol: "", + home_symbol: "~", } } } diff --git a/src/modules/directory.rs b/src/modules/directory.rs index ea35cc923..6b837f6fb 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -16,8 +16,6 @@ use crate::configs::directory::DirectoryConfig; use crate::context::Shell; use crate::formatter::StringFormatter; -const HOME_SYMBOL: &str = "~"; - /// Creates a module with the current directory /// /// Will perform path contraction, substitution, and truncation. @@ -25,7 +23,7 @@ const HOME_SYMBOL: &str = "~"; /// **Contraction** /// /// - Paths beginning with the home directory or with a git repo right inside -/// the home directory will be contracted to `~` +/// the home directory will be contracted to `~`, or the set HOME_SYMBOL /// - Paths containing a git repo will contract to begin at the repo root /// /// **Substitution** @@ -40,6 +38,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { let current_dir = &get_current_dir(&context, &config); let home_dir = context.get_home().unwrap(); + let home_symbol = String::from(config.home_symbol); + log::debug!("Current directory: {:?}", current_dir); let repo = &context.get_repo().ok()?; @@ -48,10 +48,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { log::debug!("Repo root: {:?}", repo_root); // Contract the path to the git repo root contract_repo_path(current_dir, repo_root) - .unwrap_or_else(|| contract_path(current_dir, &home_dir, HOME_SYMBOL)) + .unwrap_or_else(|| contract_path(current_dir, &home_dir, &home_symbol)) } // Contract the path to the home directory - _ => contract_path(current_dir, &home_dir, HOME_SYMBOL), + _ => contract_path(current_dir, &home_dir, &home_symbol), }; log::debug!("Dir string: {}", dir_string); @@ -60,12 +60,12 @@ pub fn module<'a>(context: &'a Context) -> Option> { // Truncate the dir string to the maximum number of path components let truncated_dir_string = truncate(substituted_dir, config.truncation_length as usize); - let prefix = if is_truncated(&truncated_dir_string) { + let prefix = if is_truncated(&truncated_dir_string, &home_symbol) { // Substitutions could have changed the prefix, so don't allow them and // fish-style path contraction together if config.fish_style_pwd_dir_length > 0 && config.substitutions.is_empty() { // If user is using fish style path, we need to add the segment first - let contracted_home_dir = contract_path(¤t_dir, &home_dir, HOME_SYMBOL); + let contracted_home_dir = contract_path(¤t_dir, &home_dir, &home_symbol); to_fish_style( config.fish_style_pwd_dir_length as usize, contracted_home_dir, @@ -113,8 +113,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { Some(module) } -fn is_truncated(path: &str) -> bool { - !(path.starts_with(HOME_SYMBOL) +fn is_truncated(path: &str, home_symbol: &str) -> bool { + !(path.starts_with(&home_symbol) || PathBuf::from(path).has_root() || (cfg!(target_os = "windows") && PathBuf::from(String::from(path) + r"\").has_root())) } @@ -549,15 +549,41 @@ mod tests { } #[test] - fn home_directory() -> io::Result<()> { + fn home_directory_default_home_symbol() -> io::Result<()> { let actual = ModuleRenderer::new("directory") .path(home_dir().unwrap()) - .config(toml::toml! { // Necessary if homedir is a git repo + .collect(); + let expected = Some(format!("{} ", Color::Cyan.bold().paint("~"))); + + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn home_directory_custom_home_symbol() -> io::Result<()> { + let actual = ModuleRenderer::new("directory") + .path(home_dir().unwrap()) + .config(toml::toml! { [directory] - truncate_to_repo = false + home_symbol = "🚀" }) .collect(); - let expected = Some(format!("{} ", Color::Cyan.bold().paint("~"))); + let expected = Some(format!("{} ", Color::Cyan.bold().paint("🚀"))); + + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn home_directory_custom_home_symbol_subdirectories() -> io::Result<()> { + let actual = ModuleRenderer::new("directory") + .path(home_dir().unwrap().join("path/subpath")) + .config(toml::toml! { + [directory] + home_symbol = "🚀" + }) + .collect(); + let expected = Some(format!("{} ", Color::Cyan.bold().paint("🚀/path/subpath"))); assert_eq!(expected, actual); Ok(()) -- cgit v1.2.3