refactor: universal display_default

relates to #380
This commit is contained in:
Jan De Dobbeleer 2021-02-18 18:47:53 +01:00 committed by Jan De Dobbeleer
parent 59b0595c5b
commit 734158a8a3
10 changed files with 25 additions and 13 deletions

View file

@ -28,6 +28,8 @@ Display the currently active AWS profile and region.
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
properties below. Defaults to `{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}`
- display_default: `boolean` - display the segment or not when the user profile matches `default` - defaults
to `true`
## Template Properties

View file

@ -27,7 +27,7 @@ Supports conda, virtualenv and pyenv.
## Properties
- display_virtual_env: `boolean` - show the name of the virtualenv or not - defaults to `true`
- display_default_env: `boolean` - show the name of the virtualenv when it's default (`system`, `base`)
- display_default: `boolean` - show the name of the virtualenv when it's default (`system`, `base`)
or not - defaults to `true`
- display_version: `boolean` - display the python version - defaults to `true`
- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true`

View file

@ -31,7 +31,7 @@ to `\uF817 `
- display_user: `boolean` - display the user name or not - defaults to `true`
- display_host: `boolean` - display the host name or not - defaults to `true`
- default_user_name: `string` - name of the default user - defaults to empty
- display_default_user: `boolean` - display the segment or not when the user matches `default_user_name` - defaults
- display_default: `boolean` - display the segment or not when the user matches `default_user_name` - defaults
to `true`
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
properties below. Only used when a value is set, making the above properties obsolete.

View file

@ -27,6 +27,8 @@ const (
SegmentTemplate Property = "template"
// DisplayError to display when an error occurs or not
DisplayError Property = "display_error"
// DisplayDefault hides or shows the default
DisplayDefault Property = "display_default"
)
type properties struct {

View file

@ -31,7 +31,11 @@ func (a *aws) enabled() bool {
}
return ""
}
displayDefaultUser := a.props.getBool(DisplayDefault, true)
a.Profile = getEnvFirstMatch("AWS_VAULT", "AWS_PROFILE")
if !displayDefaultUser && a.Profile == defaultUser {
return false
}
a.Region = getEnvFirstMatch("AWS_DEFAULT_REGION", "AWS_REGION")
if a.Profile != "" && a.Region != "" {
return true
@ -41,6 +45,9 @@ func (a *aws) enabled() bool {
return true
}
a.getConfigFileInfo()
if !displayDefaultUser && a.Profile == defaultUser {
return false
}
return a.Profile != ""
}

View file

@ -17,8 +17,11 @@ func TestAWSSegment(t *testing.T) {
DefaultRegion string
ConfigFile string
Template string
DisplayDefault bool
}{
{Case: "disabled", ExpectedString: "", ExpectedEnabled: false},
{Case: "enabled with default user", ExpectedString: "default@eu-west", Profile: "default", Region: "eu-west", ExpectedEnabled: true, DisplayDefault: true},
{Case: "disabled with default user", ExpectedString: "default", Profile: "default", Region: "eu-west", ExpectedEnabled: false, DisplayDefault: false},
{Case: "enabled no region", ExpectedString: "company", ExpectedEnabled: true, Profile: "company"},
{Case: "enabled with region", ExpectedString: "company@eu-west", ExpectedEnabled: true, Profile: "company", Region: "eu-west"},
{
@ -49,7 +52,9 @@ func TestAWSSegment(t *testing.T) {
env.On("getFileContent", "/usr/home/.aws/config").Return("")
env.On("homeDir", nil).Return("/usr/home")
props := &properties{
values: map[Property]interface{}{},
values: map[Property]interface{}{
DisplayDefault: tc.DisplayDefault,
},
}
if tc.Template != "" {
props.values[SegmentTemplate] = tc.Template

View file

@ -10,8 +10,6 @@ type python struct {
const (
// DisplayVirtualEnv shows or hides the virtual env
DisplayVirtualEnv Property = "display_virtual_env"
// DisplayDefaultEnv shows or hides the default env names (system/base)
DisplayDefaultEnv Property = "display_default_env"
)
func (p *python) string() string {
@ -78,7 +76,7 @@ func (p *python) canUseVenvName(name string) bool {
if name == "" || name == "." {
return false
}
if p.language.props.getBool(DisplayDefaultEnv, true) {
if p.language.props.getBool(DisplayDefault, true) {
return true
}
invalidNames := [2]string{"system", "base"}

View file

@ -14,13 +14,13 @@ func TestPythonVirtualEnv(t *testing.T) {
CondaDefaultEnvName string
PyEnvName string
DisplayVersion bool
DisplayDefaultEnv bool
DisplayDefault bool
}{
{Expected: "VENV", VirtualEnvName: "VENV"},
{Expected: "CONDA", CondaEnvName: "CONDA"},
{Expected: "CONDA", CondaDefaultEnvName: "CONDA"},
{Expected: "", CondaDefaultEnvName: "base"},
{Expected: "base", CondaDefaultEnvName: "base", DisplayDefaultEnv: true},
{Expected: "base", CondaDefaultEnvName: "base", DisplayDefault: true},
{Expected: "PYENV", PyEnvName: "PYENV"},
{Expected: "PYENV 3.8.4", PyEnvName: "PYENV", DisplayVersion: true},
}
@ -39,7 +39,7 @@ func TestPythonVirtualEnv(t *testing.T) {
values: map[Property]interface{}{
DisplayVersion: tc.DisplayVersion,
DisplayVirtualEnv: true,
DisplayDefaultEnv: tc.DisplayDefaultEnv,
DisplayDefault: tc.DisplayDefault,
},
}
python := &python{}

View file

@ -29,13 +29,11 @@ const (
SSHIcon Property = "ssh_icon"
// DefaultUserName holds the default user of the platform
DefaultUserName Property = "default_user_name"
// DisplayDefaultUser hides or shows the user name when it's the user set in DefaultUserName
DisplayDefaultUser Property = "display_default_user"
)
func (s *session) enabled() bool {
s.UserName = s.getUserName()
showDefaultUser := s.props.getBool(DisplayDefaultUser, true)
showDefaultUser := s.props.getBool(DisplayDefault, true)
defaultUser := s.props.getString(DefaultUserName, "")
if !showDefaultUser && defaultUser == s.UserName {
return false

View file

@ -43,7 +43,7 @@
"background": "#2f2f2f",
"properties": {
"user_info_separator": "<#7a7a7a>\uf1fa</>",
"display_default_user": false,
"display_default": false,
"user_color": "#77f5d6",
"host_color": "#2EEFBF",
"postfix": "<#7a7a7a> \ue0b1</>"