From ea6249b5243acf0cce2352a1b580479546b92340 Mon Sep 17 00:00:00 2001 From: cgzones Date: Thu, 1 Dec 2022 23:09:11 +0100 Subject: [PATCH] feat(directory): add before_repo_root_style (#4595) --- .github/config-schema.json | 12 ++++++++++-- docs/config/README.md | 29 +++++++++++++++-------------- src/configs/directory.rs | 4 +++- src/modules/directory.rs | 8 ++++++-- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 06040bbc7b9a..d43b396da320 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -330,13 +330,14 @@ }, "directory": { "default": { + "before_repo_root_style": null, "disabled": false, "fish_style_pwd_dir_length": 0, "format": "[$path]($style)[$read_only]($read_only_style) ", "home_symbol": "~", "read_only": "🔒", "read_only_style": "red", - "repo_root_format": "[$before_root_path]($style)[$repo_root]($repo_root_style)[$path]($style)[$read_only]($read_only_style) ", + "repo_root_format": "[$before_root_path]($before_repo_root_style)[$repo_root]($repo_root_style)[$path]($style)[$read_only]($read_only_style) ", "repo_root_style": null, "style": "cyan bold", "substitutions": {}, @@ -2468,7 +2469,7 @@ "type": "string" }, "repo_root_format": { - "default": "[$before_root_path]($style)[$repo_root]($repo_root_style)[$path]($style)[$read_only]($read_only_style) ", + "default": "[$before_root_path]($before_repo_root_style)[$repo_root]($repo_root_style)[$path]($style)[$read_only]($read_only_style) ", "type": "string" }, "style": { @@ -2482,6 +2483,13 @@ "null" ] }, + "before_repo_root_style": { + "default": null, + "type": [ + "string", + "null" + ] + }, "disabled": { "default": false, "type": "boolean" diff --git a/docs/config/README.md b/docs/config/README.md index 0715a4c27279..426980cb2bc2 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1103,20 +1103,21 @@ it would have been `nixpkgs/pkgs`. ### Options -| Option | Default | Description | -| ------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ | -| `truncation_length` | `3` | The number of parent folders that the current directory should be truncated to. | -| `truncate_to_repo` | `true` | Whether or not to truncate to the root of the git repo that you're currently in. | -| `format` | `'[$path]($style)[$read_only]($read_only_style) '` | The format for the module. | -| `style` | `'bold cyan'` | The style for the module. | -| `disabled` | `false` | Disables the `directory` module. | -| `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: '…/' | -| `repo_root_style` | | The style for the root of the git repo. The default value is equivalent to `style`. | -| `repo_root_format` | `'[$before_root_path]($style)[$repo_root]($repo_root_style)[$path]($style)[$read_only]($read_only_style) '` | The format of a git repo when `repo_root_style` is defined. | -| `home_symbol` | `'~'` | The symbol indicating home directory. | -| `use_os_path_sep` | `true` | Use the OS specific path separator instead of always using `/` (e.g. `\` on Windows) | +| Option | Default | Description | +| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | +| `truncation_length` | `3` | The number of parent folders that the current directory should be truncated to. | +| `truncate_to_repo` | `true` | Whether or not to truncate to the root of the git repo that you're currently in. | +| `format` | `'[$path]($style)[$read_only]($read_only_style) '` | The format for the module. | +| `style` | `'bold cyan'` | The style for the module. | +| `disabled` | `false` | Disables the `directory` module. | +| `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: '…/' | +| `before_repo_root_style` | | The style for the path segment above the root of the git repo. The default value is equivalent to `style`. | +| `repo_root_style` | | The style for the root of the git repo. The default value is equivalent to `style`. | +| `repo_root_format` | `'[$before_root_path]($before_repo_root_style)[$repo_root]($repo_root_style)[$path]($style)[$read_only]($read_only_style) '` | The format of a git repo when `before_repo_root_style` and `repo_root_style` is defined. | +| `home_symbol` | `'~'` | The symbol indicating home directory. | +| `use_os_path_sep` | `true` | Use the OS specific path separator instead of always using `/` (e.g. `\` on Windows) |
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 2701e74bd437..f98a3a30fa97 100644 --- a/src/configs/directory.rs +++ b/src/configs/directory.rs @@ -19,6 +19,7 @@ pub struct DirectoryConfig<'a> { pub repo_root_format: &'a str, pub style: &'a str, pub repo_root_style: Option<&'a str>, + pub before_repo_root_style: Option<&'a str>, pub disabled: bool, pub read_only: &'a str, pub read_only_style: &'a str, @@ -36,9 +37,10 @@ impl<'a> Default for DirectoryConfig<'a> { use_logical_path: true, substitutions: IndexMap::new(), format: "[$path]($style)[$read_only]($read_only_style) ", - repo_root_format: "[$before_root_path]($style)[$repo_root]($repo_root_style)[$path]($style)[$read_only]($read_only_style) ", + repo_root_format: "[$before_root_path]($before_repo_root_style)[$repo_root]($repo_root_style)[$path]($style)[$read_only]($read_only_style) ", style: "cyan bold", repo_root_style: None, + before_repo_root_style: None, disabled: false, read_only: "🔒", read_only_style: "red", diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 63cce307004f..916e41c70bd8 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -137,6 +137,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { config.repo_root_format }; let repo_root_style = config.repo_root_style.unwrap_or(config.style); + let before_repo_root_style = config.before_repo_root_style.unwrap_or(config.style); let parsed = StringFormatter::new(display_format).and_then(|formatter| { formatter @@ -144,6 +145,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { "style" => Some(Ok(config.style)), "read_only_style" => Some(Ok(config.read_only_style)), "repo_root_style" => Some(Ok(repo_root_style)), + "before_repo_root_style" => Some(Ok(before_repo_root_style)), _ => None, }) .map(|variable| match variable { @@ -1694,12 +1696,14 @@ mod tests { truncation_symbol = "…/" truncate_to_repo = false repo_root_style = "green" + before_repo_root_style = "blue" }) .path(dir) .collect(); let expected = Some(format!( - "{}{}repo{} ", - Color::Cyan.bold().paint(convert_path_sep("…/above/")), + "{}{}{}repo{} ", + Color::Blue.prefix(), + convert_path_sep("…/above/"), Color::Green.prefix(), Color::Cyan.bold().paint(convert_path_sep("/src/sub/path")) ));