Skip to content

Commit

Permalink
Add natural ordering for clocks
Browse files Browse the repository at this point in the history
This turns locations into an ordered array, so we can have a 'natural' ordering
This is backwards compatible
Resolves #896
  • Loading branch information
Seanstoppable committed Oct 3, 2020
1 parent 6cc00f5 commit fcc2a21
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
13 changes: 13 additions & 0 deletions modules/clocks/clock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clocks

import (
"strings"
"time"
)

Expand All @@ -18,6 +19,14 @@ func NewClock(label string, timeLoc *time.Location) 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 {
return clock.LocalTime().Format(dateFormat)
}
Expand All @@ -33,3 +42,7 @@ func (clock *Clock) ToLocal(t time.Time) time.Time {
func (clock *Clock) Time(timeFormat string) string {
return clock.LocalTime().Format(timeFormat)
}

func sanitizeLocation(locStr string) string {
return strings.Replace(locStr, " ", "_", -1)
}
4 changes: 3 additions & 1 deletion modules/clocks/clock_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ type ClockCollection struct {
}

func (clocks *ClockCollection) Sorted(sortOrder string) []Clock {
if sortOrder == "chronological" {
if sortOrder == "natural" {
//no-op
} else if sortOrder == "chronological" {
clocks.SortedChronologically()
} else {
clocks.SortedAlphabetically()
Expand Down
41 changes: 36 additions & 5 deletions modules/clocks/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const (
type Settings struct {
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."`
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."`
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."`
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."`
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', '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
Expand All @@ -28,9 +28,40 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co

dateFormat: ymlConfig.UString("dateFormat", utils.SimpleDateFormat),
timeFormat: ymlConfig.UString("timeFormat", utils.SimpleTimeFormat),
locations: ymlConfig.UMap("locations"),
locations: buildLocations(ymlConfig),
sort: ymlConfig.UString("sort"),
}

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
}
20 changes: 3 additions & 17 deletions modules/clocks/widget.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package clocks

import (
"strings"
"time"

"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
)
Expand All @@ -28,7 +25,7 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
timeFormat: settings.timeFormat,
}

widget.clockColl = widget.buildClockCollection(settings.locations)
widget.clockColl = widget.buildClockCollection()

return &widget
}
Expand All @@ -45,21 +42,10 @@ func (widget *Widget) Refresh() {

/* -------------------- Unexported Functions -------------------- */

func (widget *Widget) buildClockCollection(locData map[string]interface{}) ClockCollection {
func (widget *Widget) buildClockCollection() ClockCollection {
clockColl := ClockCollection{}

for label, locStr := range locData {
timeLoc, err := time.LoadLocation(widget.sanitizeLocation(locStr.(string)))
if err != nil {
continue
}

clockColl.Clocks = append(clockColl.Clocks, NewClock(label, timeLoc))
}
clockColl.Clocks = widget.settings.locations

return clockColl
}

func (widget *Widget) sanitizeLocation(locStr string) string {
return strings.Replace(locStr, " ", "_", -1)
}

0 comments on commit fcc2a21

Please sign in to comment.