mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
All widgets now refresh their own data using their own internal go routine. This allows them to set their own update schedule (where RefreshInterval is the time in seconds between refreshes). The app uses a goroutine to redraw itself once a second.
70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package status
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/rivo/tview"
|
|
)
|
|
|
|
type Widget struct {
|
|
RefreshedAt time.Time
|
|
RefreshInterval int
|
|
View *tview.TextView
|
|
}
|
|
|
|
func NewWidget() *Widget {
|
|
widget := Widget{
|
|
RefreshedAt: time.Now(),
|
|
RefreshInterval: 1,
|
|
}
|
|
|
|
widget.addView()
|
|
go widget.refresher()
|
|
|
|
return &widget
|
|
}
|
|
|
|
/* -------------------- Exported Functions -------------------- */
|
|
|
|
func (widget *Widget) Refresh() {
|
|
widget.View.SetTitle(" 🦊 Status ")
|
|
widget.RefreshedAt = time.Now()
|
|
|
|
widget.View.Clear()
|
|
fmt.Fprintf(widget.View, "%s", widget.contentFrom())
|
|
}
|
|
|
|
/* -------------------- Unexported Functions -------------------- */
|
|
|
|
func (widget *Widget) addView() {
|
|
view := tview.NewTextView()
|
|
|
|
view.SetBorder(true)
|
|
view.SetDynamicColors(true)
|
|
view.SetTitle(" BambooHR ")
|
|
|
|
widget.View = view
|
|
}
|
|
|
|
func (widget *Widget) contentFrom() string {
|
|
//return "cats and gods\ndogs and tacs"
|
|
return fmt.Sprint(rand.Intn(100))
|
|
}
|
|
|
|
func (widget *Widget) refresher() {
|
|
tick := time.NewTicker(time.Duration(widget.RefreshInterval) * time.Second)
|
|
quit := make(chan struct{})
|
|
|
|
for {
|
|
select {
|
|
case <-tick.C:
|
|
widget.Refresh()
|
|
case <-quit:
|
|
tick.Stop()
|
|
return
|
|
}
|
|
}
|
|
}
|