diff --git a/src/segment_text.go b/src/segment_text.go index 669ecb11ff5e..d41300d9c13d 100644 --- a/src/segment_text.go +++ b/src/segment_text.go @@ -1,8 +1,9 @@ package main type text struct { - props *properties - env environmentInfo + props *properties + env environmentInfo + content string } const ( @@ -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) { diff --git a/src/segment_text_test.go b/src/segment_text_test.go index a6087a358640..1385adfdfcdd 100644 --- a/src/segment_text_test.go +++ b/src/segment_text_test.go @@ -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 { @@ -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{ @@ -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) } }