forked from knqyf263/pet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
65 lines (56 loc) · 1.38 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
package dialog
import (
"regexp"
"strings"
"github.com/jroimartin/gocui"
)
var (
views = []string{}
layoutStep = 3
curView = -1
idxView = 0
//CurrentCommand is the command before assigning to variables
CurrentCommand string
//FinalCommand is the command after assigning to variables
FinalCommand string
)
func insertParams(command string, params map[string]string) string {
resultCommand := command
for k, v := range params {
resultCommand = strings.Replace(resultCommand, k, v, -1)
}
return resultCommand
}
// SearchForParams returns variables from a command
func SearchForParams(lines []string) map[string]string {
re := `<([\S].+?[\S])>`
if len(lines) == 1 {
r, _ := regexp.Compile(re)
params := r.FindAllStringSubmatch(lines[0], -1)
if len(params) == 0 {
return nil
}
extracted := map[string]string{}
for _, p := range params {
splitted := strings.Split(p[1], "=")
if len(splitted) == 1 {
extracted[p[0]] = ""
} else {
extracted[p[0]] = splitted[1]
}
}
return extracted
}
return nil
}
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
}