/
core.go
249 lines (213 loc) · 5.23 KB
/
core.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
242
243
244
245
246
247
248
249
package core
import (
"context"
"fmt"
"sort"
"github.com/Laughs-In-Flowers/data"
"github.com/Laughs-In-Flowers/flip"
"github.com/Laughs-In-Flowers/log"
"github.com/Laughs-In-Flowers/warhola/lib/canvas"
"github.com/Laughs-In-Flowers/warhola/lib/util/ctx"
"github.com/Laughs-In-Flowers/warhola/lib/util/geo"
)
// A string key map of functions returning a flip.Command.
var Core cmdMap
type cmdMap map[string]func() flip.Command
// Register a string tag corresponding to a function providing a flip.Command.
func (c cmdMap) Register(tag string, fn func() flip.Command) {
c[tag] = fn
}
// Register a function that passes in the cmdMap instance to the func.
func (c cmdMap) RegisterFunc(fn func(cmdMap)) {
fn(c)
}
func execute(o *Options, c context.Context, a []string, e executionGroup) (context.Context, flip.ExitStatus) {
e.sort()
var status flip.ExitStatus
for _, x := range e {
c, status = x.efn(o, c)
if status != flip.ExitNo {
return c, status
}
}
return c, status
}
type (
execution struct {
pri int
efn executionFn
}
executionFn func(o *Options, c context.Context) (context.Context, flip.ExitStatus)
executionGroup []execution
)
func (e executionGroup) Len() int { return len(e) }
func (e executionGroup) Less(i, j int) bool { return e[i].pri < e[j].pri }
func (e executionGroup) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
func (e executionGroup) sort() { sort.Sort(e) }
func defaultExecutionGroup() executionGroup {
ret := make(executionGroup, 0)
ret = append(ret, setLogStep, endStep)
return ret
}
func buildExec(debugKey string, fns ...execution) executionGroup {
ret := defaultExecutionGroup()
ret = append(ret, fns...)
ret = append(ret, debugStep(debugKey))
return ret
}
func coreExec(e ...innerExec) executionGroup {
var ret []execution
for i, v := range e {
ret = append(ret, execution{50 + i, coreMainStep(v)})
}
return ret
}
type innerExec func(*Options, canvas.Canvas) (canvas.Canvas, flip.ExitStatus)
func coreMainStep(ie innerExec) executionFn {
return func(o *Options, c context.Context) (context.Context, flip.ExitStatus) {
cv := ctx.Canvas(c)
cv, exit := ie(o, cv)
c = context.WithValue(c, 4, cv)
return c, exit
}
}
var (
setLogStep = execution{
0,
func(o *Options, c context.Context) (context.Context, flip.ExitStatus) {
o.Logger = ctx.Log(c)
return c, flip.ExitNo
},
}
// Nothing uses this, yet could it be useful (or should it be removed)?
//setCtxOptsStep = execution{
// 1,
// func(o *Options, c context.Context) (context.Context, flip.ExitStatus) {
// // 'global' stuff from main in ctx to make 'local'
// return c, flip.ExitNo
// },
//}
debugStep = func(key string) execution {
return execution{
99,
func(o *Options, c context.Context) (context.Context, flip.ExitStatus) {
if d := ctx.DebugMap(c); d != nil {
ii := CoreOptions.Match(key)
for _, i := range ii {
key := i.Key()
val := fmt.Sprintf("%v", i.Provided())
d[key] = val
}
c = context.WithValue(c, 1, d)
}
return c, flip.ExitNo
},
}
}
endStep = execution{
100,
func(o *Options, c context.Context) (context.Context, flip.ExitStatus) {
return c, flip.ExitSuccess
},
}
)
type (
command struct {
group, tag, instruction string
priority int
x []execution
ffn flagSetFunc
cfn cmdFunc
}
flagSetFunc func(*Options) *flip.FlagSet
cmdFunc func(*command) flip.Command
)
func NewCommand(group, tag, instruction string,
prio int,
ffn flagSetFunc,
cfn cmdFunc,
x ...execution) *command {
return &command{
group,
tag,
instruction,
prio,
x,
ffn,
cfn,
}
}
func (c *command) executing() executionGroup {
return buildExec(c.tag, c.x...)
}
func (c *command) setExecute(x ...execution) {
c.x = x
}
func defaultCommandFunc(c *command) flip.Command {
return flip.NewCommand(
c.group,
c.tag,
c.instruction,
c.priority,
false,
func(ctx context.Context, a []string) (context.Context, flip.ExitStatus) {
return execute(CoreOptions, ctx, a, c.executing())
},
c.ffn(CoreOptions),
)
}
func (c *command) Command() flip.Command {
return c.cfn(c)
}
// A package level Options struct consisting of a logger and a data.Vector.
type Options struct {
log.Logger
*data.Vector
}
func (o *Options) pullGeometry(keys ...string) *geo.Geometry {
var g string = o.ToString("default.geometry")
for _, k := range keys {
v := o.ToString(k)
if v != "" {
g = v
break
}
}
return geo.New(g)
}
func coreErrorHandler(o *Options, err error) flip.ExitStatus {
if err != nil {
o.Println(err)
return flip.ExitFailure
}
return flip.ExitNo
}
// A package level Options instance.
var CoreOptions *Options
func init() {
initCoreFonts()
CoreOptions = &Options{nil, data.New("core_options")}
Core = make(cmdMap)
//adjustment
Core.Register("adjust", adjust)
//blend
Core.Register("blend", blend)
//blur
Core.Register("blur", blur)
//channel
//BuitIns.Register()
//convolute
Core.Register("convolve", convolve)
//effect
//BuiltIns.RegisterFunc()
//histogram
//BuiltIns.Register()
//noise
Core.Register("noise", noise)
//text
Core.Register("text", text)
//transform
Core.RegisterFunc(registerTransformCmds)
//translate
Core.RegisterFunc(registerTranslateCmds)
}