termui/_examples/grid.go

123 lines
2.5 KiB
Go
Raw Normal View History

2017-01-13 23:07:43 -07:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2022-08-28 10:46:41 -06:00
// Use of this source code is governed by a GPLv3 license that can
2015-03-20 14:21:50 -06:00
// 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 (
2019-01-23 21:12:10 -07:00
"log"
2018-08-17 00:18:16 -06:00
"math"
"time"
2015-10-10 09:20:44 -06:00
ui "git.tebitea.media/sashakoshka/termui/v3"
"git.tebitea.media/sashakoshka/termui/v3/widgets"
2018-08-17 00:18:16 -06:00
)
2015-03-15 13:56:38 -06:00
func main() {
2019-01-23 21:12:10 -07:00
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
2015-03-15 13:56:38 -06:00
defer ui.Close()
2019-01-23 21:12:10 -07:00
sinFloat64 := (func() []float64 {
2015-03-15 13:56:38 -06:00
n := 400
2019-01-23 21:12:10 -07:00
data := make([]float64, n)
for i := range data {
data[i] = 1 + math.Sin(float64(i)/5)
2015-03-15 13:56:38 -06:00
}
2019-01-23 21:12:10 -07:00
return data
2015-03-15 13:56:38 -06:00
})()
2019-01-23 21:12:10 -07:00
sl := widgets.NewSparkline()
sl.Data = sinFloat64[:100]
sl.LineColor = ui.ColorCyan
sl.TitleStyle.Fg = ui.ColorWhite
slg := widgets.NewSparklineGroup(sl)
slg.Title = "Sparkline"
2019-01-25 04:05:49 -07:00
lc := widgets.NewPlot()
2019-01-23 21:12:10 -07:00
lc.Title = "braille-mode Line Chart"
lc.Data = append(lc.Data, sinFloat64)
2015-03-15 13:56:38 -06:00
lc.AxesColor = ui.ColorWhite
2019-01-23 21:12:10 -07:00
lc.LineColors[0] = ui.ColorYellow
2015-03-15 13:56:38 -06:00
2019-01-23 21:12:10 -07:00
gs := make([]*widgets.Gauge, 3)
2015-03-20 06:24:48 -06:00
for i := range gs {
2019-01-23 21:12:10 -07:00
gs[i] = widgets.NewGauge()
2015-03-20 06:24:48 -06:00
gs[i].Percent = i * 10
gs[i].BarColor = ui.ColorRed
}
2019-01-23 21:12:10 -07:00
ls := widgets.NewList()
ls.Rows = []string{
2015-03-20 06:24:48 -06:00
"[1] Downloading File 1",
2019-01-23 21:12:10 -07:00
"",
"",
"",
2015-03-20 06:24:48 -06:00
"[2] Downloading File 2",
"",
2019-01-23 21:12:10 -07:00
"",
"",
2015-03-20 06:24:48 -06:00
"[3] Uploading File 3",
}
2019-01-25 04:05:49 -07:00
ls.Border = false
2015-03-15 13:56:38 -06:00
2019-01-23 21:12:10 -07:00
p := widgets.NewParagraph()
p.Text = "<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget"
p.Title = "Demonstration"
grid := ui.NewGrid()
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
grid.Set(
ui.NewRow(1.0/2,
ui.NewCol(1.0/2, slg),
ui.NewCol(1.0/2, lc),
),
ui.NewRow(1.0/2,
ui.NewCol(1.0/4, ls),
ui.NewCol(1.0/4,
ui.NewRow(.9/3, gs[0]),
ui.NewRow(.9/3, gs[1]),
ui.NewRow(1.2/3, gs[2]),
),
ui.NewCol(1.0/2, p),
),
)
ui.Render(grid)
2018-11-28 19:19:34 -07:00
tickerCount := 1
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C
2018-11-28 19:19:34 -07:00
for {
select {
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)
2019-01-23 21:12:10 -07:00
grid.SetRect(0, 0, payload.Width, payload.Height)
2018-11-28 19:19:34 -07:00
ui.Clear()
2019-01-23 21:12:10 -07:00
ui.Render(grid)
2018-11-28 19:19:34 -07:00
}
case <-ticker:
2019-01-23 21:12:10 -07:00
if tickerCount == 100 {
return
}
for _, g := range gs {
g.Percent = (g.Percent + 3) % 100
}
2019-01-23 21:12:10 -07:00
slg.Sparklines[0].Data = sinFloat64[tickerCount : tickerCount+100]
lc.Data[0] = sinFloat64[2*tickerCount:]
ui.Render(grid)
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
}