diff --git a/clocks/clock.go b/clocks/clock.go new file mode 100644 index 00000000..519b42fe --- /dev/null +++ b/clocks/clock.go @@ -0,0 +1,11 @@ +package clocks + +import( + "time" +) + +type Clock struct { + Label string + LocalTime time.Time + Timezone string +} diff --git a/clocks/clock_collection.go b/clocks/clock_collection.go new file mode 100644 index 00000000..8ba9bd05 --- /dev/null +++ b/clocks/clock_collection.go @@ -0,0 +1,37 @@ +package clocks + +import ( + "sort" +) + +type ClockCollection struct { + Clocks []Clock +} + +func (clockColl *ClockCollection) Sorted() []Clock { + if "chronological" == Config.UString("wtf.mods.clocks.sort", "alphabetical") { + clockColl.SortedChronologically() + } else { + clockColl.SortedAlphabetically() + } + + return clockColl.Clocks +} + +func (clockColl *ClockCollection) SortedAlphabetically() { + sort.Slice(clockColl.Clocks, func(i, j int) bool { + clock := clockColl.Clocks[i] + other := clockColl.Clocks[j] + + return clock.Label < other.Label + }) +} + +func (clockColl *ClockCollection) SortedChronologically() { + sort.Slice(clockColl.Clocks, func(i, j int) bool { + clock := clockColl.Clocks[i] + other := clockColl.Clocks[j] + + return clock.LocalTime.Before(other.LocalTime) + }) +} diff --git a/clocks/display.go b/clocks/display.go index 3eea0298..da238818 100644 --- a/clocks/display.go +++ b/clocks/display.go @@ -2,31 +2,34 @@ package clocks import ( "fmt" - "strings" ) -func (widget *Widget) display() { - locs := widget.locations(Config.UMap("wtf.mods.clocks.locations")) - - if len(locs) == 0 { +func (widget *Widget) display(clocks []Clock) { + if len(clocks) == 0 { fmt.Fprintf(widget.View, "\n%s", " no timezone data available") return } - labels := widget.sortedLabels(locs) - - tzs := []string{} - for idx, label := range labels { - zoneStr := fmt.Sprintf( - " [%s]%-12s %-10s %7s[white]", - widget.colorFor(idx), - label, - locs[label].Format(TimeFormat), - locs[label].Format(DateFormat), + str := "\n" + for idx, clock := range clocks { + str = str + fmt.Sprintf( + " [%s]%-12s %-10s %7s[white]\n", + widget.rowColor(idx), + clock.Label, + clock.LocalTime.Format(TimeFormat), + clock.LocalTime.Format(DateFormat), ) - - tzs = append(tzs, zoneStr) } - fmt.Fprintf(widget.View, "\n%s", strings.Join(tzs, "\n")) + fmt.Fprintf(widget.View, "%s", str) +} + +func (widget *Widget) rowColor(idx int) string { + rowCol := Config.UString("wtf.mods.clocks.rowcolors.even", "lightblue") + + if idx%2 == 0 { + rowCol = Config.UString("wtf.mods.clocks.rowcolors.odd", "white") + } + + return rowCol } diff --git a/clocks/widget.go b/clocks/widget.go index e6bafc7e..457d4a55 100644 --- a/clocks/widget.go +++ b/clocks/widget.go @@ -1,7 +1,6 @@ package clocks import ( - "sort" "time" "github.com/olebedev/config" @@ -33,47 +32,32 @@ func (widget *Widget) Refresh() { return } + clockColl := widget.buildClockCollection(Config.UMap("wtf.mods.clocks.locations")) + widget.View.Clear() - widget.display() + widget.display(clockColl.Sorted()) widget.RefreshedAt = time.Now() } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) colorFor(idx int) string { - rowColor := Config.UString("wtf.mods.clocks.rowcolors.even", "lightblue") - - if idx%2 == 0 { - rowColor = Config.UString("wtf.mods.clocks.rowcolors.odd", "white") - } - - return rowColor -} - -func (widget *Widget) locations(locs map[string]interface{}) map[string]time.Time { - times := make(map[string]time.Time) - - for label, loc := range locs { - tzloc, err := time.LoadLocation(loc.(string)) +func (widget *Widget) buildClockCollection(locData map[string]interface{}) ClockCollection { + clockColl := ClockCollection{} + for label, locStr := range locData { + timeLoc, err := time.LoadLocation(locStr.(string)) if err != nil { continue } - times[label] = time.Now().In(tzloc) + clock := Clock{ + Label: label, + LocalTime: time.Now().In(timeLoc), + Timezone: locStr.(string), + } + + clockColl.Clocks = append(clockColl.Clocks, clock) } - return times -} - -func (widget *Widget) sortedLabels(locs map[string]time.Time) []string { - labels := []string{} - - for label, _ := range locs { - labels = append(labels, label) - } - - sort.Strings(labels) - - return labels + return clockColl }