-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselext.go
260 lines (228 loc) · 6.18 KB
/
selext.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
// +build OMIT
package main
import (
"errors"
"fmt"
"github.com/andlabs/ui"
_ "github.com/andlabs/ui/winmanifest"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
)
var eol = func() string {
switch runtime.GOOS {
case "linux":
return "\n"
case "windows":
return "\r"
case "darwin":
return "\n\r"
default:
return "\n"
}
}()
func reportError() {
}
func stringsUnique(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func commandUniq(input string) (string, error) {
lines := strings.Split(input, eol)
lines = stringsUnique(lines)
return strings.Join(lines, eol), nil
}
func commandTrim(input string) (string, error) {
lines := strings.Split(input, eol)
var trimmed []string
for _, v := range lines {
trimmed = append(trimmed, strings.Trim(v, " "))
}
return strings.Join(trimmed, eol), nil
}
func commandCount(input string) (string, error) {
return strconv.Itoa(len(strings.Split(input, eol))), nil
}
func commandReg(input string, regexPattern string) (string, error) {
var err error
r, _ := regexp.Compile("\\(.+\\)")
if !r.MatchString(regexPattern) {
regexPattern = "(" + regexPattern + ")"
}
r, err = regexp.Compile(regexPattern)
if err != nil {
return "", errors.New("re: invalid regexp")
}
res := r.FindAllStringSubmatch(input, -1)
var matches []string
for _, v := range res {
matches = append(matches, v[1])
}
return strings.Join(matches, eol), nil
}
func commandEmail(input string) (string, error) {
r, _ := regexp.Compile("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+)")
res := r.FindAllString(input, -1)
return strings.Join(res, eol), nil
}
func commandIpv4(input string) (string, error) {
r, _ := regexp.Compile("((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)")
res := r.FindAllString(input, -1)
return strings.Join(res, eol), nil
}
func commandUpper(input string) (string, error) {
return strings.ToUpper(input), nil
}
func commandLower(input string) (string, error) {
return strings.ToLower(input), nil
}
func commandPrefix(input string, prefix string) (string, error) {
lines := strings.Split(input, eol)
var res []string
for _, v := range lines {
res = append(res, prefix+v)
}
return strings.Join(res, eol), nil
}
func commandPostfix(input string, postfix string) (string, error) {
lines := strings.Split(input, eol)
var res []string
for _, v := range lines {
res = append(res, v+postfix)
}
return strings.Join(res, eol), nil
}
func commandSum(input string) (string, error) {
lines := strings.Split(input, eol)
sum := 0.0
for _, v := range lines {
if s, err := strconv.ParseFloat(v, 64); err == nil {
sum += s
} else {
return "", errors.New(fmt.Sprintf("re: invalid float format: %s", v))
}
}
return strconv.FormatFloat(sum, 'f', 6, 64), nil
}
func commandAsc(input string) (string, error) {
lines := strings.Split(input, eol)
sort.Strings(lines)
return strings.Join(lines, eol), nil
}
func commandDesc(input string) (string, error) {
lines := strings.Split(input, eol)
sort.Sort(sort.Reverse(sort.StringSlice(lines)))
return strings.Join(lines, eol), nil
}
func processCommands(input string, code string) (string, error) {
var err error
code, err = commandTrim(code)
output := input
codeLines := strings.Split(code, eol)
for _, line := range codeLines {
if strings.HasPrefix(line, "#") {
continue
}
commandSlice := strings.Split(line, ",")
switch commandSlice[0] {
case "count":
output, err = commandCount(output)
case "trim":
output, err = commandTrim(output)
case "uniq":
output, err = commandUniq(output)
case "reg":
output, err = commandReg(output, strings.Trim(strings.Join(commandSlice[1:], ""), "\"'"))
case "email":
output, err = commandEmail(output)
case "ipv4":
output, err = commandIpv4(output)
case "upper":
output, err = commandUpper(output)
case "lower":
output, err = commandLower(output)
case "prefix":
output, err = commandPrefix(output, strings.Trim(strings.Join(commandSlice[1:], ""), "\"'"))
case "postfix":
output, err = commandPostfix(output, strings.Trim(strings.Join(commandSlice[1:], ""), "\"'"))
case "sum":
output, err = commandSum(output)
case "asc":
output, err = commandAsc(output)
case "desc":
output, err = commandDesc(output)
}
}
return output, err
}
func main() {
err := ui.Main(func() {
window := ui.NewWindow("selext v1", 800, 600, false)
inputTextArea := ui.NewMultilineEntry()
inputTextArea.SetText("please@he\n" +
"12\n" +
"dcdc\n" +
"$80\n" +
"$60\n" +
"dcdc\n" +
"23\n" +
"ceacaec34\n" +
"ceacaec34\n" +
vboxInput := ui.NewVerticalBox()
vboxInput.Append(ui.NewLabel("Input"), false)
vboxInput.Append(inputTextArea, true)
outputTextArea := ui.NewMultilineEntry()
outputTextArea.SetReadOnly(true)
vboxOutput := ui.NewVerticalBox()
vboxOutput.Append(ui.NewLabel("Output"), false)
vboxOutput.Append(outputTextArea, true)
hbox1 := ui.NewHorizontalBox()
hbox1.Append(vboxInput, true)
hbox1.Append(vboxOutput, true)
buttonRun := ui.NewButton("Run")
messagesTextArea := ui.NewMultilineEntry()
messagesTextArea.SetReadOnly(true)
vbox1 := ui.NewVerticalBox()
vbox1.Append(ui.NewLabel("Messages"), false)
vbox1.Append(messagesTextArea, true)
codeTextArea := ui.NewMultilineEntry()
vboxCode := ui.NewVerticalBox()
vboxCode.Append(ui.NewLabel("Code"), false)
vboxCode.Append(codeTextArea, true)
vboxCode.Append(buttonRun, false)
hbox2 := ui.NewHorizontalBox()
hbox2.Append(vboxCode, true)
hbox2.Append(vbox1, true)
vboxmain := ui.NewVerticalBox()
vboxmain.Append(hbox1, true)
vboxmain.Append(hbox2, true)
buttonRun.OnClicked(func(*ui.Button) {
output, err := processCommands(inputTextArea.Text(), codeTextArea.Text())
if err != nil {
ui.MsgBoxError(window, "Command error", err.Error())
} else {
outputTextArea.SetText(output)
}
})
window.SetMargined(true)
window.SetChild(vboxmain)
window.OnClosing(func(*ui.Window) bool {
ui.Quit()
return true
})
window.Show()
})
if err != nil {
panic(err)
}
}