mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-26 11:34:04 -08:00
parent
9f4f9d30db
commit
9bd21c6573
|
@ -25,5 +25,19 @@ Display text.
|
|||
## Properties
|
||||
|
||||
- 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
|
||||
[go-text-template]: https://golang.org/pkg/text/template/
|
||||
[sprig]: https://masterminds.github.io/sprig/
|
||||
|
|
|
@ -16,7 +16,13 @@ func (t *text) enabled() bool {
|
|||
|
||||
func (t *text) string() string {
|
||||
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) {
|
||||
|
|
43
src/segment_text_test.go
Normal file
43
src/segment_text_test.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue