-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (115 loc) · 3 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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"github.com/asticode/go-astikit"
"github.com/asticode/go-astilectron"
"github.com/tdewolff/minify"
mjson "github.com/tdewolff/minify/json"
)
func main() {
l := log.New(log.Writer(), log.Prefix(), log.Flags())
// Create astilectron
a, err := astilectron.New(l, astilectron.Options{
AppName: "Minimize JSON",
BaseDirectoryPath: "html",
})
if err != nil {
l.Fatal(fmt.Errorf("main: creating astilectron failed: %w", err))
}
defer a.Close()
a.HandleSignals()
// Start
if err = a.Start(); err != nil {
l.Fatal(fmt.Errorf("main: starting astilectron failed: %w", err))
}
// New window
var w *astilectron.Window
if w, err = a.NewWindow("html/index.html", &astilectron.WindowOptions{
Center: astikit.BoolPtr(true),
Width: astikit.IntPtr(950),
Height: astikit.IntPtr(550),
Resizable: astikit.BoolPtr(false),
}); err != nil {
l.Fatal(fmt.Errorf("main: new window failed: %w", err))
}
// Create windows
if err = w.Create(); err != nil {
l.Fatal(fmt.Errorf("main: creating window failed: %w", err))
}
// Add the window menu
addMenu(a, w)
// This will listen to messages sent by Javascript
min := minify.New()
w.OnMessage(func(m *astilectron.EventMessage) interface{} {
const (
cmdFormatPrefix = "CMD_FORMAT_"
errPrefix = "ERR: %w"
)
var s string
if err := m.Unmarshal(&s); err != nil {
panic(err)
}
switch {
case s == "ready":
l.Println("app ready")
case strings.Contains(s, cmdFormatPrefix):
json2minimize := strings.TrimPrefix(s, cmdFormatPrefix)
minimized, err := minimizeJSON(min, json2minimize)
if err != nil {
return fmt.Errorf(errPrefix, err).Error()
}
return minimized
default:
l.Println("unkowned message: " + s)
}
return nil
})
// uncomment to open developer tools
//w.OpenDevTools()
// Blocking pattern
a.Wait()
}
func addMenu(a *astilectron.Astilectron, w *astilectron.Window) {
const aboutCmd = "CMD_ABOUT"
m := a.NewMenu([]*astilectron.MenuItemOptions{
{
Label: astikit.StrPtr("About"),
Accelerator: astilectron.NewAccelerator("Control+U"),
OnClick: func(e astilectron.Event) (deleteListener bool) {
w.SendMessage(aboutCmd)
return false
},
},
{
Label: astikit.StrPtr("Window"),
SubMenu: []*astilectron.MenuItemOptions{
{Label: astikit.StrPtr("Minimize"), Role: astilectron.MenuItemRoleMinimize},
{Label: astikit.StrPtr("Close"), Role: astilectron.MenuItemRoleClose},
},
},
})
if err := m.Create(); err != nil {
panic(fmt.Errorf("main: creating menu failed: %w", err))
}
}
func isJSON(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
func minimizeJSON(m *minify.M, value string) (string, error) {
if !isJSON(value) {
return "", errors.New("not valid JSON")
}
var w, r bytes.Buffer
r.WriteString(value)
if err := mjson.Minify(m, &w, &r, nil); err != nil {
return "", err
}
minimized := w.String()
return minimized, nil
}