oh-my-posh/src/segment_python.go

87 lines
1.9 KiB
Go
Raw Normal View History

package main
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 Property = "fetch_virtual_env"
)
func (p *python) template() string {
return languageTemplate
}
2022-01-01 11:09:52 -08:00
func (p *python) init(props Properties, env 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/)",
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() {
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 := base(venv, p.language.env)
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(DisplayDefault, true) {
return true
}
invalidNames := [2]string{"system", "base"}
for _, a := range invalidNames {
if a == name {
return false
}
}
return true
}