forked from methos2016/DuckyManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionalities.go
109 lines (84 loc) · 2.29 KB
/
functionalities.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
package main
import (
"strings"
"github.com/nsf/termbox-go"
)
func pickFunctionality(ev termbox.Event, currentState State) error {
if ev.Ch != 0 {
switch ev.Ch {
case 's', 'S':
res, err := search(currentState.Scripts)
if err != nil {
return err
}
if len(res) != 0 {
mainLoop(State{
Title: "Search results",
Scripts: res,
Position: 0,
PositionUpper: 0,
})
} else {
err := showErrorMsg(translate.NoMatch)
if err != nil {
return err
}
}
case 'e', 'E':
if err := edit(currentState); err != nil {
return err
}
}
}
return nil
}
func search(scripts []Script) (res []Script, err error) {
var functions = []func([]Script, string) []Script{ListByName, ListByUser, ListByTags, ListByDesc}
var titles = []string{translate.SidebarTitle, translate.SidebarBy, translate.SidebarTags, translate.SidebarDesc}
// Pointers needed for compatibility with edit function
var values = make([]*string, len(titles))
var valueHolders = make([]string, len(titles))
for i := range values {
values[i] = &valueHolders[i]
}
if err = editableMenu(titles, values); err != nil {
return
}
res = searchFromValues(scripts, values, functions)
SortScripts(res)
res = TrimRepeated(res)
return
}
func edit(currentState State) (err error) {
script := currentState.Scripts[currentState.Position]
var values = []*string{&script.Name, &script.User, &script.Tags, &script.Desc}
var titles = []string{translate.SidebarTitle, translate.SidebarBy, translate.SidebarTags, translate.SidebarDesc}
if err = editableMenu(titles, values); err != nil {
return
}
// Update values of script
currentState.Scripts[currentState.Position] = script
SortScripts(currentState.Scripts)
return
}
func searchFromValues(scripts []Script, values []*string, functions []func([]Script, string) []Script) (res []Script) {
for i, value := range values {
if *value != "" {
// Search multiple, for tags
if strings.Contains(",", *value) {
for _, v := range strings.Split(*value, ",") {
res = append(res, functions[i](scripts, v)...)
}
} else {
res = append(res, functions[i](scripts, *value)...)
}
}
}
return
}
func waitForEnter() {
ev := termbox.PollEvent()
for ev.Key != termbox.KeyEnter {
ev = termbox.PollEvent()
}
}