-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
147 lines (116 loc) · 3.1 KB
/
parser.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
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"regexp"
)
const (
active = "a"
create = "c"
horizontal = "h"
vertical = "v"
)
type Args []string
func Parse(session string, file *os.File) (cmds []Args) {
windows := Args{}
scanner := bufio.NewScanner(file)
cmds = append(cmds, setupCommands(session))
for scanner.Scan() {
line := scanner.Text()
captures, err := ParseLine(line)
if err != nil {
log.Printf("Skipped: '%v'. Err: %v. Got: %+v", line, err, captures)
continue
}
window := captures["window"]
operation := captures["operation"]
target := captures["target"]
action := captures["action"]
if len(operation) > 1 {
target = operation[0:(len(operation) - 1)]
operation = operation[len(operation)-1:]
}
switch operation {
case create:
if contains(windows, window) {
log.Fatal("Already created window %s", window)
}
windows = append(windows, window)
cmds = append(cmds, newWindow(session, window))
case horizontal, vertical:
if !contains(windows, window) {
log.Fatalf("need to create the window first before splitting it: %v", line)
}
cmds = append(cmds, splitWindow(session, window, tmuxSplit(operation), target))
case active:
cmds = append(cmds, selectWindow(session, window))
if target != "" {
cmds = append(cmds, selectPane(session, target))
}
}
if action != "" {
cmds = append(cmds, sendKeys(session, window, action))
}
}
return
}
func contains(arr Args, v1 string) bool {
for _, v2 := range arr {
if v1 == v2 {
return true
}
}
return false
}
func newWindow(session, window string) Args {
return Args{"new-window", "-a", "-t", session, "-n", window, "-c", os.Getenv("PWD")}
}
func selectWindow(session, window string) Args {
target := fmt.Sprintf("%s:%s", session, window)
return Args{"select-window", "-t", target}
}
func selectPane(session, target string) Args {
pane := fmt.Sprintf("%s.%s", session, target)
return Args{"select-pane", "-t", pane}
}
func splitWindow(session, window, split, target string) Args {
if target != "" {
target = fmt.Sprintf(".%s", target)
}
target = fmt.Sprintf("%s:%s%s", session, window, target)
return Args{"split-window", "-t", target, fmt.Sprintf("-%s", split)}
}
func sendKeys(session, window, action string) Args {
target := fmt.Sprintf("%s:%s", session, window)
return Args{"send-keys", "-t", target, action, "C-m"}
}
func tmuxSplit(operation string) string {
if operation == horizontal {
return "v"
}
return "h"
}
func setupCommands(session string) Args {
return Args{"new-session", "-d", "-s", session}
}
func killCommands(session string) Args {
return Args{"kill-session", "-t", session}
}
func ParseLine(line string) (map[string]string, error) {
var re = regexp.MustCompile(`(?P<window>[a-z]*)\s(?P<target>\d?)(?P<operation>[cvha])\s?(?P<action>.*)`)
match := re.FindStringSubmatch(line)
captures := make(map[string]string)
if match == nil {
return captures, errors.New("Can't parse line")
}
for i, name := range re.SubexpNames() {
if i == 0 || name == "" {
continue
}
captures[name] = match[i]
}
return captures, nil
}