mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Built Graph Widget, working with basic sample
This commit is contained in:
95
bargraph/widget.go
Normal file
95
bargraph/widget.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package bargraph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
// Config is a pointer to the global config object
|
||||
var Config *config.Config
|
||||
|
||||
var started = false
|
||||
var ok = true
|
||||
|
||||
// Widget define wtf widget to register widget later
|
||||
type Widget struct {
|
||||
wtf.GraphWidget
|
||||
|
||||
// time interval for send http request
|
||||
updateInterval int
|
||||
}
|
||||
|
||||
// NewWidget Make new instance of widget
|
||||
func NewWidget() *Widget {
|
||||
widget := Widget{
|
||||
GraphWidget: wtf.NewGraphWidget(" Bar Graph", "bargraph", false),
|
||||
}
|
||||
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetWordWrap(true)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// MakeGraph - Load the dead drop stats
|
||||
func MakeGraph(widget *Widget) {
|
||||
|
||||
//this could come from config
|
||||
const lineCount = 20
|
||||
var stats [lineCount][2]int64
|
||||
|
||||
for i := lineCount - 1; i >= 0; i-- {
|
||||
|
||||
stats[i][1] = time.Now().AddDate(0, 0, i*-1).Unix() * 1000
|
||||
stats[i][0] = int64(rand.Intn(120-5) + 5)
|
||||
|
||||
}
|
||||
|
||||
widget.GraphWidget.BuildBars(20, "🌟", stats[:])
|
||||
|
||||
}
|
||||
|
||||
// Refresh & update after interval time
|
||||
func (widget *Widget) Refresh() {
|
||||
|
||||
if widget.Disabled() {
|
||||
return
|
||||
}
|
||||
|
||||
if started == false {
|
||||
// this code should run once
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Duration(widget.updateInterval) * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
started = true
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.View.Clear()
|
||||
|
||||
if !ok {
|
||||
widget.View.SetText(
|
||||
fmt.Sprint("Error!"),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
display(widget)
|
||||
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func display(widget *Widget) {
|
||||
MakeGraph(widget)
|
||||
}
|
||||
Reference in New Issue
Block a user