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

Merge branch 'naturalclockorder' of github.com:Seanstoppable/wtf into Seanstoppable-naturalclockorder

This commit is contained in:
Chris Cummer 2020-10-06 19:31:05 -07:00
commit 15b91b2d28
4 changed files with 55 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package clocks package clocks
import ( import (
"strings"
"time" "time"
) )
@ -18,6 +19,14 @@ func NewClock(label string, timeLoc *time.Location) Clock {
return clock return clock
} }
func BuildClock(label string, location string) (clock Clock, err error) {
timeLoc, err := time.LoadLocation(sanitizeLocation(location))
if err != nil {
return Clock{}, err
}
return NewClock(label, timeLoc), nil
}
func (clock *Clock) Date(dateFormat string) string { func (clock *Clock) Date(dateFormat string) string {
return clock.LocalTime().Format(dateFormat) return clock.LocalTime().Format(dateFormat)
} }
@ -33,3 +42,7 @@ func (clock *Clock) ToLocal(t time.Time) time.Time {
func (clock *Clock) Time(timeFormat string) string { func (clock *Clock) Time(timeFormat string) string {
return clock.LocalTime().Format(timeFormat) return clock.LocalTime().Format(timeFormat)
} }
func sanitizeLocation(locStr string) string {
return strings.Replace(locStr, " ", "_", -1)
}

View File

@ -10,7 +10,9 @@ type ClockCollection struct {
} }
func (clocks *ClockCollection) Sorted(sortOrder string) []Clock { func (clocks *ClockCollection) Sorted(sortOrder string) []Clock {
if sortOrder == "chronological" { if sortOrder == "natural" {
//no-op
} else if sortOrder == "chronological" {
clocks.SortedChronologically() clocks.SortedChronologically()
} else { } else {
clocks.SortedAlphabetically() clocks.SortedAlphabetically()

View File

@ -15,10 +15,10 @@ const (
type Settings struct { type Settings struct {
common *cfg.Common common *cfg.Common
dateFormat string `help:"The format of the date string for all clocks." values:"Any valid Go date layout which is handled by Time.Format. Defaults to Jan 2."` dateFormat string `help:"The format of the date string for all clocks." values:"Any valid Go date layout which is handled by Time.Format. Defaults to Jan 2."`
timeFormat string `help:"The format of the time string for all clocks." values:"Any valid Go time layout which is handled by Time.Format. Defaults to 15:04 MST."` timeFormat string `help:"The format of the time string for all clocks." values:"Any valid Go time layout which is handled by Time.Format. Defaults to 15:04 MST."`
locations map[string]interface{} `help:"Defines the timezones for the world clocks that you want to display. key is a unique label that will be displayed in the UI. value is a timezone name." values:"Any TZ database timezone."` locations []Clock `help:"Defines the timezones for the world clocks that you want to display. key is a unique label that will be displayed in the UI. value is a timezone name." values:"Any TZ database timezone."`
sort string `help:"Defines the display order of the clocks in the widget." values:"'alphabetical' or 'chronological'. 'alphabetical' will sort in acending order by key, 'chronological' will sort in ascending order by date/time."` sort string `help:"Defines the display order of the clocks in the widget." values:"'alphabetical', 'chronological', or 'natural. 'alphabetical' will sort in acending order by key, 'chronological' will sort in ascending order by date/time, 'natural' will keep ordering as per the config."`
} }
// NewSettingsFromYAML creates a new settings instance from a YAML config block // NewSettingsFromYAML creates a new settings instance from a YAML config block
@ -28,9 +28,40 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
dateFormat: ymlConfig.UString("dateFormat", utils.SimpleDateFormat), dateFormat: ymlConfig.UString("dateFormat", utils.SimpleDateFormat),
timeFormat: ymlConfig.UString("timeFormat", utils.SimpleTimeFormat), timeFormat: ymlConfig.UString("timeFormat", utils.SimpleTimeFormat),
locations: ymlConfig.UMap("locations"), locations: buildLocations(ymlConfig),
sort: ymlConfig.UString("sort"), sort: ymlConfig.UString("sort"),
} }
return &settings return &settings
} }
func buildLocations(ymlConfig *config.Config) []Clock {
clocks := []Clock{}
locations, err := ymlConfig.Map("locations")
if err == nil {
for k, v := range locations {
name := k
zone := v.(string)
clock, err := BuildClock(name, zone)
if err == nil {
clocks = append(clocks, clock)
}
}
return clocks
}
listLocations := ymlConfig.UList("locations")
for _, location := range listLocations {
if location, ok := location.(map[string]interface{}); ok {
for k, v := range location {
name := k
zone := v.(string)
clock, err := BuildClock(name, zone)
if err == nil {
clocks = append(clocks, clock)
}
}
}
}
return clocks
}

View File

@ -1,9 +1,6 @@
package clocks package clocks
import ( import (
"strings"
"time"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
) )
@ -28,7 +25,7 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
timeFormat: settings.timeFormat, timeFormat: settings.timeFormat,
} }
widget.clockColl = widget.buildClockCollection(settings.locations) widget.clockColl = widget.buildClockCollection()
return &widget return &widget
} }
@ -45,21 +42,10 @@ func (widget *Widget) Refresh() {
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) buildClockCollection(locData map[string]interface{}) ClockCollection { func (widget *Widget) buildClockCollection() ClockCollection {
clockColl := ClockCollection{} clockColl := ClockCollection{}
for label, locStr := range locData { clockColl.Clocks = widget.settings.locations
timeLoc, err := time.LoadLocation(widget.sanitizeLocation(locStr.(string)))
if err != nil {
continue
}
clockColl.Clocks = append(clockColl.Clocks, NewClock(label, timeLoc))
}
return clockColl return clockColl
} }
func (widget *Widget) sanitizeLocation(locStr string) string {
return strings.Replace(locStr, " ", "_", -1)
}