1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-26 03:27:30 +00:00

feat(python): Configure when the module is shown (#2264)

This makes it possible to configure when the python module is shown
based on the contents of a directory. This should make it possible to
be a lot more granular when configuring the module.

This includes a breaking change since we are removing the
`scan_for_pyfiles` configuration option in favour of setting the
`detect_extensions` to an empty array.
This commit is contained in:
Thomas O'Donnell 2021-02-07 22:18:52 +01:00 committed by GitHub
parent f9bebc9ab8
commit da4bd401c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 49 deletions

View File

@ -2040,29 +2040,31 @@ current Python virtual environment if one is activated.
If `pyenv_version_name` is set to `true`, it will display the pyenv version
name. Otherwise, it will display the version number from `python --version`.
The module will be shown if any of the following conditions are met:
By default the module will be shown if any of the following conditions are met:
- The current directory contains a `.python-version` file
- The current directory contains a `requirements.txt` file
- The current directory contains a `pyproject.toml` file
- The current directory contains a file with the `.py` extension (and `scan_for_pyfiles` is true)
- The current directory contains a `Pipfile` file
- The current directory contains a `tox.ini` file
- The current directory contains a `setup.py` file
- The current directory contains a `__init__.py` file
- The current directory contains a `pyproject.toml` file
- The current directory contains a `requirements.txt` file
- The current directory contains a `setup.py` file
- The current directory contains a `tox.ini` file
- The current directory contains a file with the `.py` extension.
- A virtual environment is currently activated
### Options
| Option | Default | Description |
| -------------------- | ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| -------------------- | ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- |
| `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 |
| `pyenv_prefix` | `pyenv ` | Prefix before pyenv version display, only used if pyenv is used |
| `scan_for_pyfiles` | `true` | If false, Python files in the current directory will not show this module. |
| `python_binary` | `["python", "python3, "python2"]` | Configures the python binaries that Starship should executes when getting the version. |
| `detect_extensions` | `[".py"]` | Which extensions should trigger this moudle |
| `detect_files` | `[".python-version", "Pipfile", "__init__.py", "pyproject.toml", "requirements.txt", "setup.py", "tox.ini"]` | Which filenames should trigger this module |
| `detect_folders` | `[]` | Which folders should trigger this module |
| `disabled` | `false` | Disables the `python` module. |
::: tip
@ -2112,6 +2114,14 @@ pyenv_version_name = true
python_binary = "python3"
```
```toml
# ~/.config/starship.toml
[python]
# Don't trigger for files with the py extension
detect_extensions = []
```
## Ruby
The `ruby` module shows the currently installed version of Ruby.

View File

@ -7,11 +7,13 @@ pub struct PythonConfig<'a> {
pub pyenv_version_name: bool,
pub pyenv_prefix: &'a str,
pub python_binary: VecOr<&'a str>,
pub scan_for_pyfiles: bool,
pub format: &'a str,
pub style: &'a str,
pub symbol: &'a str,
pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}
impl<'a> RootModuleConfig<'a> for PythonConfig<'a> {
@ -20,11 +22,21 @@ impl<'a> RootModuleConfig<'a> for PythonConfig<'a> {
pyenv_version_name: false,
pyenv_prefix: "pyenv ",
python_binary: VecOr(vec!["python", "python3", "python2"]),
scan_for_pyfiles: true,
format: "via [${symbol}${pyenv_prefix}(${version} )(\\($virtualenv\\))]($style)",
style: "yellow bold",
symbol: "🐍 ",
disabled: false,
detect_extensions: vec!["py"],
detect_files: vec![
"requirements.txt",
".python-version",
"pyproject.toml",
"Pipfile",
"tox.ini",
"setup.py",
"__init__.py",
],
detect_folders: vec![],
}
}
}

View File

@ -6,35 +6,17 @@ use crate::configs::python::PythonConfig;
use crate::formatter::StringFormatter;
use crate::utils;
/// Creates a module with the current Python version
///
/// Will display the Python version if any of the following criteria are met:
/// - Current directory contains a `.python-version` file
/// - Current directory contains a `requirements.txt` file
/// - Current directory contains a `pyproject.toml` file
/// - Current directory contains a file with the `.py` extension
/// - Current directory contains a `Pipfile` file
/// - Current directory contains a `tox.ini` file
/// Creates a module with the current Python version and, if active, virtual environment.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("python");
let config: PythonConfig = PythonConfig::try_load(module.config);
let is_py_project = {
let base = context.try_begin_scan()?.set_files(&[
"requirements.txt",
".python-version",
"pyproject.toml",
"Pipfile",
"tox.ini",
"setup.py",
"__init__.py",
]);
if config.scan_for_pyfiles {
base.set_extensions(&["py"]).is_match()
} else {
base.is_match()
}
};
let is_py_project = context
.try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();
let is_venv = context.get_env("VIRTUAL_ENV").is_some();
@ -265,7 +247,7 @@ mod tests {
let expected = None;
let config = toml::toml! {
[python]
scan_for_pyfiles = false
detect_extensions = []
};
let actual = ModuleRenderer::new("python")
.path(dir.path())
@ -283,7 +265,7 @@ mod tests {
let config = toml::toml! {
[python]
python_binary = "python2"
scan_for_pyfiles = false
detect_extensions = []
};
check_python2_renders(&dir, Some(config));
@ -291,7 +273,7 @@ mod tests {
let config_python3 = toml::toml! {
[python]
python_binary = "python3"
scan_for_pyfiles = false
detect_extensions = []
};
check_python3_renders(&dir, Some(config_python3));
@ -300,14 +282,14 @@ mod tests {
[python]
pyenv_version_name = true
pyenv_prefix = "test_pyenv "
scan_for_pyfiles = false
detect_extensions = []
};
check_pyenv_renders(&dir, Some(config_pyenv));
let config_multi = toml::toml! {
[python]
python_binary = ["python", "python3"]
scan_for_pyfiles = false
detect_extensions = []
};
check_multiple_binaries_renders(&dir, Some(config_multi));