oh-my-posh/src/segment_envar.go

41 lines
721 B
Go
Raw Normal View History

2020-10-09 10:22:32 -07:00
package main
type envvar struct {
2021-11-26 01:37:33 -08:00
props properties
2021-11-14 01:50:47 -08:00
env environmentInfo
Value string
2020-10-09 10:22:32 -07:00
}
const (
// VarName name of the variable
2020-10-09 10:22:32 -07:00
VarName Property = "var_name"
)
func (e *envvar) enabled() bool {
name := e.props.getString(VarName, "")
2021-11-14 01:50:47 -08:00
e.Value = e.env.getenv(name)
return e.Value != ""
2020-10-09 10:22:32 -07:00
}
func (e *envvar) string() string {
segmentTemplate := e.props.getString(SegmentTemplate, "")
if len(segmentTemplate) == 0 {
return e.Value
}
template := &textTemplate{
Template: segmentTemplate,
Context: e,
Env: e.env,
}
text, err := template.render()
if err != nil {
return err.Error()
}
return text
2020-10-09 10:22:32 -07:00
}
2021-11-26 01:37:33 -08:00
func (e *envvar) init(props properties, env environmentInfo) {
2020-10-09 10:22:32 -07:00
e.props = props
e.env = env
}