-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfilterdemo.go
178 lines (149 loc) · 3.72 KB
/
filterdemo.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
package main
import (
"errors"
"fmt"
"strconv"
"strings"
"syscall/js"
"time"
"github.com/rclone/rclone/fs/filter"
)
const (
defaultFiles = `file.txt
file.jpg
dir/flower.jpg
dir/flower.txt
`
)
type filterType int
const (
Include filterType = iota
Exclude
Filter
FilesFrom
filterTypes
)
var filterName = [filterTypes]string{
Include: "--include",
Exclude: "--exclude",
Filter: "--filter",
FilesFrom: "--files-from",
}
var filterRules = [filterTypes][]string{
Include: []string{"*.txt", "*.xls"},
Exclude: []string{"*.jpg", "*.png"},
Filter: []string{"+ *.txt", "- *.jpg", "+ *"},
FilesFrom: []string{},
}
// Globals
var (
document js.Value
results js.Value
flags js.Value
files js.Value
ftype js.Value
)
// Exit with the message
func fatalf(message string, args ...interface{}) {
text := fmt.Sprintf(message, args...)
js.Global().Call("alert", text)
panic(text)
}
// getElementById gets the element with the ID passed in or panics
func getElementById(ID string) js.Value {
obj := document.Call("getElementById", ID)
if obj.IsUndefined() {
fatalf("couldn't find ID %q", ID)
}
return obj
}
func message(text string) {
results.Set("innerHTML", text)
}
func showError(err error) {
results.Set("innerHTML", err.Error())
}
func lines(in string) []string {
return strings.Split(strings.Trim(in, "\n"), "\n")
}
func typeChanged(this js.Value, args []js.Value) interface{} {
fmt.Printf("this = %q", this.String())
return true
}
func changed(this js.Value, args []js.Value) interface{} {
// FIXME need a bash command line parser here
rules := lines(flags.Get("value").String())
fmt.Printf("rules = %q", rules)
fType, err := strconv.Atoi(ftype.Get("value").String())
if err != nil {
showError(err)
return true
}
opt := filter.DefaultOpt
switch filterType(fType) {
case Include:
opt.IncludeRule = rules
case Exclude:
opt.ExcludeRule = rules
case Filter:
opt.FilterRule = rules
case FilesFrom:
// FIXME how to do this? If it only reads from a file...
// opt.IncludeRule = rules
default:
showError(errors.New("unknown filter type"))
return true
}
f, err := filter.NewFilter(&opt)
if err != nil {
showError(err)
return true
}
fileNames := lines(files.Get("value").String())
var out strings.Builder
for _, fileName := range fileNames {
if f.Include(fileName, 50, time.Now(), nil) {
out.WriteString(fileName)
out.WriteRune('\n')
}
}
fmt.Printf("files = %q\n", fileNames)
fmt.Printf("filters = %s\n", f.DumpFilters())
message(out.String())
return true
}
// Set up the page ready to play - called when the DOM is loaded
func initialise(_ []js.Value) {
fmt.Printf("initialise\n")
// Find the elements
flags = getElementById("flags")
files = getElementById("files")
results = getElementById("results")
ftype = getElementById("filterType")
message("potato")
flags.Set("value", strings.Join(filterRules[Include], "\n"))
files.Set("value", defaultFiles)
// attach keypress handler
// flags.Call("addEventListener", "keyup", js.FuncOf(changed))
// files.Call("addEventListener", "keyup", js.FuncOf(changed))
getElementById("filter").Call("addEventListener", "click", js.FuncOf(changed))
ftype.Call("addEventListener", "click", js.FuncOf(typeChanged))
changed(js.Undefined(), nil)
getElementById("loading").Call("remove")
}
// main entry point
func main() {
fmt.Printf("main\n")
// find the document
document = js.Global().Get("document")
if document.IsUndefined() {
fatalf("couldn't find document")
}
// FIXME make it run immediately
// Run initialise when the dom is loaded
//document.Call("addEventListener", "DOMContentLoaded", js.FuncOf(initialise))
initialise(nil)
// Wait forever - everything is done on callbacks now
fmt.Printf("wait\n")
select {}
}