1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Allow custom date and time formats for clocks

This commit is contained in:
Dan Rabinowitz 2018-10-01 22:26:15 -06:00
parent eb7da5131e
commit e8ef4800f8
3 changed files with 14 additions and 11 deletions

View File

@ -2,8 +2,6 @@ package clocks
import ( import (
"time" "time"
"github.com/senorprogrammer/wtf/wtf"
) )
type Clock struct { type Clock struct {
@ -20,8 +18,8 @@ func NewClock(label string, timeLoc *time.Location) Clock {
return clock return clock
} }
func (clock *Clock) Date() string { func (clock *Clock) Date(dateFormat string) string {
return clock.LocalTime().Format(wtf.SimpleDateFormat) return clock.LocalTime().Format(dateFormat)
} }
func (clock *Clock) LocalTime() time.Time { func (clock *Clock) LocalTime() time.Time {
@ -32,6 +30,6 @@ func (clock *Clock) ToLocal(t time.Time) time.Time {
return t.In(clock.Location) return t.In(clock.Location)
} }
func (clock *Clock) Time() string { func (clock *Clock) Time(timeFormat string) string {
return clock.LocalTime().Format(wtf.SimpleTimeFormat) return clock.LocalTime().Format(timeFormat)
} }

View File

@ -6,7 +6,7 @@ import (
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
func (widget *Widget) display(clocks []Clock) { func (widget *Widget) display(clocks []Clock, dateFormat string, timeFormat string) {
if len(clocks) == 0 { if len(clocks) == 0 {
widget.View.SetText(fmt.Sprintf("\n%s", " no timezone data available")) widget.View.SetText(fmt.Sprintf("\n%s", " no timezone data available"))
return return
@ -18,8 +18,8 @@ func (widget *Widget) display(clocks []Clock) {
" [%s]%-12s %-10s %7s[white]\n", " [%s]%-12s %-10s %7s[white]\n",
wtf.RowColor("clocks", idx), wtf.RowColor("clocks", idx),
clock.Label, clock.Label,
clock.Time(), clock.Time(timeFormat),
clock.Date(), clock.Date(dateFormat),
) )
} }

View File

@ -12,6 +12,8 @@ type Widget struct {
wtf.TextWidget wtf.TextWidget
clockColl ClockCollection clockColl ClockCollection
dateFormat string
timeFormat string
} }
func NewWidget(app *tview.Application) *Widget { func NewWidget(app *tview.Application) *Widget {
@ -21,6 +23,9 @@ func NewWidget(app *tview.Application) *Widget {
widget.clockColl = widget.buildClockCollection(wtf.Config.UMap("wtf.mods.clocks.locations")) widget.clockColl = widget.buildClockCollection(wtf.Config.UMap("wtf.mods.clocks.locations"))
widget.dateFormat = wtf.Config.UString("wtf.mods.clocks.dateFormat", wtf.SimpleDateFormat)
widget.timeFormat = wtf.Config.UString("wtf.mods.clocks.timeFormat", wtf.SimpleTimeFormat)
return &widget return &widget
} }
@ -28,7 +33,7 @@ func NewWidget(app *tview.Application) *Widget {
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt() widget.UpdateRefreshedAt()
widget.display(widget.clockColl.Sorted()) widget.display(widget.clockColl.Sorted(), widget.dateFormat, widget.timeFormat)
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */