forked from emprcl/runal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
209 lines (187 loc) · 4.27 KB
/
run.go
File metadata and controls
209 lines (187 loc) · 4.27 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package runal
import (
"context"
"sync"
"time"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/input"
)
const (
defaultFPS = 30
)
type callbacks struct {
onKey func(c *Canvas, e KeyEvent)
onMouseMove func(c *Canvas, e MouseEvent)
onMouseClick func(c *Canvas, e MouseEvent)
onMouseRelease func(c *Canvas, e MouseEvent)
onMouseWheel func(c *Canvas, e MouseEvent)
}
type callbackOption func(*callbacks)
func WithOnKey(onKey func(c *Canvas, e KeyEvent)) callbackOption {
return func(c *callbacks) {
c.onKey = onKey
}
}
func WithOnMouseMove(onMouseMove func(c *Canvas, e MouseEvent)) callbackOption {
return func(c *callbacks) {
c.onMouseMove = onMouseMove
}
}
func WithOnMouseClick(onMouseClick func(c *Canvas, e MouseEvent)) callbackOption {
return func(c *callbacks) {
c.onMouseClick = onMouseClick
}
}
func WithOnMouseRelease(onMouseRelease func(c *Canvas, e MouseEvent)) callbackOption {
return func(c *callbacks) {
c.onMouseRelease = onMouseRelease
}
}
func WithOnMouseWheel(onMouseWheel func(c *Canvas, e MouseEvent)) callbackOption {
return func(c *callbacks) {
c.onMouseWheel = onMouseWheel
}
}
func Run(ctx context.Context, setup, draw func(c *Canvas), opts ...callbackOption) {
Start(ctx, nil, setup, draw, opts...).Wait()
}
func Start(ctx context.Context, done chan struct{}, setup, draw func(c *Canvas), opts ...callbackOption) *sync.WaitGroup {
if setup == nil {
log.Fatal("setup method is required")
}
if draw == nil {
log.Fatal("draw method is required")
}
ctx, cancel := context.WithCancel(ctx)
w, h := termSize()
c := newCanvas(w, h)
wg := sync.WaitGroup{}
eventCallbacks := callbacks{}
for _, opt := range opts {
opt(&eventCallbacks)
}
ticker := time.NewTicker(newFramerate(defaultFPS))
exit := func() {
ticker.Stop()
resetCursorPosition()
clearScreen()
showCursor()
disableMouse()
}
defer func() {
if r := recover(); r != nil {
exit()
panic(r)
}
}()
resize := listenForResize()
inputEvents := listenForInputEvents(ctx, &wg)
enterAltScreen()
enableMouse()
setup(c)
render := func() {
resetCursorPosition()
draw(c)
c.render()
}
render()
wg.Add(1)
go func() {
defer func() {
exit()
wg.Done()
}()
for {
select {
case <-ctx.Done():
return
case <-resize:
clearScreen()
w, h := termSize()
c.termWidth = w
c.termHeight = h
if c.autoResize {
c.resize(w, h)
}
render()
case event := <-c.bus:
switch event.name {
case "fps":
ticker.Reset(newFramerate(event.value))
case "stop":
ticker.Stop()
case "start":
ticker.Reset(newFramerate(defaultFPS))
case "render":
render()
case "exit":
if done != nil {
done <- struct{}{}
}
cancel()
return
}
case event := <-inputEvents:
switch e := event.(type) {
case input.MouseMotionEvent:
c.setMousePostion(e.X, e.Y)
if eventCallbacks.onMouseMove != nil {
eventCallbacks.onMouseMove(c, MouseEvent{
X: c.MouseX,
Y: c.MouseY,
})
}
case input.MouseClickEvent:
if eventCallbacks.onMouseClick != nil {
c.setMousePostion(e.X, e.Y)
eventCallbacks.onMouseClick(c, MouseEvent{
X: c.MouseX,
Y: c.MouseY,
Button: e.Button.String(),
})
}
case input.MouseReleaseEvent:
if eventCallbacks.onMouseRelease != nil {
c.setMousePostion(e.X, e.Y)
eventCallbacks.onMouseRelease(c, MouseEvent{
X: c.MouseX,
Y: c.MouseY,
Button: e.Button.String(),
})
}
case input.MouseWheelEvent:
if eventCallbacks.onMouseWheel != nil {
c.setMousePostion(e.X, e.Y)
eventCallbacks.onMouseWheel(c, MouseEvent{
X: c.MouseX,
Y: c.MouseY,
Button: e.Button.String(),
})
}
case input.KeyEvent:
switch e.String() {
case "ctrl+c":
if done != nil {
done <- struct{}{}
}
cancel()
return
default:
if eventCallbacks.onKey != nil {
eventCallbacks.onKey(c, KeyEvent{
Key: e.Key().String(),
Code: int(e.Key().Code),
})
}
}
}
case <-ticker.C:
render()
}
}
}()
return &wg
}
func newFramerate(fps int) time.Duration {
return time.Second / time.Duration(fps)
}