summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjliaoh <48660001+hunterliao29@users.noreply.github.com>2023-04-02 10:39:45 -0400
committerGitHub <noreply@github.com>2023-04-02 16:39:45 +0200
commitd29ce7c45d4ea21a6e14ad308bd50cb0e61d1ef8 (patch)
tree74ee7d5c91dc389679557752a5ef1a2132c1ea70
parentd2801ac44301dcef1f87ab5fd26abee36997f71d (diff)
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>
-rw-r--r--.github/config-schema.json4
-rw-r--r--docs/config/README.md1
-rw-r--r--src/configs/custom.rs2
-rw-r--r--src/modules/custom.rs42
4 files changed, 48 insertions, 1 deletions
diff --git a/.github/config-schema.json b/.github/config-schema.json
index 67988de3a..cf0ced4fa 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 118baf63f..32d3d3afb 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` | `'<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. |
diff --git a/src/configs/custom.rs b/src/configs/custom.rs
index c4026f54d..e5c13b240 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<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",
diff --git a/src/modules/custom.rs b/src/modules/custom.rs
index e224958a4..f3dde1185 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<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()
+ }
}