-
Notifications
You must be signed in to change notification settings - Fork 25
/
dump.go
184 lines (155 loc) · 3.85 KB
/
dump.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
package main
import (
"errors"
"fmt"
"image"
"io"
"os"
"github.com/as/a/dump"
"github.com/as/ui/col"
"github.com/as/ui/tag"
)
var ErrBadDump = errors.New("bad dump file")
func Load(g *Grid, file string) (e error) {
defer func() {
dump.Printf("load: err: %v", e)
}()
fd, err := os.Open(file)
if err != nil {
return err
}
defer fd.Close()
r := dump.NewScanner(fd)
var wd, f0, f1 string
if !r.Scan(&wd, &f0, &f1) {
return fmt.Errorf("load: wd: %v", r.Err())
}
if err = os.Chdir(wd); err != nil {
dump.Printf("cd %q: %s", wd, err)
}
f0, f1 = f0, f1
var colcents []int
if !r.Scan(&colcents) {
return fmt.Errorf("load: colcents: %v", r.Err())
}
dump.Printf("colcents: %#v\n", colcents)
glabel := ""
if !r.Scan('w', ' ', &glabel) {
return fmt.Errorf("load: glabel: %v", r.Err())
}
dump.Printf("grid: label: %q\n", glabel)
g.Tag.Label.Delete(0, g.Tag.Label.Len())
g.Tag.Label.InsertString(glabel, 0)
g.Tag.Label.Select(0, 0)
var cols []*col.Col
x := g.Bounds().Min.X
dx := g.Bounds().Dx()
for i, cent := range colcents {
var (
j int
tlabel string
)
if !r.Scan('c', &j, &tlabel) {
return fmt.Errorf("load: tlabel: %v", r.Err())
}
if i != j {
dump.Printf("load: col: mismatch col ids: %d != %d", i, j)
}
dump.Printf("load: col: %d: tlabel: %q", i, tlabel)
c := col.New(g.Dev(), nil)
c.Tag.Label.InsertString(tlabel, 0)
c.Tag.Label.Select(0, 0)
col.Attach(g, c, image.Pt(x+cent*dx/100, 0))
cols = append(cols, c)
}
Loop:
for {
var i, j, q0, q1, cent, nbody, wid, ntag, _, dir, dirty int
kind := byte('?')
if !r.Scan(&kind) {
break
}
switch kind {
case 'F':
r.Scan(&i, &j, &q0, &q1, ¢, &nbody, '\n', &wid, &ntag, &nbody, &dir, &dirty)
case 'f':
r.Scan(&i, &j, &q0, &q1, ¢, '\n', &wid, &ntag, &nbody, &dir, &dirty)
nbody = 0
default:
dump.Printf("load: win: c%dr%d: scan: kind %q: can't parse this type: ", i, j, kind)
break Loop
}
if r.Err() != nil {
dump.Printf("load: win: scan: %c: col(%d) row(%d): %v", kind, i, j, r.Err())
break
}
t := tag.New(g.Dev(), nil)
n, err := io.Copy(t.Label, io.LimitReader(r, int64(ntag)))
dump.Printf("tag: copy label: %d bytes: err (%v)", n, err)
t.Label.Select(0, 0)
if !r.Scan('\n') {
logf("tag label delim: %v", err)
}
n, err = io.Copy(t, io.LimitReader(r, int64(nbody)))
dump.Printf("tag: copy body: %d bytes: err (%v)", n, err)
t.Select(int64(q0), int64(q1))
c := cols[i]
col.Attach(c, t, image.Pt(0, c.Bounds().Min.Y+cent*c.Bounds().Dy()/100))
}
return nil
}
func Dump(g *Grid, wdir string, font0, font1 string) {
d, _ := dump.Create("a.dump")
defer d.Close()
d.Line(wdir)
d.Line(font0)
d.Line(font1)
// Each column's % offset from the start of the X-axis
sep := byte(' ')
for i, c := range g.List {
if i+1 == len(g.List) {
sep = '\n'
}
d.Int(flattenX(c, g), sep)
}
// The label for the grid itself
fmt.Fprintf(d, "w %s\n", g.Tag.Label.Bytes())
// The label for each column along with the column's number
for i, c := range g.List {
c, _ := c.(*col.Col)
if c == nil {
continue
}
fmt.Fprintf(d, "c%11d %s\n", i, c.Tag.Label.Bytes())
}
wid := 1
// The windows
for i, c := range g.List {
c, _ := c.(*col.Col)
if c == nil {
continue
}
for j, t := range c.List {
t, _ := t.(*tag.Tag)
if c == nil {
continue
}
// x, e, f, F
q0, q1 := t.Dot()
cent := flattenY(t, c)
ntag := t.Label.Len()
nbody := t.Len()
dir := 0
dirty := 1
fmt.Fprintf(d, "F%11d %11d %11d %11d %11d %11d \n", i, j, q0, q1, cent, nbody)
fmt.Fprintf(d, "%11d %11d %11d %11d %11d %s\n%s", wid, ntag, nbody, dir, dirty, t.Label.Bytes(), t.Bytes())
wid++
}
}
}
func flattenY(w, c Plane) int {
return 100 * (w.Bounds().Min.Y - c.Bounds().Min.Y) / c.Bounds().Dy()
}
func flattenX(c, g Plane) int {
return 100 * (c.Bounds().Min.X - g.Bounds().Min.X) / g.Bounds().Dx()
}