2017-02-17 16:41:56 +00:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-01-24 04:12:10 +00:00
|
|
|
"log"
|
2017-02-17 16:41:56 +00:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
2018-08-17 03:22:32 +00:00
|
|
|
|
2019-03-07 10:50:20 +00:00
|
|
|
ui "github.com/gizak/termui/v3"
|
|
|
|
"github.com/gizak/termui/v3/widgets"
|
2017-02-17 16:41:56 +00:00
|
|
|
)
|
|
|
|
|
2019-01-24 04:12:10 +00:00
|
|
|
var run = true
|
|
|
|
|
2017-02-17 16:41:56 +00:00
|
|
|
func main() {
|
2019-01-24 04:12:10 +00:00
|
|
|
if err := ui.Init(); err != nil {
|
|
|
|
log.Fatalf("failed to initialize termui: %v", err)
|
2018-09-07 01:22:04 +00:00
|
|
|
}
|
2018-09-06 21:48:09 +00:00
|
|
|
defer ui.Close()
|
|
|
|
|
2017-02-17 16:41:56 +00:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
randomDataAndOffset := func() (data []float64, offset float64) {
|
|
|
|
noSlices := 1 + rand.Intn(5)
|
|
|
|
data = make([]float64, noSlices)
|
|
|
|
for i := range data {
|
|
|
|
data[i] = rand.Float64()
|
|
|
|
}
|
|
|
|
offset = 2.0 * math.Pi * rand.Float64()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-24 04:12:10 +00:00
|
|
|
pc := widgets.NewPieChart()
|
|
|
|
pc.Title = "Pie Chart"
|
|
|
|
pc.SetRect(5, 5, 70, 36)
|
2017-02-17 16:41:56 +00:00
|
|
|
pc.Data = []float64{.25, .25, .25, .25}
|
2019-02-24 01:15:42 +00:00
|
|
|
pc.AngleOffset = -.5 * math.Pi
|
|
|
|
pc.LabelFormatter = func(i int, v float64) string {
|
2017-02-17 16:41:56 +00:00
|
|
|
return fmt.Sprintf("%.02f", v)
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:34 +00:00
|
|
|
pause := func() {
|
2017-02-17 16:41:56 +00:00
|
|
|
run = !run
|
|
|
|
if run {
|
2019-01-24 04:12:10 +00:00
|
|
|
pc.Title = "Pie Chart"
|
2017-02-17 16:41:56 +00:00
|
|
|
} else {
|
2019-01-24 04:12:10 +00:00
|
|
|
pc.Title = "Pie Chart (Stopped)"
|
2017-02-17 16:41:56 +00:00
|
|
|
}
|
2018-09-06 21:48:09 +00:00
|
|
|
ui.Render(pc)
|
2018-11-29 02:19:34 +00:00
|
|
|
}
|
2018-09-06 21:48:09 +00:00
|
|
|
|
|
|
|
ui.Render(pc)
|
|
|
|
|
2018-11-29 23:32:42 +00:00
|
|
|
uiEvents := ui.PollEvents()
|
|
|
|
ticker := time.NewTicker(time.Second).C
|
2018-11-29 02:19:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-11-29 23:32:42 +00:00
|
|
|
case e := <-uiEvents:
|
2018-11-29 02:19:34 +00:00
|
|
|
switch e.ID {
|
|
|
|
case "q", "<C-c>":
|
|
|
|
return
|
|
|
|
case "s":
|
|
|
|
pause()
|
|
|
|
}
|
2018-11-29 23:32:42 +00:00
|
|
|
case <-ticker:
|
2018-11-29 02:19:34 +00:00
|
|
|
if run {
|
2019-02-24 01:15:42 +00:00
|
|
|
pc.Data, pc.AngleOffset = randomDataAndOffset()
|
2018-11-29 02:19:34 +00:00
|
|
|
ui.Render(pc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 16:41:56 +00:00
|
|
|
}
|