-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
289 lines (241 loc) · 6.82 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/AllenDang/PipeIt/pipe"
g "github.com/AllenDang/giu"
)
const (
saveDir string = "Save"
)
var (
input string
output string
pipeHint string
pipeline pipe.Pipeline
savePipelineName string
savedPipelines []string
selectedIndex int32
comboPreview string
)
func changed() {
if len(pipeline) > 0 {
pipeHint = "Pipeline (left click pipe to config, right click to delete)"
} else {
pipeHint = "Pipeline (click + to add a pipe)"
}
output = ""
var data interface{} = input
for _, p := range pipeline {
data = p.Process(data)
}
output = fmt.Sprint(data)
}
func buildConfigMenu(index int, configUI g.Layout) g.Layout {
inType := pipe.DataTypeString
outType := pipeline[index].GetInputType()
if index > 0 {
inType = pipeline[index-1].GetOutputType()
}
betweenPipes := pipe.QueryPipesBetween(inType, outType)
var addBeforeMenuItems g.Layout
for i, p := range betweenPipes {
builder := p.Builder
addBeforeMenuItems = append(addBeforeMenuItems, g.Selectable(fmt.Sprintf("%s##%d-%d", p.Name, index, i)).OnClick(func() {
pipeline = append(pipeline[:index], append(pipe.Pipeline{builder()}, pipeline[index:]...)...)
changed()
}))
addBeforeMenuItems = append(addBeforeMenuItems, g.Tooltip(p.Tip))
}
var addBeforeMenu g.Layout
if len(addBeforeMenuItems) > 0 {
addBeforeMenu = append(addBeforeMenu, g.Menu(fmt.Sprintf("Add before##%d", index)).Layout(addBeforeMenuItems))
} else {
addBeforeMenu = append(addBeforeMenu, g.Menu(fmt.Sprintf("Add before##%d", index)).Layout(g.Layout{g.Label("No suitable pipe")}))
}
return g.Layout{
g.Custom(func() {
if configUI != nil {
g.ContextMenu().ID(fmt.Sprintf("%s##%d", "configMenu", index)).MouseButton(g.MouseButtonLeft).Layout(configUI).Build()
}
}),
g.ContextMenu().ID(fmt.Sprintf("%s##%d", "opMenu", index)).MouseButton(g.MouseButtonRight).Layout(g.Layout{
addBeforeMenu,
g.Selectable("Delete").OnClick(func() {
pipeline = append(pipeline[:index], pipeline[index+1:]...)
changed()
}),
}),
}
}
func buildPipesMenu() g.Widget {
var widgets []g.Widget
queryType := pipe.DataTypeString
if len(pipeline) > 0 {
queryType = pipeline[len(pipeline)-1].GetOutputType()
}
pipBuilders := pipe.QueryPipes(queryType)
if pipBuilders == nil {
widgets = append(widgets, g.Label("No suitable pipe"))
} else {
for i, pb := range pipBuilders {
builder := pb.Builder
widgets = append(widgets,
g.Selectable(fmt.Sprintf("%s##%d", pb.Name, i)).OnClick(func() {
pipeline = append(pipeline, builder())
changed()
}),
g.Tooltip(pb.Tip),
)
}
}
return g.ContextMenu().ID("AvailabePipes").MouseButton(g.MouseButtonLeft).Layout(widgets...)
}
func buildPipeLineWidgets(pipes pipe.Pipeline) g.Widget {
var widgets []g.Widget
if len(pipes) > 0 {
for i, p := range pipes {
configUI := p.GetConfigUI(func() { changed() })
widgets = append(widgets,
g.Button(fmt.Sprintf(" %s ##%d", p.GetName(), i)),
g.Tooltip(p.GetTip()),
buildConfigMenu(i, configUI),
g.Label("->"))
}
}
widgets = append(widgets, g.Button(" + "), buildPipesMenu())
return g.Row(widgets...)
}
func btnLoadClicked() {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
dir = filepath.Join(dir, saveDir)
f, err := os.Open(filepath.Join(dir, comboPreview))
if err != nil {
g.Msgbox("Error", fmt.Sprintf("Load pipeline failed, error message is %s", err.Error()))
return
}
defer f.Close()
pl, err := pipe.DecodePipeline(f)
if err != nil {
g.Msgbox("Error", fmt.Sprintf("Load pipeline failed, error message is %s", err.Error()))
return
}
pipeline = *pl
changed()
}
func btnSaveClicked() {
if len(pipeline) == 0 {
g.Msgbox("Error", "Current pipeline is empty.")
return
}
g.OpenPopup("Save Pipeline")
}
func onSave() {
defer func() {
g.CloseCurrentPopup()
loadSavedPiplines()
}()
if len(savePipelineName) == 0 {
g.Msgbox("Error", "Pipeline's name cannot be empty")
return
}
// Prepare save dir
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
dir = filepath.Join(dir, saveDir)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, os.ModeDir)
if err != nil {
g.Msgbox("Error", fmt.Sprintf("Failed to create save directory, error message is %s", err.Error()))
return
}
}
saveFilepath := filepath.Join(dir, fmt.Sprintf("%s.pl", savePipelineName))
buf, err := pipe.EncodePipeline(pipeline)
if err != nil {
g.Msgbox("Error", fmt.Sprintf("Save pipeline failed, error message is %s", err.Error()))
return
}
err = ioutil.WriteFile(saveFilepath, buf.Bytes(), 0644)
if err != nil {
g.Msgbox("Error", fmt.Sprintf("Save pipeline failed, error message is %s", err.Error()))
return
}
}
func onCancel() {
g.CloseCurrentPopup()
}
func onComboChanged() {
comboPreview = savedPipelines[selectedIndex]
}
func loadSavedPiplines() {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
dir = filepath.Join(dir, saveDir)
// Get all saved *.pl filenames.
var files []string
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() && filepath.Ext(path) == ".pl" {
files = append(files, filepath.Base(path))
}
return nil
})
savedPipelines = files
if int(selectedIndex) > len(savedPipelines) {
selectedIndex = 0
}
if len(savedPipelines) > 0 {
comboPreview = savedPipelines[selectedIndex]
}
}
func loop() {
g.SingleWindow().Layout(g.Layout{
g.SplitLayout(g.DirectionVertical, 300,
g.Layout{
g.Label("Input - input or paste text below"),
g.InputTextMultiline(&input).Size(-1, -1).OnChange(changed),
},
g.Layout{
g.Dummy(0, 8),
g.Row(
g.Label(pipeHint),
g.Combo("##savedPipeines", comboPreview, savedPipelines, &selectedIndex).Size(200).OnChange(onComboChanged),
g.Button("Load").OnClick(btnLoadClicked),
g.Button("Save").OnClick(btnSaveClicked),
),
g.PopupModal("Save Pipeline").Flags(g.WindowFlagsNoResize).Layout(g.Layout{
g.Label("Enter the name of the pipeline "),
g.InputText(&savePipelineName).Size(200),
g.Row(
g.Button("Save").OnClick(onSave),
g.Button("Cancel").OnClick(onCancel),
),
}),
buildPipeLineWidgets(pipeline),
g.Dummy(0, 8),
g.Label("Output - output text which is processed by pipeline"),
g.InputTextMultiline(&output).Size(-1, -1).Flags(g.InputTextFlagsReadOnly),
}).Border(false),
g.PrepareMsgbox(),
})
}
func readStdin() {
stat, err := os.Stdin.Stat()
if err != nil {
return
}
if (stat.Mode() & os.ModeCharDevice) == 0 {
// Data is being piped to stdin
bytes, _ := ioutil.ReadAll(os.Stdin)
input = string(bytes)
}
}
func main() {
pipeHint = "Pipeline (click + to add a pipe)"
// Try to read from stdin if there is anything.
readStdin()
// Load saved pipelines.
loadSavedPiplines()
wnd := g.NewMasterWindow("PipeIt", 1024, 768, 0)
wnd.Run(loop)
}