-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
212 lines (175 loc) · 5.23 KB
/
main.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
package gomux
import (
"fmt"
"io"
"strings"
)
type Pane struct {
Number int
commands []string
window *Window
}
func NewPane(number int, window *Window) *Pane {
p := new(Pane)
p.Number = number
p.commands = make([]string, 0)
p.window = window
return p
}
type SplitAttr struct {
Directory string
}
func (this *Pane) Exec(command string) {
fmt.Fprintf(this.window.session.writer, "tmux send-keys -t \"%s\" \"%s\" %s\n", this.getTargetName(), strings.Replace(command, "\"", "\\\"", -1), "C-m")
}
func (this *Pane) Vsplit() *Pane {
fmt.Fprint(this.window.session.writer, splitWindow{h: true, t: this.getTargetName()})
return this.window.AddPane(this.Number + 1)
}
func (this *Pane) VsplitWAttr(attr SplitAttr) *Pane {
var c string
if attr.Directory != "" {
c = attr.Directory
} else if this.window.Directory != "" {
c = this.window.Directory
} else if this.window.session.Directory != "" {
c = this.window.session.Directory
}
fmt.Fprint(this.window.session.writer, splitWindow{h: true, t: this.getTargetName(), c: c})
return this.window.AddPane(this.Number + 1)
}
func (this *Pane) Split() *Pane {
fmt.Fprint(this.window.session.writer, splitWindow{v: true, t: this.getTargetName()})
return this.window.AddPane(this.Number + 1)
}
func (this *Pane) SplitWAttr(attr SplitAttr) *Pane {
var c string
if attr.Directory != "" {
c = attr.Directory
} else if this.window.Directory != "" {
c = this.window.Directory
} else if this.window.session.Directory != "" {
c = this.window.session.Directory
}
fmt.Fprint(this.window.session.writer, splitWindow{v: true, t: this.getTargetName(), c: c})
return this.window.AddPane(this.Number + 1)
}
func (this *Pane) ResizeRight(num int) {
this.resize("R", num)
}
func (this *Pane) ResizeLeft(num int) {
this.resize("L", num)
}
func (this *Pane) ResizeUp(num int) {
this.resize("U", num)
}
func (this *Pane) ResizeDown(num int) {
this.resize("U", num)
}
func (this *Pane) resize(prefix string, num int) {
fmt.Fprintf(this.window.session.writer, "tmux resize-pane -t \"%s\" -%s %v\n", this.getTargetName(), prefix, fmt.Sprint(num))
}
func (this *Pane) getTargetName() string {
return this.window.session.Name + ":" + fmt.Sprint(this.window.Number) + "." + fmt.Sprint(this.Number)
}
// Window Represent a tmux window. You usually should not create an instance of Window directly.
type Window struct {
Number int
Name string
Directory string
session *Session
panes []*Pane
split_commands []string
}
type WindowAttr struct {
Name string
Directory string
}
func createWindow(number int, attr WindowAttr, session *Session) *Window {
w := new(Window)
w.Name = attr.Name
w.Directory = attr.Directory
w.Number = number
w.session = session
w.panes = make([]*Pane, 0)
w.split_commands = make([]string, 0)
w.AddPane(0)
if number != 0 {
fmt.Fprint(session.writer, newWindow{t: w.t(), n: w.Name, c: attr.Directory})
}
fmt.Fprint(session.writer, renameWindow{t: w.t(), n: w.Name})
return w
}
func (this *Window) t() string {
return fmt.Sprintf("-t \"%s:%s\"", this.session.Name, fmt.Sprint(this.Number))
}
// Create a new Pane and add to this window
func (this *Window) AddPane(withNumber int) *Pane {
pane := NewPane(withNumber, this)
this.panes = append(this.panes, pane)
return pane
}
// Find and return the Pane object by its index in the panes slice
func (this *Window) Pane(number int) *Pane {
return this.panes[number]
}
// Executes a command on the first pane of this window
//
// // example
// // example
func (this *Window) Exec(command string) {
this.Pane(0).Exec(command)
}
func (this *Window) Select() {
fmt.Fprint(this.session.writer, selectWindow{t: this.session.Name + ":" + fmt.Sprint(this.Number)})
}
// Session represents a tmux session.
//
// Use the method NewSession to create a Session instance.
type Session struct {
Name string
Directory string
windows []*Window
directory string
next_window_number int
writer io.Writer
}
// Creates a new Tmux Session. It will kill any existing session with the provided name.
func NewSession(name string, writer io.Writer) *Session {
p := SessionAttr{
Name: name,
}
return NewSessionAttr(p, writer)
}
type SessionAttr struct {
Name string
Directory string
}
// Creates a new Tmux Session based on NewSessionAttr. It will kill any existing session with the provided name.
func NewSessionAttr(p SessionAttr, writer io.Writer) *Session {
s := new(Session)
s.writer = writer
s.Name = p.Name
s.Directory = p.Directory
s.windows = make([]*Window, 0)
fmt.Fprint(writer, newSession{d: true, s: p.Name, c: p.Directory, n: "tmp"})
return s
}
// KillSession sends a command to kill the tmux session
func KillSession(name string, writer io.Writer) {
fmt.Fprint(writer, killSession{t: name})
}
// Creates window with provided name for this session
func (this *Session) AddWindow(name string) *Window {
attr := WindowAttr{
Name: name,
}
return this.AddWindowAttr(attr)
}
// Creates window with provided name for this session
func (this *Session) AddWindowAttr(attr WindowAttr) *Window {
w := createWindow(this.next_window_number, attr, this)
this.windows = append(this.windows, w)
this.next_window_number = this.next_window_number + 1
return w
}