feat(custom): add option to check if pwd is in a repo (#4822)
* feat(custom): add option to check if pwd is in a repo * Apply suggestions from code review Co-authored-by: David Knaack <davidkna@users.noreply.github.com> * change whenrepo to require_repo * chore: fix doc formatting --------- Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
parent
d2801ac443
commit
d29ce7c45d
|
@ -5701,6 +5701,10 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"require_repo": {
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
},
|
||||
"shell": {
|
||||
"default": [],
|
||||
"allOf": [
|
||||
|
|
|
@ -4346,6 +4346,7 @@ Format strings can also contain shell specific prompt sequences, e.g.
|
|||
| ------------------- | ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `command` | `''` | The command whose output should be printed. The command will be passed on stdin to the shell. |
|
||||
| `when` | `false` | Either a boolean value (`true` or `false`, without quotes) or a string shell command used as a condition to show the module. In case of a string, the module will be shown if the command returns a `0` status code. |
|
||||
| `require_repo` | `false` | If `true`, the module will only be shown in paths containing a (git) repository. This option alone is not sufficient display condition in absence of other options. |
|
||||
| `shell` | | [See below](#custom-command-shell) |
|
||||
| `description` | `'<custom module>'` | The description of the module that is shown when running `starship explain`. |
|
||||
| `detect_files` | `[]` | The files that will be searched in the working directory for a match. |
|
||||
|
|
|
@ -14,6 +14,7 @@ pub struct CustomConfig<'a> {
|
|||
pub symbol: &'a str,
|
||||
pub command: &'a str,
|
||||
pub when: Either<bool, &'a str>,
|
||||
pub require_repo: bool,
|
||||
pub shell: VecOr<&'a str>,
|
||||
pub description: &'a str,
|
||||
pub style: &'a str,
|
||||
|
@ -38,6 +39,7 @@ impl<'a> Default for CustomConfig<'a> {
|
|||
symbol: "",
|
||||
command: "",
|
||||
when: Either::First(false),
|
||||
require_repo: false,
|
||||
shell: VecOr::default(),
|
||||
description: "<custom config>",
|
||||
style: "green bold",
|
||||
|
|
|
@ -34,6 +34,10 @@ pub fn module<'a>(name: &str, context: &'a Context) -> Option<Module<'a>> {
|
|||
}
|
||||
}
|
||||
|
||||
if config.require_repo && context.get_repo().is_err() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Note: Forward config if `Module` ends up needing `config`
|
||||
let mut module = Module::new(&format!("custom.{name}"), config.description, None);
|
||||
|
||||
|
@ -294,7 +298,7 @@ fn handle_shell(command: &mut Command, shell: &str, shell_args: &[&str]) -> bool
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use crate::test::ModuleRenderer;
|
||||
use crate::test::{fixture_repo, FixtureProvider, ModuleRenderer};
|
||||
use nu_ansi_term::Color;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
|
@ -721,4 +725,40 @@ mod tests {
|
|||
let expected = None;
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_require_repo_not_in() -> io::Result<()> {
|
||||
let repo_dir = tempfile::tempdir()?;
|
||||
|
||||
let actual = ModuleRenderer::new("custom.test")
|
||||
.path(repo_dir.path())
|
||||
.config(toml::toml! {
|
||||
[custom.test]
|
||||
when = true
|
||||
require_repo = true
|
||||
format = "test"
|
||||
})
|
||||
.collect();
|
||||
let expected = None;
|
||||
assert_eq!(expected, actual);
|
||||
repo_dir.close()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_require_repo_in() -> io::Result<()> {
|
||||
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
||||
|
||||
let actual = ModuleRenderer::new("custom.test")
|
||||
.path(repo_dir.path())
|
||||
.config(toml::toml! {
|
||||
[custom.test]
|
||||
when = true
|
||||
require_repo = true
|
||||
format = "test"
|
||||
})
|
||||
.collect();
|
||||
let expected = Some("test".to_string());
|
||||
assert_eq!(expected, actual);
|
||||
repo_dir.close()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue