oh-my-posh/src/segment_time.go

52 lines
1.1 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()
}
2022-01-22 10:46:56 -08:00
return true
}
func (t *tempus) string() string {
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()
}
2022-01-22 10:46:56 -08:00
if len(t.templateText) > 0 {
return t.templateText
}
}
2022-01-22 10:46:56 -08:00
// keep old behaviour if no template for now
timeFormatProperty := t.props.getString(TimeFormat, "15:04:05")
return t.CurrentDate.Format(timeFormatProperty)
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
}