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"
|
2018-09-07 07:36:14 +08:00
|
|
|
"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 09:22:04 +08:00
|
|
|
err := ui.Init()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
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
|
2015-05-05 22:55:35 +08:00
|
|
|
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
|
|
|
|
|
2018-11-29 10:24:38 +08: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 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]),
|
2018-11-29 10:24:38 +08:00
|
|
|
ui.NewCol(6, 0, p)))
|
2015-03-20 20:24:48 +08:00
|
|
|
|
|
|
|
// 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-04-20 15:41:55 +08:00
|
|
|
|
2018-11-29 10:19:34 +08:00
|
|
|
tickerCount := 1
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-ui.PollEvent():
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
case <-time.NewTicker(time.Second).C:
|
|
|
|
if tickerCount > 103 {
|
2018-09-07 07:36:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, g := range gs {
|
|
|
|
g.Percent = (g.Percent + 3) % 100
|
|
|
|
}
|
2018-11-29 10:19:34 +08:00
|
|
|
sp.Lines[0].Data = spdata[:100+tickerCount]
|
|
|
|
lc.Data["default"] = sinps[2*tickerCount:]
|
2018-09-07 07:36:14 +08:00
|
|
|
ui.Render(ui.Body)
|
2018-11-29 10:19:34 +08:00
|
|
|
tickerCount++
|
2015-10-10 23:20:44 +08:00
|
|
|
}
|
2018-11-29 10:19:34 +08:00
|
|
|
}
|
2015-03-16 03:56:38 +08:00
|
|
|
}
|