forked from sqshq/sampler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
123 lines (105 loc) · 2.77 KB
/
handler.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 event
import (
ui "github.com/gizak/termui/v3"
"github.com/sqshq/sampler/component/layout"
"github.com/sqshq/sampler/config"
"github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data"
"time"
)
const (
refreshRateToRenderRateRatio = 0.5
)
type Handler struct {
samplers []*data.Sampler
options config.Options
layout *layout.Layout
renderTicker *time.Ticker
consoleEvents <-chan ui.Event
renderRate time.Duration
}
func NewHandler(samplers []*data.Sampler, options config.Options, layout *layout.Layout) *Handler {
renderRate := calcMinRenderRate(layout)
return &Handler{
samplers: samplers,
options: options,
layout: layout,
consoleEvents: ui.PollEvents(),
renderTicker: time.NewTicker(renderRate),
renderRate: renderRate,
}
}
func (h *Handler) HandleEvents() {
// initial render
ui.Render(h.layout)
for {
select {
case mode := <-h.layout.ChangeModeEvents:
h.handleModeChange(mode)
case <-h.renderTicker.C:
ui.Render(h.layout)
case e := <-h.consoleEvents:
switch e.ID {
case console.SignalClick:
payload := e.Payload.(ui.Mouse)
h.layout.HandleMouseClick(payload.X, payload.Y)
case console.KeyQuit1, console.KeyQuit2, console.KeyQuit3:
if h.layout.WerePositionsChanged() {
h.updateConfigFile()
}
return
case console.SignalResize:
payload := e.Payload.(ui.Resize)
h.layout.ChangeDimensions(payload.Width, payload.Height)
default:
h.layout.HandleKeyboardEvent(e.ID)
}
}
}
}
func (h *Handler) handleModeChange(m layout.Mode) {
// render the change before switching the tickers
ui.Render(h.layout)
h.renderTicker.Stop()
switch m {
case layout.ModeDefault:
h.renderTicker = time.NewTicker(h.renderRate)
h.pause(false)
case layout.ModePause:
h.pause(true)
// proceed with stopped timer
default:
h.renderTicker = time.NewTicker(console.MinRenderInterval)
h.pause(false)
}
}
func (h *Handler) pause(pause bool) {
for _, s := range h.samplers {
s.Pause(pause)
}
}
func (h *Handler) updateConfigFile() {
var settings []config.ComponentSettings
for _, c := range h.layout.Components {
settings = append(settings,
config.ComponentSettings{Type: c.Type, Title: c.Title, Size: c.Size, Location: c.Location})
}
config.Update(settings, h.options)
}
func calcMinRenderRate(layout *layout.Layout) time.Duration {
minRateMs := layout.Components[0].RateMs
for _, c := range layout.Components {
if c.RateMs < minRateMs {
minRateMs = c.RateMs
}
}
renderRate := time.Duration(
int(float64(minRateMs)*refreshRateToRenderRateRatio)) * time.Millisecond
if renderRate < console.MinRenderInterval {
return console.MinRenderInterval
}
if renderRate > console.MaxRenderInterval {
return console.MaxRenderInterval
}
return renderRate
}