feat(erlang): Configure when the module is shown (#2346)

* feat(erlang): Configure when the module is shown

This makes it possible to configure when the erlang 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.

* Update docs/config/README.md

Co-authored-by: Shu Kutsuzawa <cappyzawa@gmail.com>

Co-authored-by: Shu Kutsuzawa <cappyzawa@gmail.com>
This commit is contained in:
Thomas O'Donnell 2021-02-21 13:20:14 +01:00 committed by GitHub
parent 7331e4bec6
commit 873a005c42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 15 deletions

View File

@ -944,19 +944,22 @@ default = "unknown shell"
## Erlang
The `erlang` module shows the currently installed version of Erlang/OTP.
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 `rebar.config` file.
- The current directory contains a `erlang.mk` file.
### Options
| Option | Default | Description |
| ---------- | ---------------------------------- | -------------------------------------------------------- |
| `symbol` | `" "` | The symbol used before displaying the version of erlang. |
| `style` | `"bold red"` | The style for the module. |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `disabled` | `false` | Disables the `erlang` module. |
| Option | Default | Description |
| ------------------- | ------------------------------------ | -------------------------------------------------------- |
| `symbol` | `" "` | The symbol used before displaying the version of erlang. |
| `style` | `"bold red"` | The style for the module. |
| `detect_extensions` | `[]` | Which extensions should trigger this module. |
| `detect_files` | `["rebar.config", "elang.mk"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this modules. |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `disabled` | `false` | Disables the `erlang` module. |
### Variables

View File

@ -8,6 +8,9 @@ pub struct ErlangConfig<'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 ErlangConfig<'a> {
@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for ErlangConfig<'a> {
symbol: "",
style: "bold red",
disabled: false,
detect_extensions: vec![],
detect_files: vec!["rebar.config", "erlang.mk"],
detect_folders: vec![],
}
}
}

View File

@ -4,23 +4,21 @@ use crate::configs::erlang::ErlangConfig;
use crate::formatter::StringFormatter;
/// Create a module with the current Erlang version
///
/// Will display the Erlang version if any of the following criteria are met:
/// - Current directory contains a rebar.config file
/// - Current directory contains a erlang.mk file
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("erlang");
let config = ErlangConfig::try_load(module.config);
let is_erlang_project = context
.try_begin_scan()?
.set_files(&["rebar.config", "erlang.mk"])
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();
if !is_erlang_project {
return None;
}
let mut module = context.new_module("erlang");
let config = ErlangConfig::try_load(module.config);
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {