-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (195 loc) · 3.94 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
package main // import "github.com/as/a"
import (
"flag"
"fmt"
"image"
"log"
"os"
"runtime"
"github.com/as/shiny/screen"
mus "github.com/as/text/mouse"
"golang.org/x/mobile/event/lifecycle"
"github.com/as/edit"
"github.com/as/ui/col"
"github.com/as/ui/tag"
)
var (
Version = "0.6.7"
eprint = fmt.Println
timefmt = "15.04.05"
)
var (
g *Grid
D *screen.Device
events = make(chan interface{}, 300)
done = make(chan bool)
moribound = make(chan bool, 1)
sigterm = make(chan bool)
focused = false
)
func defaultFaceSize() int {
switch runtime.GOOS {
case "darwin":
return 13
default:
return 11
}
}
var (
utf8 = flag.Bool("u", false, "enable utf8 experiment")
elastic = flag.Bool("elastic", false, "enable elastic tabstops")
oled = flag.Bool("b", false, "OLED display mode (black)")
ftsize = flag.Int("ftsize", defaultFaceSize(), "font size")
srvaddr = flag.String("l", "", "(dangerous) announce and serve file system clients on given endpoint")
clientaddr = flag.String("d", "", "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: ")
flag.Parse()
}
func banner() {
logf("ver=%s", Version)
logf("pid=%d", os.Getpid())
logf("args=%q", os.Args)
repaint()
}
func main() {
defer trypprof()()
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)
for _, v := range list {
col.Attach(g, NewCol(dev, ft, image.ZP, image.ZP, v), sp)
sp.X += size.X / len(list)
}
col.Fill(g)
g.Refresh()
setLogFunc(g.aerr)
banner()
createnetworks()
actinit(g)
assert("actinit", g)
go func() {
for {
select {
case e := <-D.Scroll:
activate(p(e), g)
scroll(act, mus.ScrollEvent{Dy: 5, 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() {
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)
if e.Addr != "" {
actTag = t.(*tag.Tag)
act = actTag.Body
//actTag.Handle(actTag.Body, edit.MustCompile(e.Addr))
MoveMouse(act)
} else {
moveMouse(t.Loc().Min)
}
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() {
select {
case clean := <-moribound:
if clean {
setLogFunc(log.Printf)
logf("TODO: polite shutdown")
close(sigterm)
close(moribound)
}
default:
}
}
func procLifeCycle(e lifecycle.Event) {
if e.To == lifecycle.StageDead {
teardown()
return
}
if e.Crosses(lifecycle.StageFocused) == lifecycle.CrossOff {
focused = false
} else if e.Crosses(lifecycle.StageFocused) == lifecycle.CrossOn {
focused = true
}
}