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>
This commit is contained in:
Moritz Vetter 2021-01-20 18:42:55 +01:00 committed by GitHub
parent 8a80835f72
commit e437a463ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 34 deletions

View File

@ -2050,7 +2050,7 @@ The module will be shown if any of the following conditions are met:
| Option | Default | Description | | 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 | | `symbol` | `"🐍 "` | A format string representing the symbol of Python |
| `style` | `"yellow bold"` | The style for the module. | | `style` | `"yellow bold"` | The style for the module. |
| `pyenv_version_name` | `false` | Use pyenv to get Python version | | `pyenv_version_name` | `false` | Use pyenv to get Python version |

View File

@ -21,7 +21,7 @@ impl<'a> RootModuleConfig<'a> for PythonConfig<'a> {
pyenv_prefix: "pyenv ", pyenv_prefix: "pyenv ",
python_binary: VecOr(vec!["python", "python3", "python2"]), python_binary: VecOr(vec!["python", "python3", "python2"]),
scan_for_pyfiles: true, scan_for_pyfiles: true,
format: "via [${symbol}${pyenv_prefix}${version}( \\($virtualenv\\))]($style) ", format: "via [${symbol}${pyenv_prefix}(${version} )(\\($virtualenv\\))]($style)",
style: "yellow bold", style: "yellow bold",
symbol: "🐍 ", symbol: "🐍 ",
disabled: false, disabled: false,

View File

@ -40,19 +40,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
if !is_py_project && !is_venv { if !is_py_project && !is_venv {
return None; 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 { let pyenv_prefix = if config.pyenv_version_name {
config.pyenv_prefix config.pyenv_prefix
} else { } else {
@ -70,9 +59,15 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None, _ => None,
}) })
.map(|variable| match variable { .map(|variable| match variable {
"version" => Some(Ok(python_version.trim())), "version" => {
"virtualenv" => virtual_env.as_ref().map(|e| Ok(e.trim())), let version = get_python_version(&config)?;
"pyenv_prefix" => Some(Ok(pyenv_prefix)), 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, _ => None,
}) })
.parse(None) .parse(None)
@ -89,17 +84,23 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) Some(module)
} }
fn get_python_version(python_binary: &str) -> Option<String> { fn get_python_version(config: &PythonConfig) -> Option<String> {
match utils::exec_cmd(python_binary, &["--version"]) { if config.pyenv_version_name {
Some(output) => { return Some(utils::exec_cmd("pyenv", &["version-name"])?.stdout);
if output.stdout.is_empty() { };
Some(output.stderr) let version = config.python_binary.0.iter().find_map(|binary| {
} else { match utils::exec_cmd(binary, &["--version"]) {
Some(output.stdout) 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 { fn format_python_version(python_stdout: &str) -> String {
@ -323,7 +324,7 @@ mod tests {
.collect(); .collect();
let expected = Some(format!( let expected = Some(format!(
"via {} ", "via {}",
Color::Yellow.bold().paint("🐍 v3.8.0 (my_venv)") Color::Yellow.bold().paint("🐍 v3.8.0 (my_venv)")
)); ));
@ -341,7 +342,7 @@ mod tests {
.collect(); .collect();
let expected = Some(format!( let expected = Some(format!(
"via {} ", "via {}",
Color::Yellow.bold().paint("🐍 v3.8.0 (my_venv)") Color::Yellow.bold().paint("🐍 v3.8.0 (my_venv)")
)); ));
@ -368,7 +369,7 @@ prompt = 'foo'
.collect(); .collect();
let expected = Some(format!( let expected = Some(format!(
"via {} ", "via {}",
Color::Yellow.bold().paint("🐍 v3.8.0 (foo)") Color::Yellow.bold().paint("🐍 v3.8.0 (foo)")
)); ));
@ -387,7 +388,7 @@ prompt = 'foo'
.config(config) .config(config)
.collect(); .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); assert_eq!(expected, actual);
} }
@ -402,7 +403,7 @@ prompt = 'foo'
.config(config) .config(config)
.collect(); .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); assert_eq!(expected, actual);
} }
@ -420,7 +421,7 @@ prompt = 'foo'
.config(config) .config(config)
.collect(); .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); assert_eq!(expected, actual);
} }
@ -437,8 +438,8 @@ prompt = 'foo'
.collect(); .collect();
let expected = Some(format!( let expected = Some(format!(
"via {} ", "via {}",
Color::Yellow.bold().paint("🐍 test_pyenv system") Color::Yellow.bold().paint("🐍 test_pyenv system ")
)); ));
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }