oh-my-posh/src/segment_python.go

92 lines
2 KiB
Go
Raw Normal View History

package main
import (
"oh-my-posh/environment"
"oh-my-posh/properties"
)
type python struct {
2021-12-03 11:19:57 -08:00
language
Venv string
}
const (
2021-12-04 02:56:55 -08:00
// FetchVirtualEnv fetches the virtual env
FetchVirtualEnv properties.Property = "fetch_virtual_env"
)
func (p *python) template() string {
return languageTemplate
}
func (p *python) init(props properties.Properties, env environment.Environment) {
2021-12-03 11:19:57 -08:00
p.language = language{
2021-02-03 10:11:32 -08:00
env: env,
props: props,
extensions: []string{"*.py", "*.ipynb", "pyproject.toml", "venv.bak", "venv", ".venv"},
loadContext: p.loadContext,
inContext: p.inContext,
commands: []*cmd{
{
executable: "python",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
{
executable: "python3",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
2021-02-03 10:11:32 -08:00
versionURLTemplate: "[%s](https://www.python.org/downloads/release/python-%s%s%s/)",
2022-01-26 04:09:21 -08:00
displayMode: props.GetString(DisplayMode, DisplayModeEnvironment),
homeEnabled: props.GetBool(HomeEnabled, true),
2020-11-14 11:04:04 -08:00
}
}
func (p *python) enabled() bool {
return p.language.enabled()
}
func (p *python) loadContext() {
2022-01-26 04:09:21 -08:00
if !p.language.props.GetBool(FetchVirtualEnv, true) {
return
}
venvVars := []string{
"VIRTUAL_ENV",
"CONDA_ENV_PATH",
"CONDA_DEFAULT_ENV",
"PYENV_VERSION",
}
var venv string
for _, venvVar := range venvVars {
venv = p.language.env.Getenv(venvVar)
name := environment.Base(p.language.env, venv)
if p.canUseVenvName(name) {
p.Venv = name
break
}
}
}
func (p *python) inContext() bool {
return p.Venv != ""
}
func (p *python) canUseVenvName(name string) bool {
if name == "" || name == "." {
return false
}
if p.language.props.GetBool(properties.DisplayDefault, true) {
return true
}
invalidNames := [2]string{"system", "base"}
for _, a := range invalidNames {
if a == name {
return false
}
}
return true
}