termui/_examples/grid.go

129 lines
2.5 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
2018-08-17 14:18:16 +08:00
import (
"math"
"time"
2015-10-10 23:20:44 +08:00
2018-08-17 14:18:16 +08:00
ui "github.com/gizak/termui"
)
2015-03-16 03:56:38 +08:00
func main() {
2018-09-07 05:48:09 +08:00
ui.Init()
2015-03-16 03:56:38 +08:00
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"
2018-08-17 14:18:16 +08:00
lc.Data["default"] = sinps
2015-03-16 03:56:38 +08:00
lc.Height = 11
lc.AxesColor = ui.ColorWhite
2018-08-17 14:18:16 +08:00
lc.LineColor["default"] = ui.ColorYellow | ui.AttrBold
2015-03-16 03:56:38 +08:00
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)
ui.Handle("q", func(ui.Event) {
2015-10-10 23:20:44 +08:00
ui.StopLoop()
})
2018-09-07 05:48:09 +08:00
drawTicker := time.NewTicker(time.Second)
drawTickerCount := 1
go func() {
for {
if drawTickerCount > 103 {
ui.StopLoop()
return
}
for _, g := range gs {
g.Percent = (g.Percent + 3) % 100
}
sp.Lines[0].Data = spdata[:100+drawTickerCount]
lc.Data["default"] = sinps[2*drawTickerCount:]
ui.Render(ui.Body)
drawTickerCount++
<-drawTicker.C
2015-10-10 23:20:44 +08:00
}
}()
ui.Handle("<Resize>", func(e ui.Event) {
payload := e.Payload.(ui.Resize)
ui.Body.Width = payload.Width
2015-10-10 23:20:44 +08:00
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
}