-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (75 loc) · 1.96 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
//go:generate go run github.com/efskap/statics -f
package main
import (
"bytes"
"fmt"
"html/template"
"strings"
"syscall/js"
"unicode/utf8"
)
var document js.Value // shortcut for convenience
var parsedTmpl *template.Template
var done chan bool // used to keep the program from exiting
func init() {
done = make(chan bool)
document = js.Global().Get("document")
loadTemplate()
}
func loadTemplate() {
tmplSrc := string(files["parsed.gohtml"])
funcs := template.FuncMap{}
parsedTmpl = template.Must(template.New("parsed").Funcs(funcs).Parse(tmplSrc))
}
func main() {
js.Global().Set("parseFile", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
go func() {
setStatus("parsing...")
array := args[0]
fileBytes := make([]uint8, array.Get("byteLength").Int())
js.CopyBytesToGo(fileBytes, array)
reader := bytes.NewBuffer(fileBytes)
if result, err := processFile(reader); err != nil {
showError(err)
} else {
displayResults(result)
}
}()
return nil
}))
js.Global().Call("onReady")
<-done // prevents the program from exiting (unless you write to this channel)
println("exiting")
}
type Results struct {
FirstLine string
FileSize int
}
func processFile(file *bytes.Buffer) (out Results, err error) {
out.FileSize = file.Len()
str := file.String()
if utf8.ValidString(str) {
out.FirstLine = strings.SplitN(str, "\n", 2)[0]
}
return
}
func displayResults(results Results) {
setStatus("")
getElementByID("parsed").Set("hidden", false)
buffer := new(bytes.Buffer)
if err := parsedTmpl.ExecuteTemplate(buffer, "parsed", results); err != nil {
showError(err)
}
getElementByID("parsed").Set("innerHTML", buffer.String())
}
func showError(err error) {
fmt.Println(err)
setStatus(err.Error())
}
func setStatus(text string) {
getElementByID("status-text").Set("hidden", false)
getElementByID("status-text").Set("textContent", text)
}
func getElementByID(id string) js.Value {
return document.Call("getElementById", id)
}