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

Clocks are sortable

This commit is contained in:
Chris Cummer 2018-04-19 09:29:49 -07:00
parent 590c83c134
commit 6fab7c0668
4 changed files with 84 additions and 49 deletions

11
clocks/clock.go Normal file
View File

@ -0,0 +1,11 @@
package clocks
import(
"time"
)
type Clock struct {
Label string
LocalTime time.Time
Timezone string
}

View File

@ -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)
})
}

View File

@ -2,31 +2,34 @@ package clocks
import ( import (
"fmt" "fmt"
"strings"
) )
func (widget *Widget) display() { func (widget *Widget) display(clocks []Clock) {
locs := widget.locations(Config.UMap("wtf.mods.clocks.locations")) if len(clocks) == 0 {
if len(locs) == 0 {
fmt.Fprintf(widget.View, "\n%s", " no timezone data available") fmt.Fprintf(widget.View, "\n%s", " no timezone data available")
return return
} }
labels := widget.sortedLabels(locs) str := "\n"
for idx, clock := range clocks {
tzs := []string{} str = str + fmt.Sprintf(
for idx, label := range labels { " [%s]%-12s %-10s %7s[white]\n",
zoneStr := fmt.Sprintf( widget.rowColor(idx),
" [%s]%-12s %-10s %7s[white]", clock.Label,
widget.colorFor(idx), clock.LocalTime.Format(TimeFormat),
label, clock.LocalTime.Format(DateFormat),
locs[label].Format(TimeFormat),
locs[label].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
} }

View File

@ -1,7 +1,6 @@
package clocks package clocks
import ( import (
"sort"
"time" "time"
"github.com/olebedev/config" "github.com/olebedev/config"
@ -33,47 +32,32 @@ func (widget *Widget) Refresh() {
return return
} }
clockColl := widget.buildClockCollection(Config.UMap("wtf.mods.clocks.locations"))
widget.View.Clear() widget.View.Clear()
widget.display() widget.display(clockColl.Sorted())
widget.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) colorFor(idx int) string { func (widget *Widget) buildClockCollection(locData map[string]interface{}) ClockCollection {
rowColor := Config.UString("wtf.mods.clocks.rowcolors.even", "lightblue") clockColl := ClockCollection{}
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))
for label, locStr := range locData {
timeLoc, err := time.LoadLocation(locStr.(string))
if err != nil { if err != nil {
continue continue
} }
times[label] = time.Now().In(tzloc) clock := Clock{
Label: label,
LocalTime: time.Now().In(timeLoc),
Timezone: locStr.(string),
} }
return times clockColl.Clocks = append(clockColl.Clocks, clock)
} }
func (widget *Widget) sortedLabels(locs map[string]time.Time) []string { return clockColl
labels := []string{}
for label, _ := range locs {
labels = append(labels, label)
}
sort.Strings(labels)
return labels
} }