Skip to content

Commit

Permalink
fix(text): disable when text resolves to empty
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 6, 2021
1 parent 356dda3 commit d081677
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
17 changes: 9 additions & 8 deletions src/segment_text.go
@@ -1,8 +1,9 @@
package main

type text struct {
props *properties
env environmentInfo
props *properties
env environmentInfo
content string
}

const (
Expand All @@ -11,18 +12,18 @@ const (
)

func (t *text) enabled() bool {
return true
}

func (t *text) string() string {
textProperty := t.props.getString(TextProperty, "!!text property not defined!!")
template := &textTemplate{
Template: textProperty,
Context: t,
Env: t.env,
}
textOutput := template.renderPlainContextTemplate(nil)
return textOutput
t.content = template.renderPlainContextTemplate(nil)
return len(t.content) > 0
}

func (t *text) string() string {
return t.content
}

func (t *text) init(props *properties, env environmentInfo) {
Expand Down
11 changes: 8 additions & 3 deletions src/segment_text_test.go
Expand Up @@ -8,15 +8,18 @@ import (

func TestTextSegment(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Text string
Case string
ExpectedString string
Text string
ExpectedDisabled bool
}{
{Case: "standard text", ExpectedString: "hello", Text: "hello"},
{Case: "template text with env var", ExpectedString: "hello world", Text: "{{ .Env.HELLO }} world"},
{Case: "template text with shell name", ExpectedString: "hello world from terminal", Text: "{{ .Env.HELLO }} world from {{ .Shell }}"},
{Case: "template text with folder", ExpectedString: "hello world in posh", Text: "{{ .Env.HELLO }} world in {{ .Folder }}"},
{Case: "template text with user", ExpectedString: "hello Posh", Text: "{{ .Env.HELLO }} {{ .User }}"},
{Case: "empty text", Text: "", ExpectedDisabled: true},
{Case: "empty template result", Text: "{{ .Env.WORLD }}", ExpectedDisabled: true},
}

for _, tc := range cases {
Expand All @@ -27,6 +30,7 @@ func TestTextSegment(t *testing.T) {
env.On("isRunningAsRoot", nil).Return(true)
env.On("getShellName", nil).Return("terminal")
env.On("getenv", "HELLO").Return("hello")
env.On("getenv", "WORLD").Return("")
env.On("getCurrentUser", nil).Return("Posh")
env.On("getHostName", nil).Return("MyHost", nil)
props := &properties{
Expand All @@ -38,6 +42,7 @@ func TestTextSegment(t *testing.T) {
env: env,
props: props,
}
assert.Equal(t, tc.ExpectedDisabled, !txt.enabled(), tc.Case)
assert.Equal(t, tc.ExpectedString, txt.string(), tc.Case)
}
}

0 comments on commit d081677

Please sign in to comment.