forked from VladimirMarkelov/clui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainloop.go
63 lines (53 loc) · 1.14 KB
/
mainloop.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
package clui
import (
term "github.com/nsf/termbox-go"
)
// Composer is a service object that manages Views and console, processes
// events, and provides service methods. One application must have only
// one object of this type
type mainLoop struct {
// a channel to communicate with View(e.g, Views send redraw event to this channel)
channel chan Event
}
var (
loop *mainLoop
)
func initMainLoop() {
loop = new(mainLoop)
loop.channel = make(chan Event)
}
// MainLoop starts the main application event loop
func MainLoop() {
RefreshScreen()
eventQueue := make(chan term.Event)
go func() {
for {
eventQueue <- term.PollEvent()
}
}()
for {
RefreshScreen()
select {
case ev := <-eventQueue:
switch ev.Type {
case term.EventError:
panic(ev.Err)
default:
ProcessEvent(termboxEventToLocal(ev))
}
case cmd := <-loop.channel:
if cmd.Type == EventQuit {
return
}
ProcessEvent(cmd)
}
}
}
func _putEvent(ev Event) {
loop.channel <- ev
}
// PutEvent send event to a Composer directly.
// Used by Views to ask for repainting or for quitting the application
func PutEvent(ev Event) {
go _putEvent(ev)
}