1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 09:13:54 +00:00

feat(elm): Configure when the module is shown (#2341)

This makes it possible to configure when the elm 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.
This commit is contained in:
Thomas O'Donnell 2021-02-20 18:33:00 +01:00 committed by GitHub
parent 9313e90773
commit 71b6fc1777
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 20 deletions

View File

@ -864,7 +864,7 @@ symbol = "🔮 "
## Elm ## Elm
The `elm` module shows the currently installed version of Elm. The `elm` module shows the currently installed version of Elm.
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 `elm.json` file - The current directory contains a `elm.json` file
- The current directory contains a `elm-package.json` file - The current directory contains a `elm-package.json` file
@ -875,9 +875,12 @@ The module will be shown if any of the following conditions are met:
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ---------- | ------------------------------------ | ----------------------------------------------- | | ------------------- | -------------------------------------------------- | ----------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | | `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"🌳 "` | A format string representing the symbol of Elm. | | `symbol` | `"🌳 "` | A format string representing the symbol of Elm. |
| `detect_extensions` | `["elm"]` | Which extensions should trigger this module. |
| `detect_files` | `["elm.json", "elm-package.json", ".elm-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `["elm-stuff"]` | Which folders should trigger this modules. |
| `style` | `"cyan bold"` | The style for the module. | | `style` | `"cyan bold"` | The style for the module. |
| `disabled` | `false` | Disables the `elm` module. | | `disabled` | `false` | Disables the `elm` module. |

View File

@ -8,6 +8,9 @@ pub struct ElmConfig<'a> {
pub symbol: &'a str, pub symbol: &'a str,
pub style: &'a str, pub style: &'a str,
pub disabled: bool, 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 ElmConfig<'a> { impl<'a> RootModuleConfig<'a> for ElmConfig<'a> {
@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for ElmConfig<'a> {
symbol: "🌳 ", symbol: "🌳 ",
style: "cyan bold", style: "cyan bold",
disabled: false, disabled: false,
detect_extensions: vec!["elm"],
detect_files: vec!["elm.json", "elm-package.json", ".elm-version"],
detect_folders: vec!["elm-stuff"],
} }
} }
} }

View File

@ -4,28 +4,21 @@ use crate::configs::elm::ElmConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
/// Creates a module with the current Elm version /// Creates a module with the current Elm version
///
/// Will display the Elm version if any of the following criteria are met:
/// - The current directory contains a `elm.json` file
/// - The current directory contains a `elm-package.json` file
/// - The current directory contains a `.elm-version` file
/// - The current directory contains a `elm-stuff` folder
/// - The current directory contains a `*.elm` files
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("elm");
let config: ElmConfig = ElmConfig::try_load(module.config);
let is_elm_project = context let is_elm_project = context
.try_begin_scan()? .try_begin_scan()?
.set_files(&["elm.json", "elm-package.json", ".elm-version"]) .set_files(&config.detect_files)
.set_extensions(&["elm"]) .set_extensions(&config.detect_extensions)
.set_folders(&["elm-stuff"]) .set_folders(&config.detect_folders)
.is_match(); .is_match();
if !is_elm_project { if !is_elm_project {
return None; return None;
} }
let mut module = context.new_module("elm");
let config: ElmConfig = ElmConfig::try_load(module.config);
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|var, _| match var { .map_meta(|var, _| match var {