perf(php): Lazy eval php (#2190)

* perf(php): evaluate version lazily

* fix(php): update format string; update tests

Co-authored-by: Moritz Vetter <mv@3yourmind.com>
This commit is contained in:
Moritz Vetter
2021-01-22 18:03:18 +01:00
committed by GitHub
parent 60be1e9540
commit d10a83ce6b
3 changed files with 47 additions and 49 deletions
+6 -6
View File
@@ -1964,12 +1964,12 @@ The module will be shown if any of the following conditions are met:
### 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. | | `style` | `"147 bold"` | The style for the module. |
| `disabled` | `false` | Disables the `php` module. | | `disabled` | `false` | Disables the `php` module. |
### Variables ### Variables
+1 -1
View File
@@ -15,7 +15,7 @@ impl<'a> RootModuleConfig<'a> for PhpConfig<'a> {
PhpConfig { PhpConfig {
symbol: "🐘 ", symbol: "🐘 ",
style: "147 bold", style: "147 bold",
format: "via [$symbol$version]($style) ", format: "via [$symbol($version )]($style)",
disabled: false, disabled: false,
} }
} }
+40 -42
View File
@@ -20,46 +20,44 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; return None;
} }
match utils::exec_cmd( let mut module = context.new_module("php");
"php", let config: PhpConfig = PhpConfig::try_load(module.config);
&[
"-nr",
"echo PHP_MAJOR_VERSION.\".\".PHP_MINOR_VERSION.\".\".PHP_RELEASE_VERSION;",
],
) {
Some(php_cmd_output) => {
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 {
"symbol" => Some(config.symbol), "symbol" => Some(config.symbol),
_ => None, _ => None,
}) })
.map_style(|variable| match variable { .map_style(|variable| match variable {
"style" => Some(Ok(config.style)), "style" => Some(Ok(config.style)),
_ => None, _ => None,
}) })
.map(|variable| match variable { .map(|variable| match variable {
"version" => Some(Ok(format_php_version(&php_cmd_output.stdout))), "version" => {
_ => None, let php_cmd_output = utils::exec_cmd(
}) "php",
.parse(None) &[
}); "-nr",
"echo PHP_MAJOR_VERSION.\".\".PHP_MINOR_VERSION.\".\".PHP_RELEASE_VERSION;",
module.set_segments(match parsed { ],
Ok(segments) => segments, )?;
Err(error) => { Some(Ok(format_php_version(&php_cmd_output.stdout)))
log::warn!("Error in module `php`:\n{}", error);
return None;
} }
}); _ => None,
})
.parse(None)
});
Some(module) module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `php`:\n{}", error);
return None;
} }
None => None, });
}
Some(module)
} }
fn format_php_version(php_version: &str) -> String { fn format_php_version(php_version: &str) -> String {
@@ -99,8 +97,8 @@ mod tests {
let actual = ModuleRenderer::new("php").path(dir.path()).collect(); let actual = ModuleRenderer::new("php").path(dir.path()).collect();
let expected = Some(format!( let expected = Some(format!(
"via {} ", "via {}",
Color::Fixed(147).bold().paint("🐘 v7.3.8") Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
)); ));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
@@ -114,8 +112,8 @@ mod tests {
let actual = ModuleRenderer::new("php").path(dir.path()).collect(); let actual = ModuleRenderer::new("php").path(dir.path()).collect();
let expected = Some(format!( let expected = Some(format!(
"via {} ", "via {}",
Color::Fixed(147).bold().paint("🐘 v7.3.8") Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
)); ));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()
@@ -129,8 +127,8 @@ mod tests {
let actual = ModuleRenderer::new("php").path(dir.path()).collect(); let actual = ModuleRenderer::new("php").path(dir.path()).collect();
let expected = Some(format!( let expected = Some(format!(
"via {} ", "via {}",
Color::Fixed(147).bold().paint("🐘 v7.3.8") Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
)); ));
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() dir.close()