Skip to content

Commit d081677

Browse files
fix(text): disable when text resolves to empty
1 parent 356dda3 commit d081677

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/segment_text.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package main
22

33
type text struct {
4-
props *properties
5-
env environmentInfo
4+
props *properties
5+
env environmentInfo
6+
content string
67
}
78

89
const (
@@ -11,18 +12,18 @@ const (
1112
)
1213

1314
func (t *text) enabled() bool {
14-
return true
15-
}
16-
17-
func (t *text) string() string {
1815
textProperty := t.props.getString(TextProperty, "!!text property not defined!!")
1916
template := &textTemplate{
2017
Template: textProperty,
2118
Context: t,
2219
Env: t.env,
2320
}
24-
textOutput := template.renderPlainContextTemplate(nil)
25-
return textOutput
21+
t.content = template.renderPlainContextTemplate(nil)
22+
return len(t.content) > 0
23+
}
24+
25+
func (t *text) string() string {
26+
return t.content
2627
}
2728

2829
func (t *text) init(props *properties, env environmentInfo) {

src/segment_text_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ import (
88

99
func TestTextSegment(t *testing.T) {
1010
cases := []struct {
11-
Case string
12-
ExpectedString string
13-
Text string
11+
Case string
12+
ExpectedString string
13+
Text string
14+
ExpectedDisabled bool
1415
}{
1516
{Case: "standard text", ExpectedString: "hello", Text: "hello"},
1617
{Case: "template text with env var", ExpectedString: "hello world", Text: "{{ .Env.HELLO }} world"},
1718
{Case: "template text with shell name", ExpectedString: "hello world from terminal", Text: "{{ .Env.HELLO }} world from {{ .Shell }}"},
1819
{Case: "template text with folder", ExpectedString: "hello world in posh", Text: "{{ .Env.HELLO }} world in {{ .Folder }}"},
1920
{Case: "template text with user", ExpectedString: "hello Posh", Text: "{{ .Env.HELLO }} {{ .User }}"},
21+
{Case: "empty text", Text: "", ExpectedDisabled: true},
22+
{Case: "empty template result", Text: "{{ .Env.WORLD }}", ExpectedDisabled: true},
2023
}
2124

2225
for _, tc := range cases {
@@ -27,6 +30,7 @@ func TestTextSegment(t *testing.T) {
2730
env.On("isRunningAsRoot", nil).Return(true)
2831
env.On("getShellName", nil).Return("terminal")
2932
env.On("getenv", "HELLO").Return("hello")
33+
env.On("getenv", "WORLD").Return("")
3034
env.On("getCurrentUser", nil).Return("Posh")
3135
env.On("getHostName", nil).Return("MyHost", nil)
3236
props := &properties{
@@ -38,6 +42,7 @@ func TestTextSegment(t *testing.T) {
3842
env: env,
3943
props: props,
4044
}
45+
assert.Equal(t, tc.ExpectedDisabled, !txt.enabled(), tc.Case)
4146
assert.Equal(t, tc.ExpectedString, txt.string(), tc.Case)
4247
}
4348
}

0 commit comments

Comments
 (0)