mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-02 05:41:10 -08:00
parent
890f9cde1b
commit
2abcd82f45
|
@ -32,3 +32,4 @@ Supports conda, virtualenv and pyenv.
|
||||||
- display_mode: `string` - determines when the segment is displayed
|
- display_mode: `string` - determines when the segment is displayed
|
||||||
- `always`: The segment is always displayed
|
- `always`: The segment is always displayed
|
||||||
- `files`: The segment is only displayed when `*.py` or `*.ipynb` files are present (default)
|
- `files`: The segment is only displayed when `*.py` or `*.ipynb` files are present (default)
|
||||||
|
- `environment`: The segment is only displayed when a virtual env is present
|
||||||
|
|
|
@ -2,6 +2,10 @@ package main
|
||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
|
type loadContext func()
|
||||||
|
|
||||||
|
type inContext func() bool
|
||||||
|
|
||||||
type language struct {
|
type language struct {
|
||||||
props *properties
|
props *properties
|
||||||
env environmentInfo
|
env environmentInfo
|
||||||
|
@ -12,6 +16,8 @@ type language struct {
|
||||||
versionRegex string
|
versionRegex string
|
||||||
version string
|
version string
|
||||||
exitCode int
|
exitCode int
|
||||||
|
loadContext loadContext
|
||||||
|
inContext inContext
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -21,6 +27,8 @@ const (
|
||||||
DisplayModeAlways string = "always"
|
DisplayModeAlways string = "always"
|
||||||
// DisplayModeFiles displays the segment when the current folder contains certain extensions
|
// DisplayModeFiles displays the segment when the current folder contains certain extensions
|
||||||
DisplayModeFiles string = "files"
|
DisplayModeFiles string = "files"
|
||||||
|
// DisplayModeEnvironment displays the segment when the environment has a language's context
|
||||||
|
DisplayModeEnvironment string = "environment"
|
||||||
// MissingCommandTextProperty sets the text to display when the command is not present in the system
|
// MissingCommandTextProperty sets the text to display when the command is not present in the system
|
||||||
MissingCommandTextProperty Property = "missing_command_text"
|
MissingCommandTextProperty Property = "missing_command_text"
|
||||||
// MissingCommandText displays empty string by default
|
// MissingCommandText displays empty string by default
|
||||||
|
@ -43,10 +51,12 @@ func (l *language) string() string {
|
||||||
func (l *language) enabled() bool {
|
func (l *language) enabled() bool {
|
||||||
displayMode := l.props.getString(DisplayMode, DisplayModeFiles)
|
displayMode := l.props.getString(DisplayMode, DisplayModeFiles)
|
||||||
displayVersion := l.props.getBool(DisplayVersion, true)
|
displayVersion := l.props.getBool(DisplayVersion, true)
|
||||||
|
l.loadLanguageContext()
|
||||||
switch displayMode {
|
switch displayMode {
|
||||||
case DisplayModeAlways:
|
case DisplayModeAlways:
|
||||||
return (!displayVersion || l.hasCommand())
|
return (!displayVersion || l.hasCommand())
|
||||||
|
case DisplayModeEnvironment:
|
||||||
|
return l.inLanguageContext()
|
||||||
case DisplayModeFiles:
|
case DisplayModeFiles:
|
||||||
fallthrough
|
fallthrough
|
||||||
default:
|
default:
|
||||||
|
@ -98,3 +108,17 @@ func (l *language) hasCommand() bool {
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *language) loadLanguageContext() {
|
||||||
|
if l.loadContext == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.loadContext()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *language) inLanguageContext() bool {
|
||||||
|
if l.inContext == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return l.inContext()
|
||||||
|
}
|
||||||
|
|
|
@ -31,17 +31,19 @@ func (p *python) init(props *properties, env environmentInfo) {
|
||||||
versionParam: "--version",
|
versionParam: "--version",
|
||||||
extensions: []string{"*.py", "*.ipynb"},
|
extensions: []string{"*.py", "*.ipynb"},
|
||||||
versionRegex: `Python (?P<version>[0-9]+.[0-9]+.[0-9]+)`,
|
versionRegex: `Python (?P<version>[0-9]+.[0-9]+.[0-9]+)`,
|
||||||
|
loadContext: p.loadContext,
|
||||||
|
inContext: p.inContext,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *python) enabled() bool {
|
func (p *python) enabled() bool {
|
||||||
if !p.language.enabled() {
|
return p.language.enabled()
|
||||||
return false
|
}
|
||||||
}
|
|
||||||
|
func (p *python) loadContext() {
|
||||||
venvVars := []string{
|
venvVars := []string{
|
||||||
"VIRTUAL_ENV",
|
"VIRTUAL_ENV",
|
||||||
"CONDA_ENV_PATH",
|
"CONDA_ENV_PATH",
|
||||||
"CONDA_DEFAULT_ENV",
|
|
||||||
"PYENV_VERSION",
|
"PYENV_VERSION",
|
||||||
}
|
}
|
||||||
var venv string
|
var venv string
|
||||||
|
@ -52,5 +54,8 @@ func (p *python) enabled() bool {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
}
|
||||||
|
|
||||||
|
func (p *python) inContext() bool {
|
||||||
|
return p.venvName != ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ import (
|
||||||
type pythonArgs struct {
|
type pythonArgs struct {
|
||||||
virtualEnvName string
|
virtualEnvName string
|
||||||
condaEnvName string
|
condaEnvName string
|
||||||
condaDefaultName string
|
|
||||||
pyEnvName string
|
pyEnvName string
|
||||||
displayVersion bool
|
displayVersion bool
|
||||||
}
|
}
|
||||||
|
@ -21,7 +20,6 @@ func bootStrapPythonTest(args *pythonArgs) *python {
|
||||||
env.On("hasFiles", "*.py").Return(true)
|
env.On("hasFiles", "*.py").Return(true)
|
||||||
env.On("getenv", "VIRTUAL_ENV").Return(args.virtualEnvName)
|
env.On("getenv", "VIRTUAL_ENV").Return(args.virtualEnvName)
|
||||||
env.On("getenv", "CONDA_ENV_PATH").Return(args.condaEnvName)
|
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("getenv", "PYENV_VERSION").Return(args.pyEnvName)
|
||||||
env.On("getPathSeperator", nil).Return("")
|
env.On("getPathSeperator", nil).Return("")
|
||||||
props := &properties{
|
props := &properties{
|
||||||
|
@ -55,16 +53,6 @@ func TestPythonCondaEnv(t *testing.T) {
|
||||||
assert.Equal(t, expected, python.string())
|
assert.Equal(t, expected, python.string())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPythonCondaDefaultName(t *testing.T) {
|
|
||||||
expected := "CONDADEF"
|
|
||||||
args := &pythonArgs{
|
|
||||||
condaDefaultName: expected,
|
|
||||||
}
|
|
||||||
python := bootStrapPythonTest(args)
|
|
||||||
assert.True(t, python.enabled())
|
|
||||||
assert.Equal(t, expected, python.string())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPythonPyEnv(t *testing.T) {
|
func TestPythonPyEnv(t *testing.T) {
|
||||||
expected := "PYENV"
|
expected := "PYENV"
|
||||||
args := &pythonArgs{
|
args := &pythonArgs{
|
||||||
|
@ -86,3 +74,19 @@ func TestPythonPyEnvWithVersion(t *testing.T) {
|
||||||
assert.Equal(t, expected, python.string())
|
assert.Equal(t, expected, python.string())
|
||||||
assert.Equal(t, "3.8.4", python.language.version)
|
assert.Equal(t, "3.8.4", python.language.version)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPythonPythonInContext(t *testing.T) {
|
||||||
|
args := &pythonArgs{
|
||||||
|
pyEnvName: "PYENV",
|
||||||
|
displayVersion: true,
|
||||||
|
}
|
||||||
|
python := bootStrapPythonTest(args)
|
||||||
|
python.loadContext()
|
||||||
|
assert.True(t, python.inContext())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPythonPythonNotInContext(t *testing.T) {
|
||||||
|
python := bootStrapPythonTest(&pythonArgs{})
|
||||||
|
python.loadContext()
|
||||||
|
assert.False(t, python.inContext())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue