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

Add Clocks module

This commit is contained in:
Chris Cummer 2018-04-18 14:14:03 -07:00
parent 9ec1b516c1
commit a9309ccec5
5 changed files with 113 additions and 56 deletions

21
clocks/timezones.go Normal file
View File

@ -0,0 +1,21 @@
package clocks
import (
"time"
)
func Timezones(locations map[string]interface{}) map[string]time.Time {
times := make(map[string]time.Time)
for label, location := range locations {
tzloc, err := time.LoadLocation(location.(string))
if err != nil {
continue
}
times[label] = time.Now().In(tzloc)
}
return times
}

79
clocks/widget.go Normal file
View File

@ -0,0 +1,79 @@
package clocks
import (
"fmt"
"sort"
"strings"
"time"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
const TimeFormat = "15:04 MST"
const DateFormat = "Jan 2"
// Config is a pointer to the global config object
var Config *config.Config
type Widget struct {
wtf.TextWidget
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" 🕗 World Clocks ", "clocks"),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.View.Clear()
fmt.Fprintf(widget.View, "\n%s", widget.locations())
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) locations() string {
timezones := Timezones(Config.UMap("wtf.mods.clocks.locations"))
if len(timezones) == 0 {
return ""
}
// All this is to display the time entries in alphabetical order
labels := []string{}
for label, _ := range timezones {
labels = append(labels, label)
}
sort.Strings(labels)
tzs := []string{}
for idx, label := range labels {
rowColor := Config.UString("wtf.mods.clocks.rowcolors.even", "lightblue")
if idx%2 == 0 {
rowColor = Config.UString("wtf.mods.clocks.rowcolors.odd", "white")
}
zoneStr := fmt.Sprintf(
" [%s]%-12s %-10s %7s[white]",
rowColor, label,
timezones[label].Format(TimeFormat),
timezones[label].Format(DateFormat),
)
tzs = append(tzs, zoneStr)
}
return strings.Join(tzs, "\n")
}

View File

@ -1,21 +0,0 @@
package status
import (
"time"
)
func Timezones(timezones map[string]interface{}) map[string]time.Time {
times := make(map[string]time.Time)
for label, timezone := range timezones {
tzloc, err := time.LoadLocation(timezone.(string))
if err != nil {
continue
}
times[label] = time.Now().In(tzloc)
}
return times
}

View File

@ -1,9 +1,9 @@
package status
import (
"fmt"
"sort"
"strings"
//"fmt"
//"sort"
//"strings"
"time"
"github.com/olebedev/config"
@ -35,15 +35,14 @@ func (widget *Widget) Refresh() {
return
}
// FIXME: Use two calls to wtf.RightAlign here and get rid of this code duplication
_, _, w, _ := widget.View.GetInnerRect()
//_, _, w, _ := widget.View.GetInnerRect()
widget.View.Clear()
fmt.Fprintf(
widget.View,
fmt.Sprintf("\n%%%ds", w-1),
widget.timezones(),
)
//widget.View.Clear()
//fmt.Fprintf(
//widget.View,
//fmt.Sprintf("\n%%%ds", w-1),
//widget.timezones(),
//)
widget.RefreshedAt = time.Now()
}
@ -61,27 +60,3 @@ func (widget *Widget) animation() string {
return next
}
func (widget *Widget) timezones() string {
timezones := Timezones(Config.UMap("wtf.mods.status.timezones"))
if len(timezones) == 0 {
return ""
}
// All this is to display the time entries in alphabetical order
labels := []string{}
for label, _ := range timezones {
labels = append(labels, label)
}
sort.Strings(labels)
tzs := []string{}
for _, label := range labels {
zoneStr := fmt.Sprintf("%s %s", label, timezones[label].Format(wtf.TimeFormat))
tzs = append(tzs, zoneStr)
}
return strings.Join(tzs, " ◦ ")
}

3
wtf.go
View File

@ -10,6 +10,7 @@ import (
"github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/bamboohr"
"github.com/senorprogrammer/wtf/clocks"
"github.com/senorprogrammer/wtf/gcal"
"github.com/senorprogrammer/wtf/git"
"github.com/senorprogrammer/wtf/github"
@ -115,6 +116,7 @@ func main() {
wtf.Config = Config
bamboohr.Config = Config
clocks.Config = Config
gcal.Config = Config
git.Config = Config
github.Config = Config
@ -128,6 +130,7 @@ func main() {
Widgets = []wtf.TextViewer{
bamboohr.NewWidget(),
clocks.NewWidget(),
gcal.NewWidget(),
git.NewWidget(),
github.NewWidget(),