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_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. |
<details>
<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_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: "~",
}
}
}

View File

@ -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<Module<'a>> {
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<Module<'a>> {
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<Module<'a>> {
// 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(&current_dir, &home_dir, HOME_SYMBOL);
let contracted_home_dir = contract_path(&current_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<Module<'a>> {
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,13 +549,9 @@ 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
[directory]
truncate_to_repo = false
})
.collect();
let expected = Some(format!("{} ", Color::Cyan.bold().paint("~")));
@ -563,6 +559,36 @@ mod tests {
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]
fn substituted_truncated_path() -> io::Result<()> {
let actual = ModuleRenderer::new("directory")