summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMoritz Vetter <moritz.vetter@gmx.de>2021-01-20 18:42:55 +0100
committerGitHub <noreply@github.com>2021-01-20 18:42:55 +0100
commite437a463ac5c072e90e49b912ed15729650a52d8 (patch)
tree5f53afd9899ecf53461fb386753aad39f981ca36
parent8a80835f72670e02563c7c56e3812fec1037540c (diff)
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 <andytom@users.noreply.github.com> Co-authored-by: Moritz Vetter <mv@3yourmind.com> Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
-rw-r--r--docs/config/README.md2
-rw-r--r--src/configs/python.rs2
-rw-r--r--src/modules/python.rs65
3 files changed, 35 insertions, 34 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index da64c2ffc..e99da5fac 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 aec645daf..3c35a7ed1 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 0d18235a5..9ace4f5c4 100644
--- a/src/modules/python.rs
+++ b/src/modules/python.rs
@@ -40,19 +40,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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<Module<'a>> {
_ => 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<Module<'a>> {
Some(module)
}
-fn get_python_version(python_binary: &str) -> Option<String> {
- 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<String> {
+ 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);
}