From 98b89b94321f3d764da57a51fd2570a011bb4a86 Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Thu, 21 Jan 2021 23:00:12 +0100 Subject: [PATCH] perf(lua): Lazy eval lua (#2185) * perf(lua): evaluate version lazily * fix(lua): update format string * test(lua): update tests Co-authored-by: Moritz Vetter --- docs/config/README.md | 14 +++++++------- src/configs/lua.rs | 2 +- src/modules/lua.rs | 14 ++++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index db507b82..4eb8f3cb 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1555,13 +1555,13 @@ 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 Lua. | -| `style` | `"bold blue"` | The style for the module. | -| `lua_binary` | `"lua"` | Configures the lua binary that Starship executes when getting the version. | -| `disabled` | `false` | Disables the `lua` module. | +| Option | Default | Description | +| ------------ | ------------------------------------ | ----------------------------------------------------------------------------- | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `symbol` | `"🌙 "` | A format string representing the symbol of Lua. | +| `style` | `"bold blue"` | The style for the module. | +| `lua_binary` | `"lua"` | Configures the lua binary that Starship executes when getting the version. | +| `disabled` | `false` | Disables the `lua` module. | ### Variables diff --git a/src/configs/lua.rs b/src/configs/lua.rs index fb715f47..6ba8593f 100644 --- a/src/configs/lua.rs +++ b/src/configs/lua.rs @@ -14,7 +14,7 @@ pub struct LuaConfig<'a> { impl<'a> RootModuleConfig<'a> for LuaConfig<'a> { fn new() -> Self { LuaConfig { - format: "via [$symbol$version]($style) ", + format: "via [$symbol($version )]($style)", symbol: "🌙 ", style: "bold blue", lua_binary: "lua", diff --git a/src/modules/lua.rs b/src/modules/lua.rs index 9bea9266..3b30eb1c 100644 --- a/src/modules/lua.rs +++ b/src/modules/lua.rs @@ -27,7 +27,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { let mut module = context.new_module("lua"); let config = LuaConfig::try_load(module.config); - let lua_version = format_lua_version(&get_lua_version(&config.lua_binary)?)?; let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|var, _| match var { @@ -39,7 +38,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => None, }) .map(|variable| match variable { - "version" => Some(Ok(&lua_version)), + "version" => { + let lua_version = format_lua_version(&get_lua_version(&config.lua_binary)?)?; + Some(Ok(lua_version)) + } _ => None, }) .parse(None) @@ -103,7 +105,7 @@ mod tests { let dir = tempfile::tempdir()?; File::create(dir.path().join("main.lua"))?.sync_all()?; let actual = ModuleRenderer::new("lua").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🌙 v5.4.0"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🌙 v5.4.0 "))); assert_eq!(expected, actual); dir.close() } @@ -114,7 +116,7 @@ mod tests { File::create(dir.path().join(".lua-version"))?.sync_all()?; let actual = ModuleRenderer::new("lua").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🌙 v5.4.0"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🌙 v5.4.0 "))); assert_eq!(expected, actual); dir.close() } @@ -126,7 +128,7 @@ mod tests { fs::create_dir_all(&lua_dir)?; let actual = ModuleRenderer::new("lua").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🌙 v5.4.0"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🌙 v5.4.0 "))); assert_eq!(expected, actual); dir.close() } @@ -146,7 +148,7 @@ mod tests { .config(config) .collect(); - let expected = Some(format!("via {} ", Color::Blue.bold().paint("🌙 v2.0.5"))); + let expected = Some(format!("via {}", Color::Blue.bold().paint("🌙 v2.0.5 "))); assert_eq!(expected, actual); dir.close() }