-
Notifications
You must be signed in to change notification settings - Fork 1
/
flagstone.go
323 lines (289 loc) · 6.19 KB
/
flagstone.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package flagstone
import (
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"flag"
"io/ioutil"
"log"
"math"
"math/big"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/icza/gowut/gwu"
)
var port int = 8081
var subURL string = ""
var nonFlagArgs []string = []string{}
var sortArray []string = []string{}
var silent bool = false
var useNonFlagArgs bool = false
var css = `
body {
background: linear-gradient(-45deg, #fbf3dc, #d8f6fa, #d3dbf6, #d3f6ee);
background-size: 400% 400%;
animation: gradient 12s ease infinite;
font-size: 18px; font-style: normal;
}
.name,.title,.desc,.description {
font-family: "Arial Black", "Arial Bold", Gadget, sans-serif;
font-variant: normal;
font-weight: 400;
line-height: 30px;
display:block;
floet:left;
}
.name{
font-size: 22px; font-style: normal;
width:150px;
text-align:right;
margin-right: 5px;
text-decoration: underline;
}
.title{
font-size: 64px; font-style: bold;
line-height: 80px;
}
.input{
font-size: 150%;
min-width:200px;
}
.desc{
width:350px;
word-break: break-all;
margin-left: 5px;
}
.description{
width: 70%;
}
button {
display: inline-block;
width: 70%;
text-align: center;
background-color: #5373dc;
font-family: "Arial Black", "Arial Bold", Gadget, sans-serif;
font-size: 24px;
color: #FFF;
text-decoration: none;
font-weight: bold;
padding: 10px 24px;
margin-top: 30px;
border-radius: 4px;
border-bottom: 2px solid #2649bc;
margin-bottom: 10px;
}
button:hover {
transform: translateY(2px);
}
@keyframes gradient {
0% {
background-position: 0% 50%;
}
60% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
`
// SetPort : Set port number
func SetPort(n int) {
port = n
}
// SetSubURL : Set Window Name
func SetSubURL(s string) {
subURL = s
}
// SetSort : Sort By Index
func SetSort(sa []string) {
sortArray = sa
}
// SetSilent : mute log
func SetSilent(b bool) {
silent = b
}
// SetUseNonFlagArgs : make non-flag arguments
func SetUseNonFlagArgs(b bool) {
useNonFlagArgs = b
}
// SetCSS : Specify CSS
func SetCSS(s string) {
css = s
}
// Launch : Capture the flag
func Launch(title string, message string) ([]string, bool) {
done := make(chan bool)
go lanchServer(done, title, message)
return nonFlagArgs, <-done
}
func indexOf(arr []string, str string) int {
for i, v := range arr {
if v == str {
return i
}
}
return -1
}
// lanchServer : lanch server
func lanchServer(done chan bool, exeName string, message string) {
var flags []*flag.Flag
tbArgs := gwu.NewTextBox("")
if len(flag.Args()) > 0 {
tbArgs.SetText(strings.Join(flag.Args(), "\n"))
}
// randomize window name
if subURL == "" {
n, _ := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
b := []byte(n.String())
sha512 := sha512.Sum512(b)
subURL = hex.EncodeToString(sha512[:])
}
// get flags
flag.CommandLine.VisitAll(func(f *flag.Flag) {
flags = append(flags, f)
})
// sort
if len(sortArray) > 0 {
sort.Slice(flags, func(i, j int) bool {
a := indexOf(sortArray, flags[i].Name)
b := indexOf(sortArray, flags[j].Name)
return a < b
})
}
// run server
server := gwu.NewServer("x", "localhost:"+strconv.Itoa(port))
server.AddWin(createPage(done, flags, exeName, message))
if silent {
w := log.Writer()
log.SetOutput(ioutil.Discard)
server.Start(subURL)
log.SetOutput(w)
} else {
server.Start(subURL)
}
}
// create page
func createPage(done chan bool, flags []*flag.Flag, exeName string, message string) gwu.Window {
var submit bool = false
tbArgs := gwu.NewTextBox("")
// create window
if exeName == "" {
if exe, err := os.Executable(); err == nil {
exeName = filepath.Base(exe)
}
}
win := gwu.NewWindow(subURL, exeName)
win.Style().SetFullWidth()
win.SetHAlign(gwu.HACenter)
win.SetCellPadding(2)
title := gwu.NewLabel(exeName)
title.Style().AddClass("title")
win.Add(title)
description := gwu.NewLabel(message)
description.Style().AddClass("description")
win.Add(description)
ctrls := []gwu.HasText{}
// if button clicked then button disable
btn := gwu.NewButton("Run")
btn.AddEHandlerFunc(func(e gwu.Event) {
var hasErr bool = false
for i := 0; i < len(ctrls); i++ {
c := ctrls[i].(gwu.Comp)
if err := flags[i].Value.Set(ctrls[i].Text()); err != nil {
hasErr = true
if c != nil {
c.Style().SetColor(gwu.ClrRed)
e.MarkDirty(c)
}
} else {
if c != nil {
c.Style().SetColor(gwu.ClrBlack)
e.MarkDirty(c)
}
}
}
nonFlagArgs = strings.Split(tbArgs.Text(), "\n")
if hasErr == false {
submit = true
btn.SetEnabled(false)
e.MarkDirty(btn)
}
}, gwu.ETypeClick)
win.Add(btn)
// args textbox
if useNonFlagArgs {
p := gwu.NewHorizontalPanel()
lbl := gwu.NewLabel("args")
lbl.Style().AddClass("name")
p.Add(lbl)
tbArgs.SetRows(2)
tbArgs.Style().AddClass("input")
p.Add(tbArgs)
desc := gwu.NewLabel(" ")
desc.Style().AddClass("desc")
p.Add(desc)
win.Add(p)
}
// add flags textbox
for i := 0; i < len(flags); i++ {
p := gwu.NewHorizontalPanel()
lbl := gwu.NewLabel(flags[i].Name)
lbl.Style().AddClass("name")
p.Add(lbl)
tb := gwu.NewTextBox(flags[i].DefValue)
tb.Style().AddClass("input")
ctrls = append(ctrls, tb)
p.Add(tb)
desc := gwu.NewLabel(" " + flags[i].Usage)
desc.Style().AddClass("desc")
p.Add(desc)
win.Add(p)
}
// if button is disabled then close
html := `
<script defer>
var btn = document.getElementsByTagName("button");
window.setInterval(function(){
if(btn[0].disabled == true){
window.close();
}
},100)
</script>`
h := gwu.NewHTML(html)
win.Add(h)
// if window is closed then action
loaded := false
lastTime := time.Now()
win.AddEHandlerFunc(func(e gwu.Event) {
lastTime = time.Now()
loaded = true
}, gwu.ETypeWinLoad)
t := gwu.NewTimer(250 * time.Millisecond)
t.SetRepeat(true)
t.AddEHandlerFunc(func(e gwu.Event) {
lastTime = time.Now()
}, gwu.ETypeStateChange)
win.Add(t)
ticker := time.NewTicker(100 * time.Millisecond)
go func() {
for {
select {
case <-ticker.C:
if loaded && time.Now().Sub(lastTime).Seconds() >= 1 {
done <- submit
ticker.Stop()
break
}
}
}
}()
// css
cssHTML := "<style>" + css + "</style>"
win.Add(gwu.NewHTML(cssHTML))
return win
}