-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
123 lines (105 loc) · 2.67 KB
/
app.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
package tchart
import (
"errors"
"github.com/nsf/termbox-go"
ui "github.com/s-westphal/termui/v3"
)
func GetDefaultChartColors() []ui.Color {
return []ui.Color{ui.ColorRed, ui.ColorGreen, ui.ColorYellow, ui.ColorBlue, ui.ColorCyan}
}
type App struct {
*vContainer
panels []panel
widgets []Widget
}
func NewApp() *App {
return &App{
vContainer: newVContainer(),
panels: []panel{},
widgets: []Widget{},
}
}
func (app *App) AddPanel(widgetType string, storages []*Storage) error {
widget, err := CreateWidget(widgetType, "", storages)
if err != nil {
panic(err)
}
var panel = newPanel(storages, widget)
app.vContainer.putContainers(panel.container)
app.panels = append(app.panels, *panel)
return nil
}
func (app *App) AddInstructions() {
instructionsWidget := newInstructionsWidget("")
instructionsContainer := newHContainer(instructionsWidget)
instructionsContainer.setHeight(3)
app.vContainer.putContainers(instructionsContainer)
}
func (app *App) AddWidgetRow(widgets []Widget, height int) {
rowContainer := newHContainer()
for _, w := range widgets {
chartContainer := newVContainer(w)
rowContainer.putContainers(chartContainer)
}
app.widgets = append(app.widgets, widgets...)
if height != 0 {
rowContainer.setHeight(height)
}
app.vContainer.putContainers(rowContainer)
}
// Update update panels
func (app *App) Update() {
for _, panel := range app.panels {
panel.update()
}
for _, widget := range app.widgets {
widget.update()
}
w, h := termbox.Size()
app.vContainer.render(0, 0, w, h)
termbox.Flush()
}
// Render render tchart
func (app *App) Render() {
w, h := termbox.Size()
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
app.vContainer.render(0, 0, w, h)
termbox.Flush()
}
func CreateWidget(widgetType string, title string, storages []*Storage) (Widget, error) {
var widget Widget
switch widgetType {
case "L":
if title == "" {
title = "Line Chart"
}
widget = NewLineChartWidget(title, storages, GetDefaultChartColors())
case "S":
if len(storages) != 2 {
return nil, errors.New("scatter plot needs 2 columns")
}
if title == "" {
title = "Scatter Plot"
}
widget = NewScatterPlotWidget(title, storages)
case "P":
if title == "" {
title = "Pie Chart"
}
widget = NewPieChartWidget(title, storages, GetDefaultChartColors())
case "G":
if len(storages) != 1 {
return nil, errors.New("gauge needs 1 column")
}
widget = NewGaugeWidget(title, storages[0], GetDefaultChartColors())
case "B":
if len(storages) != 1 {
return nil, errors.New("barchart needs 1 column")
}
if title == "" {
title = "Bar Chart"
}
widget = NewBarChartWidget(title, storages[0], 10)
}
return widget, nil
}