-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
241 lines (218 loc) · 4.61 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main // import "github.com/as/a"
import (
"flag"
"fmt"
"image"
"io"
"log"
"os"
"github.com/as/edit"
"github.com/as/shiny/event/lifecycle"
"github.com/as/shiny/screen"
"github.com/as/ui/col"
"github.com/as/ui/tag"
)
var (
eprint = fmt.Println
timefmt = "15.04.05"
)
var (
g *Grid
D *screen.Device
events = make(chan interface{}, 25)
done = make(chan bool)
moribound = make(chan bool, 1)
sigterm = make(chan bool)
focused = false
)
var (
utf8 = flag.Bool("u", false, "enable utf8 experiment")
elastic = flag.Bool("elastic", false, "enable elastic tabstops")
images = flag.Bool("img", os.Getenv("img") == "auto", "render images in editor (experimental/unstable)")
oled = flag.Bool("b", false, "OLED display mode (black)")
ftsize = flag.Int("ftsize", defaultFaceSize(), "font size")
srvaddr = flag.String("srv", "", "(dangerous) announce and serve file system clients on given endpoint")
load = flag.String("l", "", "load state from a dump file in acme format")
dialaddr = flag.String("dial", "", "dial to a remote file system on endpoint")
quiet = flag.Bool("q", false, "dont interact with the graphical subsystem (use with -l)")
)
func init() {
// this grants the capability to shut down the program
// it happens exactly once
moribound <- true
// error.go:/logFunc/
log.SetFlags(log.Llongfile)
log.SetPrefix("a: ")
}
// showbanner is set to true only if the user ran the program without
// arguments. In any other case, it's just annoying to have this on
// see args.go:/showbanner/
var showbanner = false
func banner() {
if !showbanner {
return
}
logf("ver=%s pid=%d args=%q", Version, os.Getpid(), os.Args)
repaint()
}
func trap() {
err := recover()
if err != nil {
teardown(true)
panic(err)
}
}
func main() {
flag.Parse()
defer trypprof()()
defer trap()
list := argparse()
if *quiet {
banner()
createnetworks()
<-done
os.Exit(0)
}
dev, wind, d, ft := frameinstall()
D = d
g = NewGrid(dev, GridConfig)
{
sp, size := image.Pt(0, 0), image.Pt(900, 900)
g.Move(sp)
g.Resize(size)
if *load != "" {
Load(g, *load)
} else {
for _, v := range list {
c := NewCol(dev, ft, image.ZP, image.ZP, v)
if v == "-" {
c = NewCol(dev, ft, image.ZP, image.ZP)
io.Copy(New(c, "", "").(*tag.Tag), os.Stdin)
}
col.Attach(g, c, sp)
sp.X += size.X / len(list)
}
col.Fill(g)
}
g.Refresh()
}
setLogFunc(g.aerr)
banner()
createnetworks()
actinit(g)
assert("actinit", g)
go func() {
defer trap()
for {
select {
case e := <-D.Scroll:
activate(p(e), g)
scroll(act, ScrollEvent{Dy: 7, Event: e})
case e := <-D.Mouse:
activate(p(e), g)
e = readmouse(e)
if down == 0 {
continue
}
if borderHit(rel(e, act)) {
procBorderHit(e)
} else {
// assert("procButton", g) //
procButton(rel(e, act))
}
repaint()
case <-sigterm:
logf("mouse: sigterm")
return
}
}
}()
go func() {
defer trap()
for {
select {
case e := <-D.Key:
kbdin(e, actTag, act)
repaint()
case <-sigterm:
logf("kbd: sigterm")
return
}
}
}()
Loop:
for {
select {
case <-sigterm:
logf("mainselect: sigterm")
break Loop
case e := <-D.Size:
winSize = image.Pt(e.WidthPx, e.HeightPx)
g.Resize(winSize)
repaint()
case e := <-D.Paint:
if throttled() {
continue
}
if e.External {
g.Resize(winSize)
}
g.Upload()
wind.Publish()
case e := <-D.Lifecycle:
procLifeCycle(e)
repaint()
case e := <-events:
switch e := e.(type) {
case tag.GetEvent:
t := New(actCol, e.Basedir, e.Name)
getcmd(t.(*tag.Tag))
if e.Addr != "" {
actTag = t.(*tag.Tag)
act = actTag.Window
//actTag.Handle(actTag.Window, edit.MustCompile(e.Addr))
MoveMouse(act)
} else {
moveMouse(t.Bounds().Min)
}
repaint()
case edit.File:
g.acolor(e)
case edit.Print:
g.aout(string(e))
case error:
logf("unspecified error: %s", e)
case interface{}:
logf("missing event: %#v\n", e)
continue
}
repaint()
}
}
}
func teardown(save bool) {
select {
case clean := <-moribound:
if clean {
setLogFunc(log.Printf)
if save {
Dump(g, g.cwd(), "gomono", "goregular")
println("crash: saved: use 'a -l a.dump' to restore")
}
close(sigterm)
close(moribound)
}
default:
}
}
func procLifeCycle(e lifecycle.Event) {
if e.To == lifecycle.StageDead {
teardown(false)
return
}
if e.Crosses(lifecycle.StageFocused) == lifecycle.CrossOff {
focused = false
} else if e.Crosses(lifecycle.StageFocused) == lifecycle.CrossOn {
focused = true
}
}