oh-my-posh/segment_python.go

57 lines
1.1 KiB
Go
Raw Normal View History

package main
2020-11-14 11:04:04 -08:00
import "fmt"
type python struct {
2020-11-14 11:04:04 -08:00
language *language
venvName string
}
const (
// DisplayVirtualEnv shows or hides the virtual env
DisplayVirtualEnv Property = "display_virtual_env"
)
func (p *python) string() string {
2020-11-14 11:04:04 -08:00
if p.venvName == "" || !p.language.props.getBool(DisplayVirtualEnv, true) {
return p.language.string()
}
version := p.language.string()
if version == "" {
return p.venvName
}
2020-11-14 11:04:04 -08:00
return fmt.Sprintf("%s %s", p.venvName, version)
}
func (p *python) init(props *properties, env environmentInfo) {
2020-11-14 11:04:04 -08:00
p.language = &language{
env: env,
props: props,
commands: []string{"python", "python3"},
versionParam: "--version",
extensions: []string{"*.py", "*.ipynb"},
versionRegex: `Python (?P<version>[0-9]+.[0-9]+.[0-9]+)`,
}
}
func (p *python) enabled() bool {
2020-11-14 11:04:04 -08:00
if !p.language.enabled() {
return false
}
venvVars := []string{
"VIRTUAL_ENV",
"CONDA_ENV_PATH",
"CONDA_DEFAULT_ENV",
"PYENV_VERSION",
}
var venv string
for _, venvVar := range venvVars {
2020-11-14 11:04:04 -08:00
venv = p.language.env.getenv(venvVar)
if venv != "" {
2020-11-14 11:04:04 -08:00
p.venvName = base(venv, p.language.env)
break
}
}
return true
}