-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTriggerFunctions.go
237 lines (206 loc) · 6.52 KB
/
TriggerFunctions.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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
func convertToJSONTrigger(parentDir string, trigger Trigger) JSONTrigger {
jsonTrigger := JSONTrigger{
Name: trigger.Name,
IsActive: trigger.IsActive,
IsFolder: trigger.IsFolder,
Command: trigger.MCommand,
Multiline: trigger.IsMultiline,
MultilineDelta: strconv.Itoa(trigger.ConditionLineDelta),
Matchall: trigger.IsPerlSlashGOption,
Filter: trigger.IsFilterTrigger,
FireLength: trigger.MStayOpen,
SoundFile: trigger.MSoundFile,
Highlight: trigger.IsColorizerTrigger,
HighlightFG: trigger.MFgColor,
HighlightBG: trigger.MBgColor,
}
for i, content := range trigger.RegexCodeList {
jsonPattern := Pattern{
Pattern: content,
Type: patternNumberToType(trigger.RegexCodeProperties[i]),
}
if jsonPattern.Type == "colour" {
jsonPattern.Pattern = extractColorTriggerColors(content)
}
jsonTrigger.Patterns = append(jsonTrigger.Patterns, jsonPattern)
}
// only append the children to this triggers.json if they are *not* in a folder
// this seems weird, but the child triggers / trigger groups will be handled by recursion
// in handleTriggerGroups.
// Adding children elements associated with folders will prevent muddler from picking up the correct
// json and scripts inside those folders.
if trigger.IsFolder == "no" {
for _, child := range trigger.Triggers {
jsonTrigger.Children = append(jsonTrigger.Children, convertToJSONTrigger(parentDir, child))
}
}
writeTriggerScript(parentDir, &trigger, &jsonTrigger)
return jsonTrigger
}
func convertToAnsiColor(colorStr string) (ansiColor string) {
switch colorStr {
case "-2":
ansiColor = "IGNORE"
case "0":
ansiColor = "DEFAULT"
case "1":
ansiColor = "8" // Light black (dark gray)
case "2":
ansiColor = "0" // Black
case "3":
ansiColor = "9" // Light red
case "4":
ansiColor = "1" // Red
case "5":
ansiColor = "10" // Light green
case "6":
ansiColor = "2" // Green
case "7":
ansiColor = "11" // Light yellow
case "8":
ansiColor = "3" // Yellow
case "9":
ansiColor = "12" // Light blue
case "10":
ansiColor = "4" // Blue
case "11":
ansiColor = "13" // Light magenta
case "12":
ansiColor = "5" // Magenta
case "13":
ansiColor = "14" // Light cyan
case "14":
ansiColor = "6" // Cyan
case "15":
ansiColor = "15" // Light white
case "16":
ansiColor = "7" // White (light gray)
default:
ansiColor = colorStr // Use color directly if it doesn't match any case
}
return ansiColor
}
func extractColorTriggerColors(colorStr string) string {
// Handling "FG#BG#" pattern
var fgColor, bgColor string
fgIndex := strings.Index(colorStr, "FG")
bgIndex := strings.Index(colorStr, "BG")
if fgIndex != -1 && bgIndex != -1 {
fgColor = colorStr[fgIndex+2 : bgIndex]
bgColor = colorStr[bgIndex+2:]
return fmt.Sprintf("%s,%s", convertToAnsiColor(fgColor), convertToAnsiColor(bgColor))
}
// Handling "ANSI_COLORS_F{#}_B{#}" pattern
fgStart := strings.Index(colorStr, "F{")
bgStart := strings.Index(colorStr, "}_B{")
if fgStart != -1 && bgStart != -1 {
fgEnd := strings.Index(colorStr[fgStart:], "}") + fgStart
bgEnd := strings.Index(colorStr[bgStart+3:], "}") + bgStart + 3
if fgEnd > fgStart && bgEnd > bgStart {
fgColor = colorStr[fgStart+2 : fgEnd]
bgColor = colorStr[bgStart+4 : bgEnd]
return fmt.Sprintf("%s,%s", fgColor, bgColor)
}
}
return ""
}
func patternNumberToType(patternNumber int) string {
typeMap := map[int]string{
0: "substring",
1: "regex",
2: "startOfLine",
3: "exactMatch",
4: "lua",
5: "spacer",
6: "colour",
7: "prompt",
}
triggerType, exists := typeMap[patternNumber]
if !exists {
return "substring" // Default value
}
return triggerType
}
func handleTriggers(triggers *[]Trigger, parentDir string) []JSONTrigger {
if len(*triggers) == 0 {
return nil
}
var jsonFile []JSONTrigger
if parentDir != "" {
if err := os.MkdirAll(parentDir, 0755); err != nil {
panic(err)
}
}
for _, trigger := range *triggers {
jsonTrigger := convertToJSONTrigger(parentDir, trigger)
jsonFile = append(jsonFile, jsonTrigger)
}
return jsonFile
}
func writeTriggerScript(parentDir string, trigger *Trigger, jsonTrigger *JSONTrigger) {
scriptFileName := strings.ReplaceAll(trigger.Name, " ", "_")
scriptFilePath := filepath.Join(parentDir, scriptFileName+".lua")
if len(trigger.Script) == 0 || containsIllegalCharacters(scriptFileName) {
jsonTrigger.Script = trigger.Script
} else {
if err := os.WriteFile(scriptFilePath, []byte(trigger.Script), 0644); err != nil {
panic(err)
}
}
}
func writeJson(jsonFile []JSONTrigger, parentDir string) {
if parentDir != "" {
if err := os.MkdirAll(parentDir, 0755); err != nil {
panic(err)
}
}
jsonFilePath := filepath.Join(parentDir, "triggers.json")
jsonData, err := json.MarshalIndent(jsonFile, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(jsonFilePath, jsonData, 0644)
if err != nil {
panic(err)
}
}
func handleTriggerGroups(groups *[]TriggerGroup, baseDir string) {
for i := range *groups {
groupPath := filepath.Join(baseDir, (*groups)[i].Name)
// first, handle all the triggers in this group, but don't write triggers.json yet
jsonTriggers := handleTriggers(&((*groups)[i].Triggers), groupPath)
// next, for all triggerGroups in this group, if they are a chain, add them to triggers.json
triggerGroups := (*groups)[i].TriggerGroup // all the triggerGroups in this group
for x := range triggerGroups {
if len(triggerGroups[x].Trigger.RegexCodeList) > 0 { // chains require a trigger pattern
localJson := handleTriggers(&[]Trigger{triggerGroups[x].Trigger}, groupPath) // this group's trigger data
jsonTriggers = append(jsonTriggers, localJson...)
}
}
// now write the full triggers.json (trigger data + triggerGroup data)
writeJson(jsonTriggers, groupPath)
// recur
handleTriggerGroups(&((*groups)[i].TriggerGroup), groupPath)
}
}
// handle the root directory first, before using recursion to build the rest of the TriggerGroups.
func handleTriggerPackage(groups *[]TriggerGroup, triggers *[]Trigger, baseDir string) {
jsonTriggers := handleTriggers(triggers, baseDir)
for i := range *groups {
if len((*groups)[i].Trigger.RegexCodeList) > 0 { // chains require a trigger pattern
localJson := handleTriggers(&[]Trigger{(*groups)[i].Trigger}, baseDir) // the Trigger data on TriggerGroup
jsonTriggers = append(jsonTriggers, localJson...)
}
}
writeJson(jsonTriggers, baseDir)
handleTriggerGroups(groups, baseDir)
}