From 83e0432a75104b74e3e8834db638f8627151e32d Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Wed, 20 Jan 2021 18:51:30 +0100 Subject: [PATCH] perf(java): Lazy eval java (#2168) * perf(java): evaluate version lazily * fix(java): update format string * fix: use suggested format string Co-authored-by: Moritz Vetter --- docs/config/README.md | 12 ++++++------ src/configs/java.rs | 2 +- src/modules/java.rs | 23 ++++++++++++----------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 44a9cf22..b542e6c4 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1334,12 +1334,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 Java | -| `style` | `"red dimmed"` | The style for the module. | -| `disabled` | `false` | Disables the `java` module. | +| Option | Default | Description | +| ---------- | ---------------------------------------- | ----------------------------------------------- | +| `format` | `"via [${symbol}(${version} )]($style)"` | The format for the module. | +| `symbol` | `"☕ "` | A format string representing the symbol of Java | +| `style` | `"red dimmed"` | The style for the module. | +| `disabled` | `false` | Disables the `java` module. | ### Variables diff --git a/src/configs/java.rs b/src/configs/java.rs index 99ecc281..84c178f9 100644 --- a/src/configs/java.rs +++ b/src/configs/java.rs @@ -13,7 +13,7 @@ pub struct JavaConfig<'a> { impl<'a> RootModuleConfig<'a> for JavaConfig<'a> { fn new() -> Self { JavaConfig { - format: "via [$symbol$version]($style) ", + format: "via [$symbol($version )]($style)", disabled: false, style: "red dimmed", symbol: "☕ ", diff --git a/src/modules/java.rs b/src/modules/java.rs index 2731bf6d..dffec6e9 100644 --- a/src/modules/java.rs +++ b/src/modules/java.rs @@ -32,8 +32,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let java_version = get_java_version(context)?; - let mut module = context.new_module("java"); let config: JavaConfig = JavaConfig::try_load(module.config); @@ -48,7 +46,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => None, }) .map(|variable| match variable { - "version" => Some(Ok(&java_version)), + "version" => { + let java_version = get_java_version(context)?; + Some(Ok(java_version)) + } _ => None, }) .parse(None) @@ -175,7 +176,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join("Main.java"))?.sync_all()?; let actual = ModuleRenderer::new("java").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.dimmed().paint("☕ v13.0.2"))); + let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v13.0.2 "))); assert_eq!(expected, actual); dir.close() } @@ -185,7 +186,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join("Main.class"))?.sync_all()?; let actual = ModuleRenderer::new("java").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.dimmed().paint("☕ v13.0.2"))); + let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v13.0.2 "))); assert_eq!(expected, actual); dir.close() } @@ -195,7 +196,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join("build.gradle"))?.sync_all()?; let actual = ModuleRenderer::new("java").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.dimmed().paint("☕ v13.0.2"))); + let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v13.0.2 "))); assert_eq!(expected, actual); dir.close() } @@ -205,7 +206,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join("test.jar"))?.sync_all()?; let actual = ModuleRenderer::new("java").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.dimmed().paint("☕ v13.0.2"))); + let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v13.0.2 "))); assert_eq!(expected, actual); dir.close() } @@ -215,7 +216,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join("pom.xml"))?.sync_all()?; let actual = ModuleRenderer::new("java").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.dimmed().paint("☕ v13.0.2"))); + let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v13.0.2 "))); assert_eq!(expected, actual); dir.close() } @@ -225,7 +226,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join("build.gradle.kts"))?.sync_all()?; let actual = ModuleRenderer::new("java").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.dimmed().paint("☕ v13.0.2"))); + let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v13.0.2 "))); assert_eq!(expected, actual); dir.close() } @@ -235,7 +236,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join("build.gradle.kts"))?.sync_all()?; let actual = ModuleRenderer::new("java").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.dimmed().paint("☕ v13.0.2"))); + let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v13.0.2 "))); assert_eq!(expected, actual); dir.close() } @@ -245,7 +246,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join(".java-version"))?.sync_all()?; let actual = ModuleRenderer::new("java").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.dimmed().paint("☕ v13.0.2"))); + let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v13.0.2 "))); assert_eq!(expected, actual); dir.close() }