From f640db87a428577eba256e7c320691b17220f362 Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Wed, 13 Jan 2021 18:35:44 +0100 Subject: [PATCH] perf(crystal): Lazily evaluate version command (#2129) * perf(crystal): Lazily evaluate version command This updates the module to lazily execute the `crystal --version` command only when the `version` variable is used in the format string. * perf(crystal): Update format string Update format string to handle the `$version` variable not being set. --- docs/config/README.md | 12 ++++++------ src/configs/crystal.rs | 2 +- src/modules/crystal.rs | 11 ++++++----- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 8d3bd1672..8b8bc228b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -567,12 +567,12 @@ The module will be shown if any of the following conditions are met: ### Options -| Option | Default | Description | -| ---------- | ---------------------------------- | --------------------------------------------------------- | -| `symbol` | `"🔮 "` | The symbol used before displaying the version of crystal. | -| `style` | `"bold red"` | The style for the module. | -| `format` | `"via [$symbol$version]($style) "` | The format for the module. | -| `disabled` | `false` | Disables the `crystal` module. | +| Option | Default | Description | +| ---------- | ------------------------------------ | --------------------------------------------------------- | +| `symbol` | `"🔮 "` | The symbol used before displaying the version of crystal. | +| `style` | `"bold red"` | The style for the module. | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `disabled` | `false` | Disables the `crystal` module. | ### Variables diff --git a/src/configs/crystal.rs b/src/configs/crystal.rs index 6d342253b..248f0ec4b 100644 --- a/src/configs/crystal.rs +++ b/src/configs/crystal.rs @@ -13,7 +13,7 @@ pub struct CrystalConfig<'a> { impl<'a> RootModuleConfig<'a> for CrystalConfig<'a> { fn new() -> Self { CrystalConfig { - format: "via [$symbol$version]($style) ", + format: "via [$symbol($version )]($style)", symbol: "🔮 ", style: "bold red", disabled: false, diff --git a/src/modules/crystal.rs b/src/modules/crystal.rs index 888cf83f7..33140b555 100644 --- a/src/modules/crystal.rs +++ b/src/modules/crystal.rs @@ -20,8 +20,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let crystal_version = utils::exec_cmd("crystal", &["--version"])?.stdout; - let mut module = context.new_module("crystal"); let config: CrystalConfig = CrystalConfig::try_load(module.config); @@ -36,7 +34,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => None, }) .map(|variable| match variable { - "version" => format_crystal_version(&crystal_version).map(Ok), + "version" => format_crystal_version( + utils::exec_cmd("crystal", &["--version"])?.stdout.as_str(), + ) + .map(Ok), _ => None, }) .parse(None) @@ -89,7 +90,7 @@ mod tests { File::create(dir.path().join("shard.yml"))?.sync_all()?; let actual = ModuleRenderer::new("crystal").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.bold().paint("🔮 v0.35.1"))); + let expected = Some(format!("via {}", Color::Red.bold().paint("🔮 v0.35.1 "))); assert_eq!(expected, actual); dir.close() @@ -101,7 +102,7 @@ mod tests { File::create(dir.path().join("main.cr"))?.sync_all()?; let actual = ModuleRenderer::new("crystal").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.bold().paint("🔮 v0.35.1"))); + let expected = Some(format!("via {}", Color::Red.bold().paint("🔮 v0.35.1 "))); assert_eq!(expected, actual); dir.close()