oh-my-posh/src/segment_time.go

56 lines
1.2 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
import "time"
2019-03-13 04:14:30 -07:00
type tempus struct {
2022-01-01 11:08:08 -08:00
props Properties
2022-01-01 11:09:52 -08:00
env Environment
templateText string
CurrentDate time.Time
2019-03-13 04:14:30 -07:00
}
const (
// TimeFormat uses the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to format the current time
2019-03-13 04:14:30 -07:00
TimeFormat Property = "time_format"
)
func (t *tempus) enabled() bool {
// if no date set, use now(unit testing)
if t.CurrentDate.IsZero() {
t.CurrentDate = time.Now()
}
segmentTemplate := t.props.getString(SegmentTemplate, "")
if segmentTemplate != "" {
template := &textTemplate{
Template: segmentTemplate,
Context: t,
Env: t.env,
}
2021-04-11 06:24:03 -07:00
var err error
t.templateText, err = template.render()
if err != nil {
t.templateText = err.Error()
}
return len(t.templateText) > 0
}
2019-03-13 04:14:30 -07:00
return true
}
func (t *tempus) string() string {
return t.getFormattedText()
2019-03-13 04:14:30 -07:00
}
2022-01-01 11:09:52 -08:00
func (t *tempus) init(props Properties, env Environment) {
2019-03-13 04:14:30 -07:00
t.props = props
t.env = env
}
func (t *tempus) getFormattedText() string {
if len(t.templateText) > 0 {
return t.templateText
}
// keep old behaviour if no template
timeFormatProperty := t.props.getString(TimeFormat, "15:04:05")
return t.CurrentDate.Format(timeFormatProperty)
}