oh-my-posh/segment_python.go
Travis Illig 5844faa54d feat: dotnet segment for .NET SDK display
New segment for .NET SDK version (or unsupported version) display.

Includes update for handling command execution errors so segments
can act differently based on exit codes. Using a custom error
type to make it testable rather than passing the OS error directly
to the segment.
2020-10-16 11:39:01 -07:00

68 lines
1.3 KiB
Go

package main
import (
"fmt"
"strings"
)
type python struct {
props *properties
env environmentInfo
venvName string
pythonVersion string
}
const (
//DisplayVirtualEnv shows or hides the virtual env
DisplayVirtualEnv Property = "display_virtual_env"
)
func (p *python) string() string {
if p.venvName == "" || !p.props.getBool(DisplayVirtualEnv, true) {
return p.pythonVersion
}
return fmt.Sprintf("%s %s", p.venvName, p.pythonVersion)
}
func (p *python) init(props *properties, env environmentInfo) {
p.props = props
p.env = env
}
func (p *python) enabled() bool {
if !p.env.hasFiles("*.py") && !p.env.hasFiles("*.ipynb") {
return false
}
pythonVersions := []string{
"python3",
"python",
}
for index, python := range pythonVersions {
version, _ := p.env.runCommand(python, "--version")
if version != "" {
rawVersion := strings.TrimLeft(version, "Python")
p.pythonVersion = strings.Trim(rawVersion, " ")
break
}
//last element, Python isn't installed on this machine
if index == len(pythonVersions)-1 {
return false
}
}
venvVars := []string{
"VIRTUAL_ENV",
"CONDA_ENV_PATH",
"CONDA_DEFAULT_ENV",
"PYENV_VERSION",
}
var venv string
for _, venvVar := range venvVars {
venv = p.env.getenv(venvVar)
if venv != "" {
p.venvName = base(venv, p.env)
break
}
}
return true
}