mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
refactor: extend venv with python version
This commit is contained in:
parent
758afdd83e
commit
91b962a49a
|
@ -55,13 +55,13 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"type": "virtualenv",
|
||||
"type": "python",
|
||||
"style": "powerline",
|
||||
"powerline_symbol": "",
|
||||
"foreground": "#100e23",
|
||||
"background": "#906cff",
|
||||
"properties": {
|
||||
"python_icon": ""
|
||||
"prefix": " "
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -44,14 +44,14 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"type": "virtualenv",
|
||||
"type": "python",
|
||||
"style": "diamond",
|
||||
"foreground": "#ffffff",
|
||||
"background": "#FF6471",
|
||||
"leading_diamond": "",
|
||||
"trailing_diamond": "",
|
||||
"properties": {
|
||||
"python_icon": "",
|
||||
"prefix": " ",
|
||||
"prefix": "<#193549> </>"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,12 +76,12 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"type": "virtualenv",
|
||||
"type": "python",
|
||||
"style": "plain",
|
||||
"foreground": "#100e23",
|
||||
"properties": {
|
||||
"prefix": "",
|
||||
"python_icon": ""
|
||||
"prefix": " "
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -55,13 +55,13 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"type": "virtualenv",
|
||||
"type": "python",
|
||||
"style": "powerline",
|
||||
"powerline_symbol": "",
|
||||
"foreground": "#100e23",
|
||||
"background": "#906cff",
|
||||
"properties": {
|
||||
"python_icon": ""
|
||||
"prefix": " "
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -48,13 +48,13 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"type": "virtualenv",
|
||||
"type": "python",
|
||||
"style": "powerline",
|
||||
"powerline_symbol": "",
|
||||
"foreground": "#100e23",
|
||||
"background": "#906cff",
|
||||
"properties": {
|
||||
"python_icon": ""
|
||||
"prefix": " "
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -56,11 +56,11 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"type": "virtualenv",
|
||||
"type": "python",
|
||||
"style": "plain",
|
||||
"foreground": "#100e23",
|
||||
"properties": {
|
||||
"python_icon": ""
|
||||
"prefix": " "
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -38,8 +38,8 @@ const (
|
|||
Git SegmentType = "git"
|
||||
//Exit writes the last exit code
|
||||
Exit SegmentType = "exit"
|
||||
//Venv writes the virtual env name
|
||||
Venv SegmentType = "virtualenv"
|
||||
//Python writes the virtual env name
|
||||
Python SegmentType = "python"
|
||||
//Root writes root symbol
|
||||
Root SegmentType = "root"
|
||||
//Time writes the current timestamp
|
||||
|
@ -78,7 +78,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) (*properties,
|
|||
Path: &path{},
|
||||
Git: &git{},
|
||||
Exit: &exit{},
|
||||
Venv: &venv{},
|
||||
Python: &python{},
|
||||
Root: &root{},
|
||||
Text: &text{},
|
||||
Time: &tempus{},
|
||||
|
|
67
segment_python.go
Normal file
67
segment_python.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
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") {
|
||||
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
|
||||
}
|
150
segment_python_test.go
Executable file
150
segment_python_test.go
Executable file
|
@ -0,0 +1,150 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type pythonArgs struct {
|
||||
virtualEnvName string
|
||||
condaEnvName string
|
||||
condaDefaultName string
|
||||
pyEnvName string
|
||||
pathSeparator string
|
||||
pythonVersion string
|
||||
python3Version string
|
||||
hasFiles bool
|
||||
}
|
||||
|
||||
func newPythonArgs() *pythonArgs {
|
||||
return &pythonArgs{
|
||||
virtualEnvName: "",
|
||||
condaEnvName: "",
|
||||
condaDefaultName: "",
|
||||
pyEnvName: "",
|
||||
pathSeparator: "/",
|
||||
pythonVersion: "",
|
||||
python3Version: "",
|
||||
hasFiles: true,
|
||||
}
|
||||
}
|
||||
|
||||
func bootStrapPythonTest(args *pythonArgs) *python {
|
||||
env := new(MockedEnvironment)
|
||||
env.On("hasFiles", "*.py").Return(args.hasFiles)
|
||||
env.On("runCommand", "python", []string{"--version"}).Return(args.pythonVersion)
|
||||
env.On("runCommand", "python3", []string{"--version"}).Return(args.python3Version)
|
||||
env.On("getenv", "VIRTUAL_ENV").Return(args.virtualEnvName)
|
||||
env.On("getenv", "CONDA_ENV_PATH").Return(args.condaEnvName)
|
||||
env.On("getenv", "CONDA_DEFAULT_ENV").Return(args.condaDefaultName)
|
||||
env.On("getenv", "PYENV_VERSION").Return(args.pyEnvName)
|
||||
env.On("getPathSeperator", nil).Return(args.pathSeparator)
|
||||
python := &python{
|
||||
env: env,
|
||||
}
|
||||
return python
|
||||
}
|
||||
|
||||
func TestPythonWriterDisabledNoPythonFiles(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.hasFiles = false
|
||||
python := bootStrapPythonTest(args)
|
||||
assert.False(t, python.enabled(), "there are no Python files in the current folder")
|
||||
}
|
||||
|
||||
func TestPythonWriterDisabledNoPythonInstalled(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
python := bootStrapPythonTest(args)
|
||||
assert.False(t, python.enabled(), "Python isn't installed")
|
||||
}
|
||||
|
||||
func TestPythonWriterEnabledNoVirtualEnv(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.python3Version = "3.4.5"
|
||||
python := bootStrapPythonTest(args)
|
||||
assert.True(t, python.enabled())
|
||||
assert.Equal(t, args.python3Version, python.string())
|
||||
}
|
||||
|
||||
func TestPythonWriterEnabledVirtualEnvOverrule(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.python3Version = "3.4.5"
|
||||
args.condaEnvName = "myenv"
|
||||
props := &properties{
|
||||
values: map[Property]interface{}{
|
||||
DisplayVirtualEnv: false,
|
||||
},
|
||||
}
|
||||
python := bootStrapPythonTest(args)
|
||||
python.props = props
|
||||
assert.True(t, python.enabled())
|
||||
assert.Equal(t, args.python3Version, python.string())
|
||||
}
|
||||
|
||||
func TestPythonWriterEnabledVirtualEnv(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.python3Version = "3.4.5"
|
||||
args.condaEnvName = "myenv"
|
||||
expected := fmt.Sprintf("%s %s", args.condaEnvName, args.python3Version)
|
||||
props := &properties{
|
||||
values: map[Property]interface{}{
|
||||
DisplayVirtualEnv: true,
|
||||
},
|
||||
}
|
||||
python := bootStrapPythonTest(args)
|
||||
python.props = props
|
||||
assert.True(t, python.enabled())
|
||||
assert.Equal(t, expected, python.string())
|
||||
}
|
||||
|
||||
func TestPythonWriterEnabledWithVirtualEnv(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.virtualEnvName = "venv"
|
||||
args.python3Version = "3.4.5"
|
||||
expected := fmt.Sprintf("%s %s", args.virtualEnvName, args.python3Version)
|
||||
python := bootStrapPythonTest(args)
|
||||
assert.True(t, python.enabled())
|
||||
assert.Equal(t, expected, python.string())
|
||||
}
|
||||
|
||||
func TestPythonWriterEnabledWithCondaEnvPath(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.condaEnvName = "conda"
|
||||
args.python3Version = "3.4.5"
|
||||
expected := fmt.Sprintf("%s %s", args.condaEnvName, args.python3Version)
|
||||
python := bootStrapPythonTest(args)
|
||||
assert.True(t, python.enabled())
|
||||
assert.Equal(t, expected, python.string())
|
||||
}
|
||||
|
||||
func TestPythonWriterEnabledWithCondaDefaultEnv(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.condaDefaultName = "conda2"
|
||||
args.python3Version = "3.4.5"
|
||||
expected := fmt.Sprintf("%s %s", args.condaDefaultName, args.python3Version)
|
||||
python := bootStrapPythonTest(args)
|
||||
assert.True(t, python.enabled())
|
||||
assert.Equal(t, expected, python.string())
|
||||
}
|
||||
|
||||
func TestPythonWriterEnabledWithTwoValidEnvs(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.condaEnvName = "conda"
|
||||
args.condaDefaultName = "conda2"
|
||||
args.python3Version = "3.4.5"
|
||||
expected := fmt.Sprintf("%s %s", args.condaEnvName, args.python3Version)
|
||||
python := bootStrapPythonTest(args)
|
||||
assert.True(t, python.enabled())
|
||||
assert.Equal(t, expected, python.string())
|
||||
}
|
||||
|
||||
func TestPythonWriterNameTrailingSlash(t *testing.T) {
|
||||
args := newPythonArgs()
|
||||
args.virtualEnvName = "python/"
|
||||
args.pythonVersion = "2.7.3"
|
||||
python := bootStrapPythonTest(args)
|
||||
assert.True(t, python.enabled())
|
||||
assert.Equal(t, "python", python.venvName)
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type venv struct {
|
||||
props *properties
|
||||
env environmentInfo
|
||||
venvName string
|
||||
}
|
||||
|
||||
const (
|
||||
//PythonIcon used to indicate a Python virtualenv is active
|
||||
PythonIcon Property = "python_icon"
|
||||
)
|
||||
|
||||
func (v *venv) string() string {
|
||||
return fmt.Sprintf("%s %s", v.props.getString(PythonIcon, "PYTHON:"), v.venvName)
|
||||
}
|
||||
|
||||
func (v *venv) init(props *properties, env environmentInfo) {
|
||||
v.props = props
|
||||
v.env = env
|
||||
}
|
||||
|
||||
func (v *venv) enabled() bool {
|
||||
venvVars := []string{
|
||||
"VIRTUAL_ENV",
|
||||
"CONDA_ENV_PATH",
|
||||
"CONDA_DEFAULT_ENV",
|
||||
}
|
||||
var venv string
|
||||
for _, venvVar := range venvVars {
|
||||
venv = v.env.getenv(venvVar)
|
||||
if venv != "" {
|
||||
v.venvName = base(venv, v.env)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -1,102 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type venvArgs struct {
|
||||
virtualEnvName string
|
||||
condaEnvName string
|
||||
condaDefaultName string
|
||||
pathSeparator string
|
||||
}
|
||||
|
||||
func newVenvArgs() *venvArgs {
|
||||
return &venvArgs{
|
||||
virtualEnvName: "",
|
||||
condaEnvName: "",
|
||||
condaDefaultName: "",
|
||||
pathSeparator: "/",
|
||||
}
|
||||
}
|
||||
|
||||
func bootStrapVenvTest(args *venvArgs) *venv {
|
||||
env := new(MockedEnvironment)
|
||||
env.On("getenv", "VIRTUAL_ENV").Return(args.virtualEnvName)
|
||||
env.On("getenv", "CONDA_ENV_PATH").Return(args.condaEnvName)
|
||||
env.On("getenv", "CONDA_DEFAULT_ENV").Return(args.condaDefaultName)
|
||||
env.On("getPathSeperator", nil).Return(args.pathSeparator)
|
||||
venv := &venv{
|
||||
env: env,
|
||||
}
|
||||
return venv
|
||||
}
|
||||
|
||||
func TestVenvWriterDisabled(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
venv := bootStrapVenvTest(args)
|
||||
assert.False(t, venv.enabled(), "the virtualenv has no name")
|
||||
}
|
||||
|
||||
func TestVenvWriterEnabledWithVirtualEnv(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
args.virtualEnvName = "venv"
|
||||
venv := bootStrapVenvTest(args)
|
||||
assert.True(t, venv.enabled(), "the virtualenv has a name")
|
||||
}
|
||||
|
||||
func TestVenvWriterEnabledWithCondaEnvPath(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
args.condaEnvName = "venv"
|
||||
venv := bootStrapVenvTest(args)
|
||||
assert.True(t, venv.enabled(), "the virtualenv has a name")
|
||||
}
|
||||
|
||||
func TestVenvWriterEnabledWithCondaDefaultEnv(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
args.condaDefaultName = "venv"
|
||||
venv := bootStrapVenvTest(args)
|
||||
assert.True(t, venv.enabled(), "the virtualenv has a name")
|
||||
}
|
||||
|
||||
func TestVenvWriterEnabledWithTwoValidEnvs(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
args.virtualEnvName = "venv"
|
||||
args.condaDefaultName = "venv"
|
||||
venv := bootStrapVenvTest(args)
|
||||
assert.True(t, venv.enabled(), "the virtualenv has a name")
|
||||
}
|
||||
|
||||
func TestVenvWriterNameWithVirtualEnv(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
args.virtualEnvName = "venv"
|
||||
venv := bootStrapVenvTest(args)
|
||||
_ = venv.enabled()
|
||||
assert.Equal(t, "venv", venv.venvName)
|
||||
}
|
||||
|
||||
func TestVenvWriterNameWithCondaEnvPath(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
args.condaEnvName = "venv"
|
||||
venv := bootStrapVenvTest(args)
|
||||
_ = venv.enabled()
|
||||
assert.Equal(t, "venv", venv.venvName)
|
||||
}
|
||||
|
||||
func TestVenvWriterNameWithCondaDefaultEnv(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
args.condaDefaultName = "venv"
|
||||
venv := bootStrapVenvTest(args)
|
||||
_ = venv.enabled()
|
||||
assert.Equal(t, "venv", venv.venvName)
|
||||
}
|
||||
|
||||
func TestVenvWriterNameTrailingSlash(t *testing.T) {
|
||||
args := newVenvArgs()
|
||||
args.virtualEnvName = "venv/"
|
||||
venv := bootStrapVenvTest(args)
|
||||
_ = venv.enabled()
|
||||
assert.Equal(t, "venv", venv.venvName)
|
||||
}
|
|
@ -102,7 +102,7 @@ func getDefaultSettings() *Settings {
|
|||
Foreground: "#100e23",
|
||||
},
|
||||
{
|
||||
Type: Venv,
|
||||
Type: Python,
|
||||
Style: Powerline,
|
||||
PowerlineSymbol: "",
|
||||
Background: "#906cff",
|
||||
|
|
Loading…
Reference in a new issue