-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
tmux.go
210 lines (159 loc) · 4.61 KB
/
tmux.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
package main
import (
"os"
"os/exec"
"strings"
)
const (
VSplit = "vertical"
HSplit = "horizontal"
)
const (
EvenHorizontal = "even-horizontal"
Tiled = "tiled"
)
type TmuxOptions struct {
// Default socket name
SocketName string `yaml:"socket_name"`
// Default socket path, overrides SocketName
SocketPath string `yaml:"socket_path"`
// tmux config file
ConfigFile string `yaml:"config_file"`
}
type Tmux struct {
commander Commander
*TmuxOptions
}
type TmuxWindow struct {
ID string
Name string
Layout string
Root string
}
type TmuxPane struct {
Root string
}
func (tmux Tmux) cmd(args ...string) *exec.Cmd {
tmuxCmd := []string{"tmux"}
if tmux.SocketPath != "" {
tmuxCmd = append(tmuxCmd, "-S", tmux.SocketPath)
} else if tmux.SocketName != "" {
tmuxCmd = append(tmuxCmd, "-L", tmux.SocketName)
}
if tmux.ConfigFile != "" {
tmuxCmd = append(tmuxCmd, "-f", tmux.ConfigFile)
}
tmuxCmd = append(tmuxCmd, args...)
return exec.Command(tmuxCmd[0], tmuxCmd[1:]...)
}
func (tmux Tmux) NewSession(name string, root string, windowName string) (string, error) {
cmd := tmux.cmd("new", "-Pd", "-s", name, "-n", windowName, "-c", root)
return tmux.commander.Exec(cmd)
}
func (tmux Tmux) SessionExists(name string) bool {
cmd := tmux.cmd("has-session", "-t", name)
res, err := tmux.commander.Exec(cmd)
return res == "" && err == nil
}
func (tmux Tmux) KillWindow(target string) error {
cmd := tmux.cmd("kill-window", "-t", target)
_, err := tmux.commander.Exec(cmd)
return err
}
func (tmux Tmux) NewWindow(target string, name string, root string) (string, error) {
cmd := tmux.cmd("neww", "-Pd", "-t", target, "-c", root, "-F", "#{window_id}", "-n", name)
return tmux.commander.Exec(cmd)
}
func (tmux Tmux) SendKeys(target string, command string) error {
cmd := tmux.cmd("send-keys", "-t", target, command, "Enter")
return tmux.commander.ExecSilently(cmd)
}
func (tmux Tmux) Attach(target string, stdin *os.File, stdout *os.File, stderr *os.File) error {
cmd := tmux.cmd("attach", "-d", "-t", target)
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
return tmux.commander.ExecSilently(cmd)
}
func (tmux Tmux) RenumberWindows(target string) error {
cmd := tmux.cmd("move-window", "-r", "-s", target, "-t", target)
_, err := tmux.commander.Exec(cmd)
return err
}
func (tmux Tmux) SplitWindow(target string, splitType string, root string) (string, error) {
args := []string{"split-window", "-Pd"}
switch splitType {
case VSplit:
args = append(args, "-v")
case HSplit:
args = append(args, "-h")
}
args = append(args, []string{"-t", target, "-c", root, "-F", "#{pane_id}"}...)
cmd := tmux.cmd(args...)
pane, err := tmux.commander.Exec(cmd)
if err != nil {
return "", err
}
return pane, nil
}
func (tmux Tmux) SelectLayout(target string, layoutType string) (string, error) {
cmd := tmux.cmd("select-layout", "-t", target, layoutType)
return tmux.commander.Exec(cmd)
}
func (tmux Tmux) SetEnv(target string, key string, value string) (string, error) {
cmd := tmux.cmd("setenv", "-t", target, key, value)
return tmux.commander.Exec(cmd)
}
func (tmux Tmux) StopSession(target string) (string, error) {
cmd := tmux.cmd("kill-session", "-t", target)
return tmux.commander.Exec(cmd)
}
func (tmux Tmux) SwitchClient(target string) error {
cmd := tmux.cmd("switch-client", "-t", target)
return tmux.commander.ExecSilently(cmd)
}
func (tmux Tmux) SessionName() (string, error) {
cmd := tmux.cmd("display-message", "-p", "#S")
sessionName, err := tmux.commander.Exec(cmd)
if err != nil {
return sessionName, err
}
return sessionName, nil
}
func (tmux Tmux) ListWindows(target string) ([]TmuxWindow, error) {
var windows []TmuxWindow
cmd := tmux.cmd("list-windows", "-F", "#{window_id};#{window_name};#{window_layout};#{pane_current_path}", "-t", target)
out, err := tmux.commander.Exec(cmd)
if err != nil {
return windows, err
}
windowsList := strings.Split(out, "\n")
for _, w := range windowsList {
windowInfo := strings.Split(w, ";")
window := TmuxWindow{
ID: windowInfo[0],
Name: windowInfo[1],
Layout: windowInfo[2],
Root: windowInfo[3],
}
windows = append(windows, window)
}
return windows, nil
}
func (tmux Tmux) ListPanes(target string) ([]TmuxPane, error) {
var panes []TmuxPane
cmd := tmux.cmd("list-panes", "-F", "#{pane_current_path}", "-t", target)
out, err := tmux.commander.Exec(cmd)
if err != nil {
return panes, err
}
panesList := strings.Split(out, "\n")
for _, p := range panesList {
paneInfo := strings.Split(p, ";")
pane := TmuxPane{
Root: paneInfo[0],
}
panes = append(panes, pane)
}
return panes, nil
}