Skip to content

Commit

Permalink
feat(directory): add before_repo_root_style (#4595)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgzones committed Dec 1, 2022
1 parent ddd54e9 commit ea6249b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
12 changes: 10 additions & 2 deletions .github/config-schema.json
Expand Up @@ -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": {},
Expand Down Expand Up @@ -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": {
Expand All @@ -2482,6 +2483,13 @@
"null"
]
},
"before_repo_root_style": {
"default": null,
"type": [
"string",
"null"
]
},
"disabled": {
"default": false,
"type": "boolean"
Expand Down
29 changes: 15 additions & 14 deletions docs/config/README.md
Expand Up @@ -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) |

<details>
<summary>This module has a few advanced configuration options that control how the directory is displayed.</summary>
Expand Down
4 changes: 3 additions & 1 deletion src/configs/directory.rs
Expand Up @@ -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,
Expand All @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions src/modules/directory.rs
Expand Up @@ -137,13 +137,15 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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
.map_style(|variable| match variable {
"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 {
Expand Down Expand Up @@ -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"))
));
Expand Down

0 comments on commit ea6249b

Please sign in to comment.