-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathparams.go
97 lines (79 loc) · 2.59 KB
/
params.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
package dialog
import (
"regexp"
"strings"
"github.com/awesome-gocui/gocui"
)
var (
views = []string{}
//CurrentCommand is the command before assigning to variables
CurrentCommand string
//FinalCommand is the command after assigning to variables
FinalCommand string
// This matches most encountered patterns
// Skips match if there is a whitespace at the end ex. <param='my >
// Ignores <, > characters since they're used to match the pattern
parameterStringRegex = `<([^<>]*[^\s])>`
)
func insertParams(command string, filledInParams map[string]string) string {
r := regexp.MustCompile(parameterStringRegex)
matches := r.FindAllStringSubmatch(command, -1)
if len(matches) == 0 {
return command
}
resultCommand := command
// First match is the whole match (with brackets), second is the first group
// Ex. echo <param='my param'>
// -> matches[0][0]: <param='my param'>
// -> matches[0][1]: param='my param'
for _, p := range matches {
whole, matchedGroup := p[0], p[1]
param, _, _ := strings.Cut(matchedGroup, "=")
// Replace the whole match with the filled-in value of the param
resultCommand = strings.Replace(resultCommand, whole, filledInParams[param], -1)
}
return resultCommand
}
// SearchForParams returns variables from a command
func SearchForParams(command string) [][2]string {
r := regexp.MustCompile(parameterStringRegex)
params := r.FindAllStringSubmatch(command, -1)
if len(params) == 0 {
return nil
}
extracted := map[string]string{}
ordered_params := [][2]string{}
for _, p := range params {
_, matchedGroup := p[0], p[1]
paramKey, defaultValue, separatorFound := strings.Cut(matchedGroup, "=")
_, param_exists := extracted[paramKey]
// Set to empty if no value is provided and param is not already set
if !separatorFound && !param_exists {
extracted[paramKey] = ""
} else if separatorFound {
// Set to default value instead if it is provided
extracted[paramKey] = defaultValue
}
// Fill in the keys only if seen for the first time to track order
if !param_exists {
ordered_params = append(ordered_params, [2]string{paramKey, ""})
}
}
// Fill in the values
for i, param := range ordered_params {
pair := [2]string{param[0], extracted[param[0]]}
ordered_params[i] = pair
}
return ordered_params
}
func evaluateParams(g *gocui.Gui, _ *gocui.View) error {
paramsFilled := map[string]string{}
for _, v := range views {
view, _ := g.View(v)
res := view.Buffer()
res = strings.Replace(res, "\n", "", -1)
paramsFilled[v] = strings.TrimSpace(res)
}
FinalCommand = insertParams(CurrentCommand, paramsFilled)
return gocui.ErrQuit
}