2017-01-13 23:07:43 -07:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-03-20 14:21:50 -06:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
2015-03-15 13:56:38 -06:00
|
|
|
package main
|
|
|
|
|
2018-08-17 00:18:16 -06:00
|
|
|
import (
|
|
|
|
"math"
|
2018-09-06 17:36:14 -06:00
|
|
|
"time"
|
2015-10-10 09:20:44 -06:00
|
|
|
|
2018-08-17 00:18:16 -06:00
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
2015-03-15 13:56:38 -06:00
|
|
|
|
|
|
|
func main() {
|
2018-09-06 19:22:04 -06:00
|
|
|
err := ui.Init()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-03-15 13:56:38 -06: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
|
2015-05-05 08:55:35 -06:00
|
|
|
spark.Data = spdata[:100]
|
2015-03-15 13:56:38 -06:00
|
|
|
spark.LineColor = ui.ColorCyan
|
|
|
|
spark.TitleColor = ui.ColorWhite
|
|
|
|
|
|
|
|
sp := ui.NewSparklines(spark)
|
|
|
|
sp.Height = 11
|
2015-10-10 09:20:44 -06:00
|
|
|
sp.BorderLabel = "Sparkline"
|
2015-03-15 13:56:38 -06:00
|
|
|
|
|
|
|
lc := ui.NewLineChart()
|
2015-10-10 09:20:44 -06:00
|
|
|
lc.BorderLabel = "braille-mode Line Chart"
|
2018-08-17 00:18:16 -06:00
|
|
|
lc.Data["default"] = sinps
|
2015-03-15 13:56:38 -06:00
|
|
|
lc.Height = 11
|
|
|
|
lc.AxesColor = ui.ColorWhite
|
2018-08-17 00:18:16 -06:00
|
|
|
lc.LineColor["default"] = ui.ColorYellow | ui.AttrBold
|
2015-03-15 13:56:38 -06:00
|
|
|
|
2015-03-20 06:24:48 -06:00
|
|
|
gs := make([]*ui.Gauge, 3)
|
|
|
|
for i := range gs {
|
|
|
|
gs[i] = ui.NewGauge()
|
2015-10-10 09:20:44 -06:00
|
|
|
//gs[i].LabelAlign = ui.AlignCenter
|
2015-03-20 06:24:48 -06:00
|
|
|
gs[i].Height = 2
|
2015-10-10 09:20:44 -06:00
|
|
|
gs[i].Border = false
|
2015-03-20 06:24:48 -06:00
|
|
|
gs[i].Percent = i * 10
|
|
|
|
gs[i].PaddingBottom = 1
|
|
|
|
gs[i].BarColor = ui.ColorRed
|
|
|
|
}
|
|
|
|
|
|
|
|
ls := ui.NewList()
|
2015-10-10 09:20:44 -06:00
|
|
|
ls.Border = false
|
2015-03-20 06:24:48 -06:00
|
|
|
ls.Items = []string{
|
|
|
|
"[1] Downloading File 1",
|
|
|
|
"", // == \newline
|
|
|
|
"[2] Downloading File 2",
|
|
|
|
"",
|
|
|
|
"[3] Uploading File 3",
|
|
|
|
}
|
|
|
|
ls.Height = 5
|
|
|
|
|
2018-11-28 19:24:38 -07:00
|
|
|
p := ui.NewParagraph("<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget")
|
|
|
|
p.Height = 5
|
|
|
|
p.BorderLabel = "Demonstration"
|
2015-03-20 06:24:48 -06:00
|
|
|
|
|
|
|
// build layout
|
|
|
|
ui.Body.AddRows(
|
2015-03-15 13:56:38 -06:00
|
|
|
ui.NewRow(
|
2015-03-20 06:24:48 -06: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]),
|
2018-11-28 19:24:38 -07:00
|
|
|
ui.NewCol(6, 0, p)))
|
2015-03-20 06:24:48 -06:00
|
|
|
|
|
|
|
// calculate layout
|
|
|
|
ui.Body.Align()
|
2015-03-15 13:56:38 -06:00
|
|
|
|
2015-10-10 09:20:44 -06:00
|
|
|
ui.Render(ui.Body)
|
2015-04-20 01:41:55 -06:00
|
|
|
|
2018-11-28 19:19:34 -07:00
|
|
|
tickerCount := 1
|
2018-11-29 16:32:42 -07:00
|
|
|
uiEvents := ui.PollEvents()
|
|
|
|
ticker := time.NewTicker(time.Second).C
|
2018-11-28 19:19:34 -07:00
|
|
|
for {
|
|
|
|
select {
|
2018-11-29 16:32:42 -07:00
|
|
|
case e := <-uiEvents:
|
2018-11-28 19:19:34 -07:00
|
|
|
switch e.ID {
|
|
|
|
case "q", "<C-c>":
|
|
|
|
return
|
|
|
|
case "<Resize>":
|
|
|
|
payload := e.Payload.(ui.Resize)
|
|
|
|
ui.Body.Width = payload.Width
|
|
|
|
ui.Body.Align()
|
|
|
|
ui.Clear()
|
|
|
|
ui.Render(ui.Body)
|
|
|
|
}
|
2018-11-29 16:32:42 -07:00
|
|
|
case <-ticker:
|
2018-11-28 19:19:34 -07:00
|
|
|
if tickerCount > 103 {
|
2018-09-06 17:36:14 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, g := range gs {
|
|
|
|
g.Percent = (g.Percent + 3) % 100
|
|
|
|
}
|
2018-11-28 19:19:34 -07:00
|
|
|
sp.Lines[0].Data = spdata[:100+tickerCount]
|
|
|
|
lc.Data["default"] = sinps[2*tickerCount:]
|
2018-09-06 17:36:14 -06:00
|
|
|
ui.Render(ui.Body)
|
2018-11-28 19:19:34 -07:00
|
|
|
tickerCount++
|
2015-10-10 09:20:44 -06:00
|
|
|
}
|
2018-11-28 19:19:34 -07:00
|
|
|
}
|
2015-03-15 13:56:38 -06:00
|
|
|
}
|