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

93 lines
1.7 KiB
Go

package status
import (
"fmt"
"strings"
"time"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)
var Config *config.Config
type Widget struct {
wtf.TextWidget
Current int
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" 🎉 Status ", "status"),
Current: 0,
}
widget.addView()
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
_, _, w, _ := widget.View.GetInnerRect()
widget.View.Clear()
fmt.Fprintf(
widget.View,
fmt.Sprintf("%%%ds\n%%%ds", w-2, w-1),
widget.animation(),
widget.timezones(),
)
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) addView() {
view := tview.NewTextView()
view.SetBorder(true)
view.SetBorderColor(tcell.ColorGray)
view.SetDynamicColors(true)
view.SetTitle(widget.Name)
view.SetWrap(false)
widget.View = view
}
func (widget *Widget) animation() string {
icons := []string{"👍", "🤜", "🤙", "🤜", "🤘", "🤜", "✊", "🤜", "👌", "🤜"}
next := icons[widget.Current]
widget.Current = widget.Current + 1
if widget.Current == len(icons) {
widget.Current = 0
}
return next
}
func (widget *Widget) timezones() string {
times := Timezones(wtf.ToStrs(Config.UList("wtf.mods.status.timezones")))
if len(times) == 0 {
return ""
}
formattedTimes := []string{}
for _, time := range times {
formattedTimes = append(formattedTimes, time.Format(wtf.TimeFormat))
}
return strings.Join(formattedTimes, " • ")
}