refactor: use language context to display segment

relates to #239
This commit is contained in:
Jan De Dobbeleer 2021-01-12 20:38:13 +01:00 committed by Jan De Dobbeleer
parent 890f9cde1b
commit 2abcd82f45
4 changed files with 56 additions and 22 deletions

View file

@ -32,3 +32,4 @@ Supports conda, virtualenv and pyenv.
- display_mode: `string` - determines when the segment is displayed
- `always`: The segment is always displayed
- `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

View file

@ -2,6 +2,10 @@ package main
import "errors"
type loadContext func()
type inContext func() bool
type language struct {
props *properties
env environmentInfo
@ -12,6 +16,8 @@ type language struct {
versionRegex string
version string
exitCode int
loadContext loadContext
inContext inContext
}
const (
@ -21,6 +27,8 @@ const (
DisplayModeAlways string = "always"
// DisplayModeFiles displays the segment when the current folder contains certain extensions
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 Property = "missing_command_text"
// MissingCommandText displays empty string by default
@ -43,10 +51,12 @@ func (l *language) string() string {
func (l *language) enabled() bool {
displayMode := l.props.getString(DisplayMode, DisplayModeFiles)
displayVersion := l.props.getBool(DisplayVersion, true)
l.loadLanguageContext()
switch displayMode {
case DisplayModeAlways:
return (!displayVersion || l.hasCommand())
case DisplayModeEnvironment:
return l.inLanguageContext()
case DisplayModeFiles:
fallthrough
default:
@ -98,3 +108,17 @@ func (l *language) hasCommand() bool {
}
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()
}

View file

@ -31,17 +31,19 @@ func (p *python) init(props *properties, env environmentInfo) {
versionParam: "--version",
extensions: []string{"*.py", "*.ipynb"},
versionRegex: `Python (?P<version>[0-9]+.[0-9]+.[0-9]+)`,
loadContext: p.loadContext,
inContext: p.inContext,
}
}
func (p *python) enabled() bool {
if !p.language.enabled() {
return false
}
return p.language.enabled()
}
func (p *python) loadContext() {
venvVars := []string{
"VIRTUAL_ENV",
"CONDA_ENV_PATH",
"CONDA_DEFAULT_ENV",
"PYENV_VERSION",
}
var venv string
@ -52,5 +54,8 @@ func (p *python) enabled() bool {
break
}
}
return true
}
func (p *python) inContext() bool {
return p.venvName != ""
}

View file

@ -7,11 +7,10 @@ import (
)
type pythonArgs struct {
virtualEnvName string
condaEnvName string
condaDefaultName string
pyEnvName string
displayVersion bool
virtualEnvName string
condaEnvName string
pyEnvName string
displayVersion bool
}
func bootStrapPythonTest(args *pythonArgs) *python {
@ -21,7 +20,6 @@ func bootStrapPythonTest(args *pythonArgs) *python {
env.On("hasFiles", "*.py").Return(true)
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("")
props := &properties{
@ -55,16 +53,6 @@ func TestPythonCondaEnv(t *testing.T) {
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) {
expected := "PYENV"
args := &pythonArgs{
@ -86,3 +74,19 @@ func TestPythonPyEnvWithVersion(t *testing.T) {
assert.Equal(t, expected, python.string())
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())
}