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
3 changed files with 22 additions and 17 deletions
+6
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![],
}
}
}
+5 -9
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 {