-
Notifications
You must be signed in to change notification settings - Fork 786
/
demo.go
153 lines (130 loc) · 3.78 KB
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
// +build ignore
package main
import (
"log"
"math"
"time"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
func main() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
p := widgets.NewParagraph()
p.Title = "Text Box"
p.Text = "PRESS q TO QUIT DEMO"
p.SetRect(0, 0, 50, 5)
p.TextStyle.Fg = ui.ColorWhite
p.BorderStyle.Fg = ui.ColorCyan
updateParagraph := func(count int) {
if count%2 == 0 {
p.TextStyle.Fg = ui.ColorRed
} else {
p.TextStyle.Fg = ui.ColorWhite
}
}
listData := []string{
"[0] gizak/termui",
"[1] editbox.go",
"[2] interrupt.go",
"[3] keyboard.go",
"[4] output.go",
"[5] random_out.go",
"[6] dashboard.go",
"[7] nsf/termbox-go",
}
l := widgets.NewList()
l.Title = "List"
l.Rows = listData
l.SetRect(0, 5, 25, 12)
l.TextStyle.Fg = ui.ColorYellow
g := widgets.NewGauge()
g.Title = "Gauge"
g.Percent = 50
g.SetRect(0, 12, 50, 15)
g.BarColor = ui.ColorRed
g.BorderStyle.Fg = ui.ColorWhite
g.TitleStyle.Fg = ui.ColorCyan
sparklineData := []float64{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6}
sl := widgets.NewSparkline()
sl.Title = "srv 0:"
sl.Data = sparklineData
sl.LineColor = ui.ColorCyan
sl.TitleStyle.Fg = ui.ColorWhite
sl2 := widgets.NewSparkline()
sl2.Title = "srv 1:"
sl2.Data = sparklineData
sl2.TitleStyle.Fg = ui.ColorWhite
sl2.LineColor = ui.ColorRed
slg := widgets.NewSparklineGroup(sl, sl2)
slg.Title = "Sparkline"
slg.SetRect(25, 5, 50, 12)
sinData := (func() []float64 {
n := 220
ps := make([]float64, n)
for i := range ps {
ps[i] = 1 + math.Sin(float64(i)/5)
}
return ps
})()
lc := widgets.NewPlot()
lc.Title = "dot-marker Line Chart"
lc.Data = make([][]float64, 1)
lc.Data[0] = sinData
lc.SetRect(0, 15, 50, 25)
lc.AxesColor = ui.ColorWhite
lc.LineColors[0] = ui.ColorRed
lc.Marker = widgets.MarkerDot
barchartData := []float64{3, 2, 5, 3, 9, 5, 3, 2, 5, 8, 3, 2, 4, 5, 3, 2, 5, 7, 5, 3, 2, 6, 7, 4, 6, 3, 6, 7, 8, 3, 6, 4, 5, 3, 2, 4, 6, 4, 8, 5, 9, 4, 3, 6, 5, 3, 6}
bc := widgets.NewBarChart()
bc.Title = "Bar Chart"
bc.SetRect(50, 0, 75, 10)
bc.Labels = []string{"S0", "S1", "S2", "S3", "S4", "S5"}
bc.BarColors[0] = ui.ColorGreen
bc.NumStyles[0] = ui.NewStyle(ui.ColorBlack)
lc2 := widgets.NewPlot()
lc2.Title = "braille-mode Line Chart"
lc2.Data = make([][]float64, 1)
lc2.Data[0] = sinData
lc2.SetRect(50, 15, 75, 25)
lc2.AxesColor = ui.ColorWhite
lc2.LineColors[0] = ui.ColorYellow
p2 := widgets.NewParagraph()
p2.Text = "Hey!\nI am a borderless block!"
p2.Border = false
p2.SetRect(50, 10, 75, 10)
p2.TextStyle.Fg = ui.ColorMagenta
draw := func(count int) {
g.Percent = count % 101
l.Rows = listData[count%9:]
slg.Sparklines[0].Data = sparklineData[:30+count%50]
slg.Sparklines[1].Data = sparklineData[:35+count%50]
lc.Data[0] = sinData[count/2%220:]
lc2.Data[0] = sinData[2*count%220:]
bc.Data = barchartData[count/2%10:]
ui.Render(p, l, g, slg, lc, bc, lc2, p2)
}
tickerCount := 1
draw(tickerCount)
tickerCount++
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C
for {
select {
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
}
case <-ticker:
updateParagraph(tickerCount)
draw(tickerCount)
tickerCount++
}
}
}