summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMoritz Vetter <moritz.vetter@gmx.de>2021-01-22 18:03:18 +0100
committerGitHub <noreply@github.com>2021-01-22 18:03:18 +0100
commitd10a83ce6b5a2c08d976307134094d3ab743681e (patch)
tree038e0a4906577576a7643b756bf6e4da1625337f
parent60be1e9540e5739d45bd06b0de8abbc51ded8661 (diff)
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>
-rw-r--r--docs/config/README.md12
-rw-r--r--src/configs/php.rs2
-rw-r--r--src/modules/php.rs86
3 files changed, 49 insertions, 51 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index 030ad77df..599d0827b 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -1964,12 +1964,12 @@ The module will be shown if any of the following conditions are met:
### 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. |
+| `style` | `"147 bold"` | The style for the module. |
+| `disabled` | `false` | Disables the `php` module. |
### Variables
diff --git a/src/configs/php.rs b/src/configs/php.rs
index f17c76fe2..7f2e5cf4b 100644
--- a/src/configs/php.rs
+++ b/src/configs/php.rs
@@ -15,7 +15,7 @@ impl<'a> RootModuleConfig<'a> for PhpConfig<'a> {
PhpConfig {
symbol: "🐘 ",
style: "147 bold",
- format: "via [$symbol$version]($style) ",
+ format: "via [$symbol($version )]($style)",
disabled: false,
}
}
diff --git a/src/modules/php.rs b/src/modules/php.rs
index e7c54e7c6..62d994143 100644
--- a/src/modules/php.rs
+++ b/src/modules/php.rs
@@ -20,46 +20,44 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None;
}
- match utils::exec_cmd(
- "php",
- &[
- "-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| {
- formatter
- .map_meta(|variable, _| match variable {
- "symbol" => Some(config.symbol),
- _ => None,
- })
- .map_style(|variable| match variable {
- "style" => Some(Ok(config.style)),
- _ => None,
- })
- .map(|variable| match variable {
- "version" => Some(Ok(format_php_version(&php_cmd_output.stdout))),
- _ => None,
- })
- .parse(None)
- });
-
- module.set_segments(match parsed {
- Ok(segments) => segments,
- Err(error) => {
- log::warn!("Error in module `php`:\n{}", error);
- 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 {
+ "symbol" => Some(config.symbol),
+ _ => None,
+ })
+ .map_style(|variable| match variable {
+ "style" => Some(Ok(config.style)),
+ _ => None,
+ })
+ .map(|variable| match variable {
+ "version" => {
+ let php_cmd_output = utils::exec_cmd(
+ "php",
+ &[
+ "-nr",
+ "echo PHP_MAJOR_VERSION.\".\".PHP_MINOR_VERSION.\".\".PHP_RELEASE_VERSION;",
+ ],
+ )?;
+ Some(Ok(format_php_version(&php_cmd_output.stdout)))
}
- });
-
- Some(module)
+ _ => None,
+ })
+ .parse(None)
+ });
+
+ 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 {
@@ -99,8 +97,8 @@ mod tests {
let actual = ModuleRenderer::new("php").path(dir.path()).collect();
let expected = Some(format!(
- "via {} ",
- Color::Fixed(147).bold().paint("🐘 v7.3.8")
+ "via {}",
+ Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
));
assert_eq!(expected, actual);
dir.close()
@@ -114,8 +112,8 @@ mod tests {
let actual = ModuleRenderer::new("php").path(dir.path()).collect();
let expected = Some(format!(
- "via {} ",
- Color::Fixed(147).bold().paint("🐘 v7.3.8")
+ "via {}",
+ Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
));
assert_eq!(expected, actual);
dir.close()
@@ -129,8 +127,8 @@ mod tests {
let actual = ModuleRenderer::new("php").path(dir.path()).collect();
let expected = Some(format!(
- "via {} ",
- Color::Fixed(147).bold().paint("🐘 v7.3.8")
+ "via {}",
+ Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
));
assert_eq!(expected, actual);
dir.close()