Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(custom): add option to check if pwd is in a repo #4822

Merged
merged 4 commits into from Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/config-schema.json
Expand Up @@ -5649,6 +5649,10 @@
}
]
},
"require_repo": {
"default": false,
"type": "boolean"
},
"shell": {
"default": [],
"allOf": [
Expand Down
1 change: 1 addition & 0 deletions docs/config/README.md
Expand Up @@ -4307,6 +4307,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. |
Expand Down
2 changes: 2 additions & 0 deletions src/configs/custom.rs
Expand Up @@ -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,
Expand All @@ -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",
Expand Down
42 changes: 41 additions & 1 deletion src/modules/custom.rs
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}
}