feat(php): Configure when the module is shown (#2356)

This makes it possible to configure when the php 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:
Shu Kutsuzawa 2021-02-22 03:52:19 +09:00 committed by GitHub
parent 51f752f6b0
commit efb82454f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 17 deletions

View File

@ -2006,20 +2006,23 @@ format = "via [🦪 $version]($style) "
## PHP
The `php` module shows the currently installed version of PHP.
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 `composer.json` file
- The current directory contains a `.php-version` file
- The current directory contains a `.php` file
- The current directory contains a `.php` extension
### Options
| Option | Default | Description |
| ---------- | ------------------------------------ | ----------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"🐘 "` | The symbol used before displaying the version of PHP. |
| `style` | `"147 bold"` | The style for the module. |
| `disabled` | `false` | Disables the `php` module. |
| Option | Default | Description |
| -------------------- | ------------------------------------ | ----------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"🐘 "` | The symbol used before displaying the version of PHP. |
| `detect_extensions` | `["php"]` | Which extensions should trigger this moudle. |
| `detect_files` | `["composer.json", ".php-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"147 bold"` | The style for the module. |
| `disabled` | `false` | Disables the `php` module. |
### Variables

View File

@ -8,6 +8,9 @@ pub struct PhpConfig<'a> {
pub style: &'a str,
pub format: &'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 PhpConfig<'a> {
@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for PhpConfig<'a> {
style: "147 bold",
format: "via [$symbol($version )]($style)",
disabled: false,
detect_extensions: vec!["php"],
detect_files: vec!["composer.json", ".php-version"],
detect_folders: vec![],
}
}
}

View File

@ -4,24 +4,20 @@ use crate::configs::php::PhpConfig;
use crate::formatter::StringFormatter;
/// Creates a module with the current PHP version
///
/// Will display the PHP version if any of the following criteria are met:
/// - Current directory contains a `.php` file
/// - Current directory contains a `composer.json` or `.php-version` file
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("php");
let config: PhpConfig = PhpConfig::try_load(module.config);
let is_php_project = context
.try_begin_scan()?
.set_files(&["composer.json", ".php-version"])
.set_extensions(&["php"])
.set_files(&config.detect_files)
.set_folders(&config.detect_folders)
.set_extensions(&config.detect_extensions)
.is_match();
if !is_php_project {
return None;
}
let mut module = context.new_module("php");
let config: PhpConfig = PhpConfig::try_load(module.config);
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {