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 (
"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
}

View File

@ -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),
}
return times
}
func (widget *Widget) sortedLabels(locs map[string]time.Time) []string {
labels := []string{}
for label, _ := range locs {
labels = append(labels, label)
clockColl.Clocks = append(clockColl.Clocks, clock)
}
sort.Strings(labels)
return labels
return clockColl
}