diff --git a/src/segments/python.go b/src/segments/python.go index ba33a2ca..49cace56 100644 --- a/src/segments/python.go +++ b/src/segments/python.go @@ -21,6 +21,7 @@ const ( // FetchVirtualEnv fetches the virtual env FetchVirtualEnv properties.Property = "fetch_virtual_env" UsePythonVersionFile properties.Property = "use_python_version_file" + FolderNameFallback properties.Property = "folder_name_fallback" ) func (p *Python) Template() string { @@ -74,6 +75,7 @@ func (p *Python) loadContext() { "CONDA_DEFAULT_ENV", } + folderNameFallback := p.language.props.GetBool(FolderNameFallback, true) defaultVenvNames := []string{ ".venv", "venv", @@ -87,8 +89,7 @@ func (p *Python) loadContext() { } name := platform.Base(p.language.env, venv) - - if slices.Contains(defaultVenvNames, name) { + if folderNameFallback && slices.Contains(defaultVenvNames, name) { venv = strings.TrimSuffix(venv, name) name = platform.Base(p.language.env, venv) } diff --git a/src/segments/python_test.go b/src/segments/python_test.go index c5794d0e..63754473 100644 --- a/src/segments/python_test.go +++ b/src/segments/python_test.go @@ -150,14 +150,36 @@ func TestPythonPythonInContext(t *testing.T) { func TestPythonVirtualEnvIgnoreDefaultVenvNames(t *testing.T) { cases := []struct { - VirtualEnvName string + Expected string + FolderNameFallback bool + VirtualEnvName string }{ - {VirtualEnvName: "/path/to/folder/.venv"}, - {VirtualEnvName: "/path/to/folder/venv"}, + { + Expected: "folder", + FolderNameFallback: true, + VirtualEnvName: "/path/to/folder/.venv", + }, + { + Expected: "folder", + FolderNameFallback: true, + VirtualEnvName: "/path/to/folder/venv", + }, + { + Expected: ".venv", + FolderNameFallback: false, + VirtualEnvName: "/path/to/folder/.venv", + }, + { + Expected: "venv", + FolderNameFallback: false, + VirtualEnvName: "/path/to/folder/venv", + }, } for _, tc := range cases { - env := new(mock.MockedEnvironment) + params := &mockedLanguageParams{} + env, props := getMockedLanguageEnv(params) + env.On("GOOS").Return("") env.On("PathSeparator").Return("/") env.On("CommandPath", mock2.Anything).Return("") @@ -167,9 +189,12 @@ func TestPythonVirtualEnvIgnoreDefaultVenvNames(t *testing.T) { env.On("Getenv", "CONDA_DEFAULT_ENV").Return("") env.On("Getenv", "PYENV_VERSION").Return("") env.On("HasParentFilePath", ".python-version").Return(&platform.FileInfo{}, errors.New("no match at root level")) + + props[FolderNameFallback] = tc.FolderNameFallback + python := &Python{} - python.Init(properties.Map{}, env) + python.Init(props, env) python.loadContext() - assert.Equal(t, "folder", python.Venv) + assert.Equal(t, tc.Expected, python.Venv) } } diff --git a/website/docs/segments/python.mdx b/website/docs/segments/python.mdx index 6179b81f..3e5a706f 100644 --- a/website/docs/segments/python.mdx +++ b/website/docs/segments/python.mdx @@ -9,8 +9,6 @@ sidebar_label: Python Display the currently active python version and virtualenv. Supports conda, virtualenv and pyenv (if python points to pyenv shim). -If your virtual environment is a directory named `venv` or `.venv`, the virtual environment's name will be the parent directory. - ## Sample Configuration import Config from "@site/src/components/Config.js"; @@ -38,8 +36,9 @@ import Config from "@site/src/components/Config.js"; | `display_mode` | `string` | | | `version_url_template` | `string` | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes | | `extensions` | `[]string` | allows to override the default list of file extensions to validate | -| `folders` | `[]string` | allows to override the list of folder names to validate | +| `folders` | `[]string` | allows to override the list of folder names to validate | | `cache_version` | `boolean` | cache the executable's version or not - defaults to `false` | +| `folder_name_fallback` | `boolean` | instead of `venv` or `.venv` (case sensitive), use the parent folder name as the virtual environment's name or not - defaults to `true` | ## Template ([info][templates])