1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2025-01-13 10:25:33 +00:00

perf(nodejs): evaluate nodejs format string lazily (#2160)

* fix(nodejs): update format string

* test(nodejs): adjust tests to new format strings

* fix(nodejs): use once_cell's Lazy to implement hassleless lazy execution

* chore(nodejs): run rustfmt

Co-authored-by: Moritz Vetter <mv@3yourmind.com>
This commit is contained in:
Moritz Vetter 2021-01-20 19:03:48 +01:00 committed by GitHub
parent 8302a3ccb4
commit b5fd517972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 15 deletions

View File

@ -1760,7 +1760,7 @@ The module will be shown if any of the following conditions are met:
| Option | Default | Description |
| ---------- | ---------------------------------- | -------------------------------------------------- |
| `format` | `"via [$symbol$version]($style) "` | The format for the module. |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"⬢ "` | A format string representing the symbol of NodeJS. |
| `style` | `"bold green"` | The style for the module. |
| `disabled` | `false` | Disables the `nodejs` module. |

View File

@ -14,7 +14,7 @@ pub struct NodejsConfig<'a> {
impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> {
fn new() -> Self {
NodejsConfig {
format: "via [$symbol$version]($style) ",
format: "via [$symbol($version )]($style)",
symbol: "",
style: "bold green",
disabled: false,

View File

@ -4,10 +4,12 @@ use crate::configs::nodejs::NodejsConfig;
use crate::formatter::StringFormatter;
use crate::utils;
use once_cell::sync::Lazy;
use regex::Regex;
use semver::Version;
use semver::VersionReq;
use serde_json as json;
use std::ops::Deref;
use std::path::Path;
/// Creates a module with the current Node.js version
@ -36,9 +38,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("nodejs");
let config = NodejsConfig::try_load(module.config);
let nodejs_version = utils::exec_cmd("node", &["--version"])?.stdout;
let engines_version = get_engines_version(&context.current_dir);
let in_engines_range = check_engines_version(&nodejs_version, engines_version);
let nodejs_version =
Lazy::new(|| utils::exec_cmd("node", &["--version"]).map(|cmd| cmd.stdout));
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
@ -47,6 +48,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
})
.map_style(|variable| match variable {
"style" => {
let engines_version = get_engines_version(&context.current_dir);
let in_engines_range =
check_engines_version(nodejs_version.deref().as_ref()?, engines_version);
if in_engines_range {
Some(Ok(config.style))
} else {
@ -56,7 +60,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None,
})
.map(|variable| match variable {
"version" => Some(Ok(nodejs_version.trim())),
"version" => nodejs_version
.deref()
.as_ref()
.map(|version| version.trim())
.map(Ok),
_ => None,
})
.parse(None)