diff --git a/clocks/timezones.go b/clocks/timezones.go new file mode 100644 index 00000000..86f3613a --- /dev/null +++ b/clocks/timezones.go @@ -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 +} diff --git a/clocks/widget.go b/clocks/widget.go new file mode 100644 index 00000000..e7036fe2 --- /dev/null +++ b/clocks/widget.go @@ -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") +} diff --git a/status/timezones.go b/status/timezones.go deleted file mode 100644 index 8a9a5a48..00000000 --- a/status/timezones.go +++ /dev/null @@ -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 -} diff --git a/status/widget.go b/status/widget.go index d919fc4f..a42b0480 100644 --- a/status/widget.go +++ b/status/widget.go @@ -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, " ◦ ") -} diff --git a/wtf.go b/wtf.go index 5af139ab..93026d0c 100644 --- a/wtf.go +++ b/wtf.go @@ -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(),