From 2532251a13a5605b020a1c076a372e300fc5723a Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Wed, 20 Jan 2021 18:56:18 +0100 Subject: [PATCH] perf(dart): Lazy eval dart (#2166) * perf(dart): evaluate version lazily * fix(dart): update format string * fix: use suggested format string Co-authored-by: Moritz Vetter --- docs/config/README.md | 12 ++++++------ src/configs/dart.rs | 2 +- src/modules/dart.rs | 17 +++++++++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index b542e6c4..fdc89a69 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -604,12 +604,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` | `"🎯 "` | A format string representing the symbol of Dart | -| `style` | `"bold blue"` | The style for the module. | -| `disabled` | `false` | Disables the `dart` module. | +| Option | Default | Description | +| ---------- | ------------------------------------ | ----------------------------------------------- | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `symbol` | `"🎯 "` | A format string representing the symbol of Dart | +| `style` | `"bold blue"` | The style for the module. | +| `disabled` | `false` | Disables the `dart` module. | ### Variables diff --git a/src/configs/dart.rs b/src/configs/dart.rs index f0353218..6fafdc6b 100644 --- a/src/configs/dart.rs +++ b/src/configs/dart.rs @@ -13,7 +13,7 @@ pub struct DartConfig<'a> { impl<'a> RootModuleConfig<'a> for DartConfig<'a> { fn new() -> Self { DartConfig { - format: "via [$symbol$version]($style) ", + format: "via [$symbol($version )]($style)", symbol: "🎯 ", style: "bold blue", disabled: false, diff --git a/src/modules/dart.rs b/src/modules/dart.rs index a2269030..cd2b297f 100644 --- a/src/modules/dart.rs +++ b/src/modules/dart.rs @@ -22,8 +22,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let dart_version = utils::exec_cmd("dart", &["--version"])?.stderr; - let mut module = context.new_module("dart"); let config: DartConfig = DartConfig::try_load(module.config); @@ -38,7 +36,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => None, }) .map(|variable| match variable { - "version" => parse_dart_version(&dart_version).map(Ok), + "version" => { + let dart_version = utils::exec_cmd("dart", &["--version"])?.stderr; + parse_dart_version(&dart_version).map(Ok) + } _ => None, }) .parse(None) @@ -94,7 +95,7 @@ mod tests { File::create(dir.path().join("any.dart"))?.sync_all()?; let actual = ModuleRenderer::new("dart").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🎯 v2.8.4 "))); assert_eq!(expected, actual); dir.close() } @@ -105,7 +106,7 @@ mod tests { fs::create_dir_all(dir.path().join(".dart_tool"))?; let actual = ModuleRenderer::new("dart").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🎯 v2.8.4 "))); assert_eq!(expected, actual); dir.close() } @@ -116,7 +117,7 @@ mod tests { File::create(dir.path().join("pubspec.yaml"))?.sync_all()?; let actual = ModuleRenderer::new("dart").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🎯 v2.8.4 "))); assert_eq!(expected, actual); dir.close() } @@ -127,7 +128,7 @@ mod tests { File::create(dir.path().join("pubspec.yml"))?.sync_all()?; let actual = ModuleRenderer::new("dart").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🎯 v2.8.4 "))); assert_eq!(expected, actual); dir.close() } @@ -138,7 +139,7 @@ mod tests { File::create(dir.path().join("pubspec.lock"))?.sync_all()?; let actual = ModuleRenderer::new("dart").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🎯 v2.8.4"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🎯 v2.8.4 "))); assert_eq!(expected, actual); dir.close() }