termui/_example/grid.go

123 lines
2.4 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-03-16 03:56:38 +08:00
package main
import ui "github.com/gizak/termui"
2015-10-10 23:20:44 +08:00
2015-03-16 03:56:38 +08:00
import "math"
func main() {
2015-10-10 23:20:44 +08:00
if err := ui.Init(); err != nil {
2015-03-16 03:56:38 +08:00
panic(err)
}
defer ui.Close()
sinps := (func() []float64 {
n := 400
ps := make([]float64, n)
for i := range ps {
ps[i] = 1 + math.Sin(float64(i)/5)
}
return ps
})()
sinpsint := (func() []int {
ps := make([]int, len(sinps))
for i, v := range sinps {
ps[i] = int(100*v + 10)
}
return ps
})()
spark := ui.Sparkline{}
spark.Height = 8
spdata := sinpsint
spark.Data = spdata[:100]
2015-03-16 03:56:38 +08:00
spark.LineColor = ui.ColorCyan
spark.TitleColor = ui.ColorWhite
sp := ui.NewSparklines(spark)
sp.Height = 11
2015-10-10 23:20:44 +08:00
sp.BorderLabel = "Sparkline"
2015-03-16 03:56:38 +08:00
lc := ui.NewLineChart()
2015-10-10 23:20:44 +08:00
lc.BorderLabel = "braille-mode Line Chart"
2015-03-16 03:56:38 +08:00
lc.Data = sinps
lc.Height = 11
lc.AxesColor = ui.ColorWhite
lc.LineColor = ui.ColorYellow | ui.AttrBold
2015-03-20 20:24:48 +08:00
gs := make([]*ui.Gauge, 3)
for i := range gs {
gs[i] = ui.NewGauge()
2015-10-10 23:20:44 +08:00
//gs[i].LabelAlign = ui.AlignCenter
2015-03-20 20:24:48 +08:00
gs[i].Height = 2
2015-10-10 23:20:44 +08:00
gs[i].Border = false
2015-03-20 20:24:48 +08:00
gs[i].Percent = i * 10
gs[i].PaddingBottom = 1
gs[i].BarColor = ui.ColorRed
}
ls := ui.NewList()
2015-10-10 23:20:44 +08:00
ls.Border = false
2015-03-20 20:24:48 +08:00
ls.Items = []string{
"[1] Downloading File 1",
"", // == \newline
"[2] Downloading File 2",
"",
"[3] Uploading File 3",
}
ls.Height = 5
par := ui.NewPar("<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget")
par.Height = 5
2015-10-10 23:20:44 +08:00
par.BorderLabel = "Demonstration"
2015-03-20 20:24:48 +08:00
// build layout
ui.Body.AddRows(
2015-03-16 03:56:38 +08:00
ui.NewRow(
2015-03-20 20:24:48 +08:00
ui.NewCol(6, 0, sp),
ui.NewCol(6, 0, lc)),
ui.NewRow(
ui.NewCol(3, 0, ls),
ui.NewCol(3, 0, gs[0], gs[1], gs[2]),
ui.NewCol(6, 0, par)))
// calculate layout
ui.Body.Align()
2015-03-16 03:56:38 +08:00
2015-10-10 23:20:44 +08:00
ui.Render(ui.Body)
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)
i := t.Count
if i > 103 {
ui.StopLoop()
return
}
2015-10-10 23:20:44 +08:00
for _, g := range gs {
g.Percent = (g.Percent + 3) % 100
}
2015-03-16 03:56:38 +08:00
2015-10-10 23:20:44 +08:00
sp.Lines[0].Data = spdata[:100+i]
lc.Data = sinps[2*i:]
ui.Render(ui.Body)
})
2015-03-16 03:56:38 +08:00
2015-10-10 23:20:44 +08:00
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
ui.Body.Width = ui.TermWidth()
ui.Body.Align()
ui.Clear()
2015-10-10 23:20:44 +08:00
ui.Render(ui.Body)
})
ui.Loop()
2015-03-16 03:56:38 +08:00
}