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

Migrate all modules to their own subfolder

Handles #375
This commit is contained in:
Sean Smith
2019-02-18 11:16:34 -05:00
parent c28c31aedb
commit 8030380f89
123 changed files with 51 additions and 51 deletions

35
modules/clocks/clock.go Normal file
View File

@@ -0,0 +1,35 @@
package clocks
import (
"time"
)
type Clock struct {
Label string
Location *time.Location
}
func NewClock(label string, timeLoc *time.Location) Clock {
clock := Clock{
Label: label,
Location: timeLoc,
}
return clock
}
func (clock *Clock) Date(dateFormat string) string {
return clock.LocalTime().Format(dateFormat)
}
func (clock *Clock) LocalTime() time.Time {
return clock.ToLocal(time.Now())
}
func (clock *Clock) ToLocal(t time.Time) time.Time {
return t.In(clock.Location)
}
func (clock *Clock) Time(timeFormat string) string {
return clock.LocalTime().Format(timeFormat)
}

View File

@@ -0,0 +1,41 @@
package clocks
import (
"sort"
"time"
"github.com/wtfutil/wtf/wtf"
)
type ClockCollection struct {
Clocks []Clock
}
func (clocks *ClockCollection) Sorted() []Clock {
if "chronological" == wtf.Config.UString("wtf.mods.clocks.sort", "alphabetical") {
clocks.SortedChronologically()
} else {
clocks.SortedAlphabetically()
}
return clocks.Clocks
}
func (clocks *ClockCollection) SortedAlphabetically() {
sort.Slice(clocks.Clocks, func(i, j int) bool {
clock := clocks.Clocks[i]
other := clocks.Clocks[j]
return clock.Label < other.Label
})
}
func (clocks *ClockCollection) SortedChronologically() {
now := time.Now()
sort.Slice(clocks.Clocks, func(i, j int) bool {
clock := clocks.Clocks[i]
other := clocks.Clocks[j]
return clock.ToLocal(now).String() < other.ToLocal(now).String()
})
}

27
modules/clocks/display.go Normal file
View File

@@ -0,0 +1,27 @@
package clocks
import (
"fmt"
"github.com/wtfutil/wtf/wtf"
)
func (widget *Widget) display(clocks []Clock, dateFormat string, timeFormat string) {
if len(clocks) == 0 {
widget.View.SetText(fmt.Sprintf("\n%s", " no timezone data available"))
return
}
str := ""
for idx, clock := range clocks {
str = str + fmt.Sprintf(
" [%s]%-12s %-10s %7s[white]\n",
wtf.RowColor("clocks", idx),
clock.Label,
clock.Time(timeFormat),
clock.Date(dateFormat),
)
}
widget.View.SetText(str)
}

57
modules/clocks/widget.go Normal file
View File

@@ -0,0 +1,57 @@
package clocks
import (
"strings"
"time"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
clockColl ClockCollection
dateFormat string
timeFormat string
}
func NewWidget(app *tview.Application) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "World Clocks", "clocks", false),
}
widget.clockColl = widget.buildClockCollection(wtf.Config.UMap("wtf.mods.clocks.locations"))
widget.dateFormat = wtf.Config.UString("wtf.mods.clocks.dateFormat", wtf.SimpleDateFormat)
widget.timeFormat = wtf.Config.UString("wtf.mods.clocks.timeFormat", wtf.SimpleTimeFormat)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
widget.display(widget.clockColl.Sorted(), widget.dateFormat, widget.timeFormat)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) buildClockCollection(locData map[string]interface{}) 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))
}
return clockColl
}
func (widget *Widget) sanitizeLocation(locStr string) string {
return strings.Replace(locStr, " ", "_", -1)
}