feat(vagrant): Configure when the module is shown (#2314)

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

This makes it possible to configure when the vagrant module is shown based on the contents of a directory.

* fix documentation

Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>

Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
This commit is contained in:
David Knaack 2021-02-15 20:21:21 +01:00 committed by GitHub
parent 6bd4e724e9
commit 856610d53b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 13 deletions

View File

@ -2528,18 +2528,21 @@ show_always = true
## Vagrant ## Vagrant
The `vagrant` module shows the currently installed version of Vagrant. The `vagrant` module shows the currently installed version of Vagrant.
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 `Vagrantfile` file - The current directory contains a `Vagrantfile` file
### 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 Vagrant. | | `symbol` | `"⍱ "` | A format string representing the symbol of Vagrant. |
| `style` | `"cyan bold"` | The style for the module. | | `detect_extensions` | `[]` | Which extensions should trigger this module. |
| `disabled` | `false` | Disables the `Vagrant` module. | | `detect_files` | `["Vagrantfile"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"cyan bold"` | The style for the module. |
| `disabled` | `false` | Disables the `Vagrant` module. |
### Variables ### Variables

View File

@ -8,6 +8,9 @@ pub struct VagrantConfig<'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 VagrantConfig<'a> { impl<'a> RootModuleConfig<'a> for VagrantConfig<'a> {
@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for VagrantConfig<'a> {
symbol: "", symbol: "",
style: "cyan bold", style: "cyan bold",
disabled: false, disabled: false,
detect_extensions: vec![],
detect_files: vec!["Vagrantfile"],
detect_folders: vec![],
} }
} }
} }

View File

@ -4,21 +4,21 @@ use crate::configs::vagrant::VagrantConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
/// Creates a module with the current Vagrant version /// Creates a module with the current Vagrant version
///
/// Will display the Vagrant version if any of the following criteria are met:
/// - Current directory contains a `Vagrantfile` file
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("vagrant");
let config = VagrantConfig::try_load(module.config);
let is_vagrant_project = context let is_vagrant_project = context
.try_begin_scan()? .try_begin_scan()?
.set_files(&["Vagrantfile"]) .set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match(); .is_match();
if !is_vagrant_project { if !is_vagrant_project {
return None; return None;
} }
let mut module = context.new_module("vagrant");
let config = VagrantConfig::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 {