diff options
author | Moritz Vetter <moritz.vetter@gmx.de> | 2021-01-22 18:08:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-22 18:08:04 +0100 |
commit | 212dbc4e6b913fa4752f3f2702c1b2b399119298 (patch) | |
tree | 8230b054278ac9444b7446c26f901705da521f7b | |
parent | 16b8cbfdb88d2847f206a68f9d8bbd9a75f174c6 (diff) |
perf(ocaml): evaluate version lazily; adjust format string; update test (#2188)
Co-authored-by: Moritz Vetter <mv@3yourmind.com>
-rw-r--r-- | docs/config/README.md | 12 | ||||
-rw-r--r-- | src/configs/ocaml.rs | 2 | ||||
-rw-r--r-- | src/modules/ocaml.rs | 49 |
3 files changed, 32 insertions, 31 deletions
diff --git a/docs/config/README.md b/docs/config/README.md index 72b48ab46..b67b21df8 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1799,12 +1799,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 string for the module. | -| `symbol` | `"🐫 "` | The symbol used before displaying the version of OCaml. | -| `style` | `"bold yellow"` | The style for the module. | -| `disabled` | `false` | Disables the `ocaml` module. | +| Option | Default | Description | +| ---------- | ------------------------------------ | ------------------------------------------------------- | +| `format` | `"via [$symbol($version )]($style)"` | The format string for the module. | +| `symbol` | `"🐫 "` | The symbol used before displaying the version of OCaml. | +| `style` | `"bold yellow"` | The style for the module. | +| `disabled` | `false` | Disables the `ocaml` module. | ### Variables diff --git a/src/configs/ocaml.rs b/src/configs/ocaml.rs index 5b970430d..f407c6b9d 100644 --- a/src/configs/ocaml.rs +++ b/src/configs/ocaml.rs @@ -13,7 +13,7 @@ pub struct OCamlConfig<'a> { impl<'a> RootModuleConfig<'a> for OCamlConfig<'a> { fn new() -> Self { OCamlConfig { - format: "via [$symbol$version]($style) ", + format: "via [$symbol($version )]($style)", symbol: "🐫 ", style: "bold yellow", disabled: false, diff --git a/src/modules/ocaml.rs b/src/modules/ocaml.rs index 523f8bd55..8bc7d91a4 100644 --- a/src/modules/ocaml.rs +++ b/src/modules/ocaml.rs @@ -25,17 +25,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { return None; } - let is_esy_project = context - .try_begin_scan()? - .set_folders(&["esy.lock"]) - .is_match(); - - let ocaml_version = if is_esy_project { - utils::exec_cmd("esy", &["ocaml", "-vnum"])?.stdout - } else { - utils::exec_cmd("ocaml", &["-vnum"])?.stdout - }; - let mut module = context.new_module("ocaml"); let config: OCamlConfig = OCamlConfig::try_load(module.config); @@ -50,7 +39,19 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { _ => None, }) .map(|variable| match variable { - "version" => Some(Ok(format!("v{}", &ocaml_version.trim()))), + "version" => { + let is_esy_project = context + .try_begin_scan()? + .set_folders(&["esy.lock"]) + .is_match(); + + let ocaml_version = if is_esy_project { + utils::exec_cmd("esy", &["ocaml", "-vnum"])?.stdout + } else { + utils::exec_cmd("ocaml", &["-vnum"])?.stdout + }; + Some(Ok(format!("v{}", &ocaml_version.trim()))) + } _ => None, }) .parse(None) @@ -89,7 +90,7 @@ mod tests { File::create(dir.path().join("any.opam"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -100,7 +101,7 @@ mod tests { fs::create_dir_all(dir.path().join("_opam"))?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -115,7 +116,7 @@ mod tests { "{\"dependencies\": {\"ocaml\": \"4.8.1000\"}}", )?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.08.1"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.08.1 "))); assert_eq!(expected, actual); dir.close() } @@ -126,7 +127,7 @@ mod tests { File::create(dir.path().join("dune"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -137,7 +138,7 @@ mod tests { File::create(dir.path().join("dune-project"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -148,7 +149,7 @@ mod tests { File::create(dir.path().join("jbuild"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -159,7 +160,7 @@ mod tests { File::create(dir.path().join("jbuild-ignore"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -170,7 +171,7 @@ mod tests { File::create(dir.path().join(".merlin"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -181,7 +182,7 @@ mod tests { File::create(dir.path().join("any.ml"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -192,7 +193,7 @@ mod tests { File::create(dir.path().join("any.mli"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -203,7 +204,7 @@ mod tests { File::create(dir.path().join("any.re"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } @@ -214,7 +215,7 @@ mod tests { File::create(dir.path().join("any.rei"))?.sync_all()?; let actual = ModuleRenderer::new("ocaml").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐫 v4.10.0 "))); assert_eq!(expected, actual); dir.close() } |