feat(text): add template support

resolves #884
This commit is contained in:
Jan De Dobbeleer 2021-08-10 05:48:44 +02:00 committed by Jan De Dobbeleer
parent 9f4f9d30db
commit 9bd21c6573
3 changed files with 64 additions and 1 deletions

View file

@ -25,5 +25,19 @@ Display text.
## Properties ## Properties
- text: `string` - text/icon to display. Accepts [coloring foreground][coloring] just like `prefix` and `postfix`. - text: `string` - text/icon to display. Accepts [coloring foreground][coloring] just like `prefix` and `postfix`.
Powered by go [text/template][go-text-template] templates extended with [sprig][sprig] utilizing the
properties below.
## Template Properties
- `.Root`: `boolean` - is the current user root/admin or not
- `.Path`: `string` - the current working directory
- `.Folder`: `string` - the current working folder
- `.Shell`: `string` - the current shell name
- `.User`: `string` - the current user name
- `.Host`: `string` - the host name
- `.Env.VarName`: `string` - Any environment variable where `VarName` is the environment variable name
[coloring]: /docs/configure#colors [coloring]: /docs/configure#colors
[go-text-template]: https://golang.org/pkg/text/template/
[sprig]: https://masterminds.github.io/sprig/

View file

@ -16,7 +16,13 @@ func (t *text) enabled() bool {
func (t *text) string() string { func (t *text) string() string {
textProperty := t.props.getString(TextProperty, "!!text property not defined!!") textProperty := t.props.getString(TextProperty, "!!text property not defined!!")
return textProperty template := &textTemplate{
Template: textProperty,
Context: t,
Env: t.env,
}
textOutput := template.renderPlainContextTemplate(nil)
return textOutput
} }
func (t *text) init(props *properties, env environmentInfo) { func (t *text) init(props *properties, env environmentInfo) {

43
src/segment_text_test.go Normal file
View file

@ -0,0 +1,43 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTextSegment(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Text string
}{
{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 }}"},
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getcwd", nil).Return("/usr/home/posh")
env.On("homeDir", nil).Return("/usr/home")
env.On("getPathSeperator", nil).Return("/")
env.On("isRunningAsRoot", nil).Return(true)
env.On("getShellName", nil).Return("terminal")
env.On("getenv", "HELLO").Return("hello")
env.On("getCurrentUser", nil).Return("Posh")
env.On("getHostName", nil).Return("MyHost", nil)
props := &properties{
values: map[Property]interface{}{
TextProperty: tc.Text,
},
}
txt := &text{
env: env,
props: props,
}
assert.Equal(t, tc.ExpectedString, txt.string(), tc.Case)
}
}