1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-26 03:27:30 +00:00

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 ## PHP
The `php` module shows the currently installed version of 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 `composer.json` file
- The current directory contains a `.php-version` file - The current directory contains a `.php-version` file
- The current directory contains a `.php` file - The current directory contains a `.php` extension
### 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` | `"🐘 "` | The symbol used before displaying the version of PHP. | | `symbol` | `"🐘 "` | The symbol used before displaying the version of PHP. |
| `style` | `"147 bold"` | The style for the module. | | `detect_extensions` | `["php"]` | Which extensions should trigger this moudle. |
| `disabled` | `false` | Disables the `php` module. | | `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 ### Variables

View File

@ -8,6 +8,9 @@ pub struct PhpConfig<'a> {
pub style: &'a str, pub style: &'a str,
pub format: &'a str, pub format: &'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 PhpConfig<'a> { impl<'a> RootModuleConfig<'a> for PhpConfig<'a> {
@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for PhpConfig<'a> {
style: "147 bold", style: "147 bold",
format: "via [$symbol($version )]($style)", format: "via [$symbol($version )]($style)",
disabled: false, 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; use crate::formatter::StringFormatter;
/// Creates a module with the current PHP version /// 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>> { 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 let is_php_project = context
.try_begin_scan()? .try_begin_scan()?
.set_files(&["composer.json", ".php-version"]) .set_files(&config.detect_files)
.set_extensions(&["php"]) .set_folders(&config.detect_folders)
.set_extensions(&config.detect_extensions)
.is_match(); .is_match();
if !is_php_project { if !is_php_project {
return None; 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| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|variable, _| match variable { .map_meta(|variable, _| match variable {