Skip to content

Commit

Permalink
feat(nim): Configure when the module is shown (starship#2347)
Browse files Browse the repository at this point in the history
This makes it possible to configure when the nim module is shown
based on the contents of a directory. This should make it possible to
be a lot more granular when configuring the module.
  • Loading branch information
cappyzawa authored Feb 21, 2021
1 parent 873a005 commit 4651060
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
17 changes: 10 additions & 7 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ truncation_symbol = ""
## Nim

The `nim` module shows the currently installed version of Nim.
The module will be shown if any of the following conditions are met:
By default the module will be shown if any of the following conditions are met:

- The current directory contains a `nim.cfg` file
- The current directory contains a file with the `.nim` extension
Expand All @@ -1717,12 +1717,15 @@ The module will be shown if any of the following conditions are met:

### Options

| Option | Default | Description |
| ---------- | ---------------------------------- | ----------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module |
| `symbol` | `"👑 "` | The symbol used before displaying the version of Nim. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `nim` module. |
| Option | Default | Description |
| -------------------- | ------------------------------------ | ----------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module |
| `symbol` | `"👑 "` | The symbol used before displaying the version of Nim. |
| `detect_extensions` | `["nim", "nims", "nimble"]` | Which extensions should trigger this moudle. |
| `detect_files` | `["nim.cfg"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `nim` module. |

### Variables

Expand Down
6 changes: 6 additions & 0 deletions src/configs/nim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub struct NimConfig<'a> {
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for NimConfig<'a> {
Expand All @@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for NimConfig<'a> {
symbol: "👑 ",
style: "yellow bold",
disabled: false,
detect_extensions: vec!["nim", "nims", "nimble"],
detect_files: vec!["nim.cfg"],
detect_folders: vec![],
}
}
}
14 changes: 5 additions & 9 deletions src/modules/nim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ use crate::configs::nim::NimConfig;
use crate::formatter::StringFormatter;

/// Creates a module with the current Nim version
///
/// Will display the Nim version if any of the following criteria are met:
/// - The current directory contains a file with extension `.nim`, `.nims`, or `.nimble`
/// - The current directory contains a `nim.cfg` file
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("nim");
let config = NimConfig::try_load(module.config);
let is_nim_project = context
.try_begin_scan()?
.set_files(&["nim.cfg"])
.set_extensions(&["nim", "nims", "nimble"])
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_files)
.is_match();

if !is_nim_project {
return None;
}

let mut module = context.new_module("nim");
let config = NimConfig::try_load(module.config);

let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
Expand Down

0 comments on commit 4651060

Please sign in to comment.