-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
140 lines (117 loc) · 2.16 KB
/
main.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
package main
import (
"os"
"github.com/gdamore/tcell"
"github.com/reusee/li/li"
)
type (
Scope = li.Scope
any = interface{}
)
func main() {
// scope
var scope Scope
funcCalls := make(chan any, 128)
var derives []any
scope = li.NewGlobal(
func() li.Derive {
return func(inits ...any) {
derives = append(derives, inits...)
}
},
func() li.RunInMainLoop {
return func(fn any) {
funcCalls <- fn
}
},
)
// open files
views := make(li.Views)
scope = scope.Fork(func() li.Views { return views })
scope.Call(func(
newBuffers li.NewBuffersFromPath,
newView li.NewViewFromBuffer,
) {
for _, path := range os.Args[1:] {
var buffers []*li.Buffer
var err error
buffers, err = newBuffers(path)
if err != nil {
return
}
for _, buffer := range buffers {
_, err := newView(buffer)
ce(err)
}
}
})
var screen li.Screen
defer func() {
screen.Fini()
}()
scope.Assign(&screen)
screenEvents := make(chan li.ScreenEvent, 128)
go func() {
for {
ev := screen.PollEvent()
if mouse, ok := ev.(*tcell.EventMouse); ok && mouse.Buttons() == 0 {
// no mouse mouve events
continue
}
screenEvents <- ev
}
}()
// panic handling
var exit li.Exit
scope.Assign(&exit)
defer exit()
// main loop
var sigExit li.SigExit
var renderTimer li.RenderTimer
var resetRenderTimer li.ResetRenderTimer
var trigger li.Trigger
scope.Assign(
&sigExit,
&renderTimer,
&resetRenderTimer,
&trigger,
)
scope = scope.Fork(
func() li.SetContent {
return screen.SetContent
},
)
applyDerives := func() {
if len(derives) > 0 {
scope = scope.Fork(derives...)
derives = derives[:0]
}
}
for {
trigger(li.EvLoopBegin{})
applyDerives()
select {
case ev := <-screenEvents:
scope.Call(func(
handle li.HandleScreenEvent,
) {
handle(ev)
})
resetRenderTimer()
case <-sigExit:
return
case fn := <-funcCalls:
scope.Call(fn)
resetRenderTimer()
case <-renderTimer.C:
// render
var root li.Element
scope.Call(li.Root).Assign(&root)
scope.Call(root.RenderFunc())
screen.Show()
}
applyDerives()
trigger(li.EvLoopEnd{})
applyDerives()
}
}