This repository was archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrender.go
174 lines (156 loc) · 6.25 KB
/
render.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package render
import (
"context"
"errors"
"strconv"
"time"
"github.com/ricoberger/dash/pkg/dashboard"
"github.com/ricoberger/dash/pkg/datasource"
fLog "github.com/ricoberger/dash/pkg/log"
"github.com/ricoberger/dash/pkg/render/utils"
"github.com/ricoberger/dash/pkg/render/widget"
"github.com/mum4k/termdash"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/keyboard"
"github.com/mum4k/termdash/terminal/termbox"
"github.com/mum4k/termdash/terminal/terminalapi"
)
var (
ErrNoDashboards = errors.New("no dashboards were provided")
)
func Run(explore bool, datasources map[string]datasource.Client, dashboards []dashboard.Dashboard, initialInterval, initialRefresh string) error {
// Check if there was at least one dashboard provided. This is required for the storage implementation, because we
// choose the first dashboard as the initial one.
// When the check succeeded we create the storage, which holds the current state of dash.
if len(dashboards) == 0 {
return ErrNoDashboards
}
storage, err := utils.NewStorage(explore, datasources, dashboards, initialInterval, initialRefresh)
if err != nil {
return err
}
// Initialize termdash.
// We create the statusbar, modal and the grid. The initial view shows the statusbar and grid. If an item from the
// statusbar is selected the modal content will be rendered instead of the grid.
t, err := termbox.New()
if err != nil {
return err
}
defer t.Close()
statusbar, err := widget.NewStatusbar(t.Size().X, storage)
if err != nil {
return err
}
modal, err := widget.NewModal(storage)
if err != nil {
return err
}
gridOpts := widget.GridLayout(storage)
c, err := container.New(t, container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(gridOpts...), container.SplitFixed(1)), container.ID("layout"))
if err != nil {
return err
}
var modalActive bool
var previousKey keyboard.Key
ctx, cancel := context.WithCancel(context.Background())
ticker := time.NewTicker(storage.GetRefresh())
defer ticker.Stop()
go func() {
for {
select {
case <-ticker.C:
if !modalActive {
fLog.Debugf("refresh was triggered")
storage.RefreshInterval()
gridOpts = widget.GridLayout(storage)
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(gridOpts...), container.SplitFixed(1)))
}
case <-ctx.Done():
return
}
}
}()
keyboardSubscriber := func(k *terminalapi.Keyboard) {
fLog.Debugf("key %s was pressed", k.Key)
switch k.Key {
case 'q', keyboard.KeyCtrlC:
cancel()
case keyboard.KeyEnter:
if modalActive {
modalType, err := modal.Select()
if err == nil {
if modalType == widget.ModalTypeDatasource {
storage.RefreshInterval()
} else if modalType == widget.ModalTypeDashboard {
storage.RefreshInterval()
} else if modalType == widget.ModalTypeVariable {
storage.RefreshInterval()
} else if modalType == widget.ModalTypeInterval {
storage.RefreshInterval()
} else if modalType == widget.ModalTypeRefresh {
ticker = time.NewTicker(storage.GetRefresh())
} else if modalType == widget.ModalTypeExplore {
storage.RefreshInterval()
}
modalActive = false
statusbar.Update(t.Size().X)
gridOpts = widget.GridLayout(storage)
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(gridOpts...), container.SplitFixed(1)))
}
}
case keyboard.KeyF1:
modalActive = modal.Show(&widget.ModalOptions{Type: widget.ModalTypeDashboard, VariableIndex: 0})
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(container.PlaceWidget(modal)), container.SplitFixed(1)))
case keyboard.KeyF2:
modalActive = modal.Show(&widget.ModalOptions{Type: widget.ModalTypeDatasource, VariableIndex: 0})
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(container.PlaceWidget(modal)), container.SplitFixed(1)))
case keyboard.KeyF3:
if explore {
modalActive = modal.Show(&widget.ModalOptions{Type: widget.ModalTypeExplore, VariableIndex: 0})
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(container.PlaceWidget(modal)), container.SplitFixed(1)))
}
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
if explore {
if modalActive {
modal.SelectIndex(string(k.Key))
}
} else {
if modalActive {
modal.SelectIndex(string(k.Key))
} else if previousKey == keyboard.KeyF3 && k.Key != '0' {
variableIndex, err := strconv.Atoi(string(k.Key))
if err == nil && variableIndex <= len(storage.Dashboard().Variables) {
modalActive = modal.Show(&widget.ModalOptions{Type: widget.ModalTypeVariable, VariableIndex: variableIndex - 1})
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(container.PlaceWidget(modal)), container.SplitFixed(1)))
}
}
}
case keyboard.KeyF4:
modalActive = modal.Show(&widget.ModalOptions{Type: widget.ModalTypeInterval, VariableIndex: 0})
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(container.PlaceWidget(modal)), container.SplitFixed(1)))
case keyboard.KeyF5:
modalActive = modal.Show(&widget.ModalOptions{Type: widget.ModalTypeRefresh, VariableIndex: 0})
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(container.PlaceWidget(modal)), container.SplitFixed(1)))
case keyboard.KeyEsc:
modalActive = false
storage.RefreshInterval()
gridOpts = widget.GridLayout(storage)
c.Update("layout", container.SplitHorizontal(container.Top(container.PlaceWidget(statusbar)), container.Bottom(gridOpts...), container.SplitFixed(1)))
case keyboard.KeyBackspace, keyboard.KeyBackspace2, keyboard.KeyDelete:
if modalActive {
modal.RemoveIndex()
}
default:
if explore {
if modalActive {
modal.SelectIndex(string(k.Key))
}
}
}
previousKey = k.Key
}
if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(keyboardSubscriber)); err != nil {
return err
}
return nil
}