termui/_example/dashboard.go

143 lines
3.6 KiB
Go
Raw Normal View History

2017-01-14 14:07:43 +08:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2015-03-21 04:21:50 +08:00
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
// +build ignore
2015-02-05 15:09:20 +08:00
package main
import ui "github.com/gizak/termui"
2015-02-08 09:19:16 +08:00
import "math"
2015-02-05 15:09:20 +08:00
func main() {
2015-10-10 23:20:44 +08:00
if err := ui.Init(); err != nil {
2015-02-05 15:09:20 +08:00
panic(err)
}
defer ui.Close()
p := ui.NewPar(":PRESS q TO QUIT DEMO")
2015-02-08 09:19:16 +08:00
p.Height = 3
p.Width = 50
2015-02-05 15:09:20 +08:00
p.TextFgColor = ui.ColorWhite
2015-10-10 23:20:44 +08:00
p.BorderLabel = "Text Box"
p.BorderFg = ui.ColorCyan
2015-10-14 00:45:03 +08:00
p.Handle("/timer/1s", func(e ui.Event) {
cnt := e.Data.(ui.EvtTimer)
if cnt.Count%2 == 0 {
p.TextFgColor = ui.ColorRed
} else {
p.TextFgColor = ui.ColorWhite
}
})
2015-02-05 15:09:20 +08:00
strs := []string{"[0] gizak/termui", "[1] editbox.go", "[2] iterrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"}
list := ui.NewList()
list.Items = strs
list.ItemFgColor = ui.ColorYellow
2015-10-10 23:20:44 +08:00
list.BorderLabel = "List"
2015-02-05 15:09:20 +08:00
list.Height = 7
list.Width = 25
list.Y = 4
g := ui.NewGauge()
g.Percent = 50
2015-02-08 09:19:16 +08:00
g.Width = 50
2015-02-05 15:09:20 +08:00
g.Height = 3
g.Y = 11
2015-10-10 23:20:44 +08:00
g.BorderLabel = "Gauge"
2015-02-05 15:09:20 +08:00
g.BarColor = ui.ColorRed
2015-10-10 23:20:44 +08:00
g.BorderFg = ui.ColorWhite
g.BorderLabelFg = ui.ColorCyan
2015-02-05 15:09:20 +08:00
2015-02-06 00:03:32 +08:00
spark := ui.Sparkline{}
spark.Height = 1
spark.Title = "srv 0:"
spdata := []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6}
2015-02-06 00:03:32 +08:00
spark.Data = spdata
spark.LineColor = ui.ColorCyan
spark.TitleColor = ui.ColorWhite
spark1 := ui.Sparkline{}
spark1.Height = 1
spark1.Title = "srv 1:"
spark1.Data = spdata
spark1.TitleColor = ui.ColorWhite
spark1.LineColor = ui.ColorRed
sp := ui.NewSparklines(spark, spark1)
2015-02-08 09:19:16 +08:00
sp.Width = 25
sp.Height = 7
2015-10-10 23:20:44 +08:00
sp.BorderLabel = "Sparkline"
2015-02-08 09:19:16 +08:00
sp.Y = 4
sp.X = 25
sinps := (func() []float64 {
n := 220
2015-02-08 09:19:16 +08:00
ps := make([]float64, n)
for i := range ps {
ps[i] = 1 + math.Sin(float64(i)/5)
2015-02-08 09:19:16 +08:00
}
return ps
})()
lc := ui.NewLineChart()
2015-10-10 23:20:44 +08:00
lc.BorderLabel = "dot-mode Line Chart"
2015-02-08 09:19:16 +08:00
lc.Data = sinps
lc.Width = 50
lc.Height = 11
lc.X = 0
lc.Y = 14
lc.AxesColor = ui.ColorWhite
lc.LineColor = ui.ColorRed | ui.AttrBold
lc.Mode = "dot"
bc := ui.NewBarChart()
bcdata := []int{3, 2, 5, 3, 9, 5, 3, 2, 5, 8, 3, 2, 4, 5, 3, 2, 5, 7, 5, 3, 2, 6, 7, 4, 6, 3, 6, 7, 8, 3, 6, 4, 5, 3, 2, 4, 6, 4, 8, 5, 9, 4, 3, 6, 5, 3, 6}
bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"}
2015-10-10 23:20:44 +08:00
bc.BorderLabel = "Bar Chart"
2015-02-08 09:19:16 +08:00
bc.Width = 26
bc.Height = 10
bc.X = 51
bc.Y = 0
bc.DataLabels = bclabels
bc.BarColor = ui.ColorGreen
bc.NumColor = ui.ColorBlack
lc1 := ui.NewLineChart()
2015-10-10 23:20:44 +08:00
lc1.BorderLabel = "braille-mode Line Chart"
lc1.Data = sinps
2015-02-08 09:19:16 +08:00
lc1.Width = 26
lc1.Height = 11
lc1.X = 51
lc1.Y = 14
lc1.AxesColor = ui.ColorWhite
lc1.LineColor = ui.ColorYellow | ui.AttrBold
p1 := ui.NewPar("Hey!\nI am a borderless block!")
2015-10-10 23:20:44 +08:00
p1.Border = false
2015-02-08 09:19:16 +08:00
p1.Width = 26
p1.Height = 2
p1.TextFgColor = ui.ColorMagenta
p1.X = 52
p1.Y = 11
2015-02-06 00:03:32 +08:00
2015-02-05 15:09:20 +08:00
draw := func(t int) {
g.Percent = t % 101
list.Items = strs[t%9:]
sp.Lines[0].Data = spdata[:30+t%50]
sp.Lines[1].Data = spdata[:35+t%50]
lc.Data = sinps[t/2%220:]
lc1.Data = sinps[2*t%220:]
2015-02-08 09:19:16 +08:00
bc.Data = bcdata[t/2%10:]
ui.Render(p, list, g, sp, lc, bc, lc1, p1)
2015-02-05 15:09:20 +08:00
}
2015-10-10 23:20:44 +08:00
ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("/timer/1s", func(e ui.Event) {
t := e.Data.(ui.EvtTimer)
draw(int(t.Count))
})
ui.Loop()
2015-02-05 15:09:20 +08:00
}