From d29ce7c45d4ea21a6e14ad308bd50cb0e61d1ef8 Mon Sep 17 00:00:00 2001 From: jliaoh <48660001+hunterliao29@users.noreply.github.com> Date: Sun, 2 Apr 2023 10:39:45 -0400 Subject: [PATCH] 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 * change whenrepo to require_repo * chore: fix doc formatting --------- Co-authored-by: David Knaack --- .github/config-schema.json | 4 ++++ docs/config/README.md | 1 + src/configs/custom.rs | 2 ++ src/modules/custom.rs | 42 +++++++++++++++++++++++++++++++++++++- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 67988de3..cf0ced4f 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -5701,6 +5701,10 @@ } ] }, + "require_repo": { + "default": false, + "type": "boolean" + }, "shell": { "default": [], "allOf": [ diff --git a/docs/config/README.md b/docs/config/README.md index 118baf63..32d3d3af 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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` | `''` | 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. | diff --git a/src/configs/custom.rs b/src/configs/custom.rs index c4026f54..e5c13b24 100644 --- a/src/configs/custom.rs +++ b/src/configs/custom.rs @@ -14,6 +14,7 @@ pub struct CustomConfig<'a> { pub symbol: &'a str, pub command: &'a str, pub when: Either, + 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: "", style: "green bold", diff --git a/src/modules/custom.rs b/src/modules/custom.rs index e224958a..f3dde118 100644 --- a/src/modules/custom.rs +++ b/src/modules/custom.rs @@ -34,6 +34,10 @@ pub fn module<'a>(name: &str, context: &'a Context) -> Option> { } } + 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() + } }