feat(directory): Add home directory symbol (#2198)

* feat(directory): Add home directory symbol

* Replace HOME_SYMBOL constant as a config variable
This commit is contained in:
Rodrigo Suárez 2021-01-24 18:50:37 -03:00 committed by GitHub
parent b2e8252785
commit ca36d15acd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 14 deletions

View File

@ -656,6 +656,7 @@ it would have been `nixpkgs/pkgs`.
| `read_only` | `"🔒"` | The symbol indicating current directory is read only. | | `read_only` | `"🔒"` | The symbol indicating current directory is read only. |
| `read_only_style` | `"red"` | The style for the read only symbol. | | `read_only_style` | `"red"` | The style for the read only symbol. |
| `truncation_symbol` | `""` | The symbol to prefix to truncated paths. eg: "…/" | | `truncation_symbol` | `""` | The symbol to prefix to truncated paths. eg: "…/" |
| `home_symbol` | `"~"` | The symbol indicating home directory. |
<details> <details>
<summary>This module has a few advanced configuration options that control how the directory is displayed.</summary> <summary>This module has a few advanced configuration options that control how the directory is displayed.</summary>

View File

@ -16,6 +16,7 @@ pub struct DirectoryConfig<'a> {
pub read_only: &'a str, pub read_only: &'a str,
pub read_only_style: &'a str, pub read_only_style: &'a str,
pub truncation_symbol: &'a str, pub truncation_symbol: &'a str,
pub home_symbol: &'a str,
} }
impl<'a> RootModuleConfig<'a> for DirectoryConfig<'a> { impl<'a> RootModuleConfig<'a> for DirectoryConfig<'a> {
@ -32,6 +33,7 @@ impl<'a> RootModuleConfig<'a> for DirectoryConfig<'a> {
read_only: "🔒", read_only: "🔒",
read_only_style: "red", read_only_style: "red",
truncation_symbol: "", truncation_symbol: "",
home_symbol: "~",
} }
} }
} }

View File

@ -16,8 +16,6 @@ use crate::configs::directory::DirectoryConfig;
use crate::context::Shell; use crate::context::Shell;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
const HOME_SYMBOL: &str = "~";
/// Creates a module with the current directory /// Creates a module with the current directory
/// ///
/// Will perform path contraction, substitution, and truncation. /// Will perform path contraction, substitution, and truncation.
@ -25,7 +23,7 @@ const HOME_SYMBOL: &str = "~";
/// **Contraction** /// **Contraction**
/// ///
/// - Paths beginning with the home directory or with a git repo right inside /// - 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 /// - Paths containing a git repo will contract to begin at the repo root
/// ///
/// **Substitution** /// **Substitution**
@ -40,6 +38,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let current_dir = &get_current_dir(&context, &config); let current_dir = &get_current_dir(&context, &config);
let home_dir = context.get_home().unwrap(); let home_dir = context.get_home().unwrap();
let home_symbol = String::from(config.home_symbol);
log::debug!("Current directory: {:?}", current_dir); log::debug!("Current directory: {:?}", current_dir);
let repo = &context.get_repo().ok()?; let repo = &context.get_repo().ok()?;
@ -48,10 +48,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
log::debug!("Repo root: {:?}", repo_root); log::debug!("Repo root: {:?}", repo_root);
// Contract the path to the git repo root // Contract the path to the git repo root
contract_repo_path(current_dir, 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 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); log::debug!("Dir string: {}", dir_string);
@ -60,12 +60,12 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
// Truncate the dir string to the maximum number of path components // Truncate the dir string to the maximum number of path components
let truncated_dir_string = truncate(substituted_dir, config.truncation_length as usize); 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 // Substitutions could have changed the prefix, so don't allow them and
// fish-style path contraction together // fish-style path contraction together
if config.fish_style_pwd_dir_length > 0 && config.substitutions.is_empty() { 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 // If user is using fish style path, we need to add the segment first
let contracted_home_dir = contract_path(&current_dir, &home_dir, HOME_SYMBOL); let contracted_home_dir = contract_path(&current_dir, &home_dir, &home_symbol);
to_fish_style( to_fish_style(
config.fish_style_pwd_dir_length as usize, config.fish_style_pwd_dir_length as usize,
contracted_home_dir, contracted_home_dir,
@ -113,8 +113,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) Some(module)
} }
fn is_truncated(path: &str) -> bool { fn is_truncated(path: &str, home_symbol: &str) -> bool {
!(path.starts_with(HOME_SYMBOL) !(path.starts_with(&home_symbol)
|| PathBuf::from(path).has_root() || PathBuf::from(path).has_root()
|| (cfg!(target_os = "windows") && PathBuf::from(String::from(path) + r"\").has_root())) || (cfg!(target_os = "windows") && PathBuf::from(String::from(path) + r"\").has_root()))
} }
@ -549,13 +549,9 @@ mod tests {
} }
#[test] #[test]
fn home_directory() -> io::Result<()> { fn home_directory_default_home_symbol() -> io::Result<()> {
let actual = ModuleRenderer::new("directory") let actual = ModuleRenderer::new("directory")
.path(home_dir().unwrap()) .path(home_dir().unwrap())
.config(toml::toml! { // Necessary if homedir is a git repo
[directory]
truncate_to_repo = false
})
.collect(); .collect();
let expected = Some(format!("{} ", Color::Cyan.bold().paint("~"))); let expected = Some(format!("{} ", Color::Cyan.bold().paint("~")));
@ -563,6 +559,36 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn home_directory_custom_home_symbol() -> io::Result<()> {
let actual = ModuleRenderer::new("directory")
.path(home_dir().unwrap())
.config(toml::toml! {
[directory]
home_symbol = "🚀"
})
.collect();
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(())
}
#[test] #[test]
fn substituted_truncated_path() -> io::Result<()> { fn substituted_truncated_path() -> io::Result<()> {
let actual = ModuleRenderer::new("directory") let actual = ModuleRenderer::new("directory")