From e437a463ac5c072e90e49b912ed15729650a52d8 Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Wed, 20 Jan 2021 18:42:55 +0100 Subject: [PATCH] perf(python): Lazy eval of python version command (#2158) * perf(python): evaluate version lazily * fix(python): update format string * fix: use suggested format strings Co-authored-by: Thomas O'Donnell Co-authored-by: Moritz Vetter Co-authored-by: Thomas O'Donnell --- docs/config/README.md | 2 +- src/configs/python.rs | 2 +- src/modules/python.rs | 65 ++++++++++++++++++++++--------------------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index da64c2ff..e99da5fa 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2050,7 +2050,7 @@ The module will be shown if any of the following conditions are met: | Option | Default | Description | | -------------------- | ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | -| `format` | `'via [${symbol}${pyenv_prefix}${version}( \($virtualenv\))]($style) '` | The format for the module. | +| `format` | `'via [${symbol}${pyenv_prefix}(${version} )(\($virtualenv\))]($style)'` | The format for the module. | | `symbol` | `"🐍 "` | A format string representing the symbol of Python | | `style` | `"yellow bold"` | The style for the module. | | `pyenv_version_name` | `false` | Use pyenv to get Python version | diff --git a/src/configs/python.rs b/src/configs/python.rs index aec645da..3c35a7ed 100644 --- a/src/configs/python.rs +++ b/src/configs/python.rs @@ -21,7 +21,7 @@ impl<'a> RootModuleConfig<'a> for PythonConfig<'a> { pyenv_prefix: "pyenv ", python_binary: VecOr(vec!["python", "python3", "python2"]), scan_for_pyfiles: true, - format: "via [${symbol}${pyenv_prefix}${version}( \\($virtualenv\\))]($style) ", + format: "via [${symbol}${pyenv_prefix}(${version} )(\\($virtualenv\\))]($style)", style: "yellow bold", symbol: "🐍 ", disabled: false, diff --git a/src/modules/python.rs b/src/modules/python.rs index 0d18235a..9ace4f5c 100644 --- a/src/modules/python.rs +++ b/src/modules/python.rs @@ -40,19 +40,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { if !is_py_project && !is_venv { return None; - } - - let python_version = if config.pyenv_version_name { - utils::exec_cmd("pyenv", &["version-name"])?.stdout - } else { - let version = config - .python_binary - .0 - .iter() - .find_map(|binary| get_python_version(binary))?; - format_python_version(&version) }; - let virtual_env = get_python_virtual_env(context); + let pyenv_prefix = if config.pyenv_version_name { config.pyenv_prefix } else { @@ -70,9 +59,15 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => None, }) .map(|variable| match variable { - "version" => Some(Ok(python_version.trim())), - "virtualenv" => virtual_env.as_ref().map(|e| Ok(e.trim())), - "pyenv_prefix" => Some(Ok(pyenv_prefix)), + "version" => { + let version = get_python_version(&config)?; + Some(Ok(version.trim().to_string())) + } + "virtualenv" => { + let virtual_env = get_python_virtual_env(context); + virtual_env.as_ref().map(|e| Ok(e.trim().to_string())) + } + "pyenv_prefix" => Some(Ok(pyenv_prefix.to_string())), _ => None, }) .parse(None) @@ -89,17 +84,23 @@ pub fn module<'a>(context: &'a Context) -> Option> { Some(module) } -fn get_python_version(python_binary: &str) -> Option { - match utils::exec_cmd(python_binary, &["--version"]) { - Some(output) => { - if output.stdout.is_empty() { - Some(output.stderr) - } else { - Some(output.stdout) +fn get_python_version(config: &PythonConfig) -> Option { + if config.pyenv_version_name { + return Some(utils::exec_cmd("pyenv", &["version-name"])?.stdout); + }; + let version = config.python_binary.0.iter().find_map(|binary| { + match utils::exec_cmd(binary, &["--version"]) { + Some(output) => { + if output.stdout.is_empty() { + Some(output.stderr) + } else { + Some(output.stdout) + } } + None => None, } - None => None, - } + })?; + Some(format_python_version(&version)) } fn format_python_version(python_stdout: &str) -> String { @@ -323,7 +324,7 @@ mod tests { .collect(); let expected = Some(format!( - "via {} ", + "via {}", Color::Yellow.bold().paint("🐍 v3.8.0 (my_venv)") )); @@ -341,7 +342,7 @@ mod tests { .collect(); let expected = Some(format!( - "via {} ", + "via {}", Color::Yellow.bold().paint("🐍 v3.8.0 (my_venv)") )); @@ -368,7 +369,7 @@ prompt = 'foo' .collect(); let expected = Some(format!( - "via {} ", + "via {}", Color::Yellow.bold().paint("🐍 v3.8.0 (foo)") )); @@ -387,7 +388,7 @@ prompt = 'foo' .config(config) .collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐍 v2.7.17"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐍 v2.7.17 "))); assert_eq!(expected, actual); } @@ -402,7 +403,7 @@ prompt = 'foo' .config(config) .collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐍 v3.8.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐍 v3.8.0 "))); assert_eq!(expected, actual); } @@ -420,7 +421,7 @@ prompt = 'foo' .config(config) .collect(); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐍 v3.8.0"))); + let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐍 v3.8.0 "))); assert_eq!(expected, actual); } @@ -437,8 +438,8 @@ prompt = 'foo' .collect(); let expected = Some(format!( - "via {} ", - Color::Yellow.bold().paint("🐍 test_pyenv system") + "via {}", + Color::Yellow.bold().paint("🐍 test_pyenv system ") )); assert_eq!(expected, actual); }