forked from thavlik/go-tmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
172 lines (149 loc) · 3.69 KB
/
session.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
// The MIT License (MIT)
// Copyright (C) 2019 Georgy Komarov <jubnzv@gmail.com>
package tmux
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
type Session struct {
Name string // Session name
Id int // Session id
StartDirectory string // Path to window start directory
Windows []Window // List of windows used on session initialization
}
// Checks tmux rules for sessions naming. Reference:
// https://github.com/tmux/tmux/blob/5489796737108cb9bba01f831421e531a50b946b/session.c#L238
func checkSessionName(name string) bool {
if len(name) == 0 {
return false
}
if strings.Contains(name, ":") {
return false
}
if strings.Contains(name, ".") {
return false
}
return true
}
// Add window to session configuration. This will change only in-library
// session representation. Used for initial configuration before creating new
// session.
func (s *Session) AddWindow(window Window) {
s.Windows = append(s.Windows, window)
}
func (s *Session) InitSession() error {
if len(s.Windows) == 0 {
return errors.New("Nothing to do.")
}
return nil
}
// List all windows in the current session.
func (s *Session) ListWindows() ([]Window, error) {
args := []string{
"list-windows",
"-t", s.Name,
"-F", "#{window_id}:#{window_name}:#{pane_current_path}"}
out, _, err := RunCmd(args)
if err != nil {
return nil, err
}
outLines := strings.Split(out, "\n")
windows := []Window{}
re := regexp.MustCompile(`@([0-9]+):(.+):(.+)`)
for _, line := range outLines {
result := re.FindStringSubmatch(line)
if len(result) < 4 {
continue
}
id, err_atoi := strconv.Atoi(result[1])
if err_atoi != nil {
return nil, err_atoi
}
windows = append(windows, Window{
Name: result[2],
Id: id,
StartDirectory: result[3],
SessionName: s.Name,
SessionId: s.Id})
}
return windows, nil
}
// Attach to existing tmux session.
func (s *Session) AttachSession() error {
args := []string{}
// If run inside tmux, switch the current session to the new one.
if !IsInsideTmux() {
args = append(args, "attach-session", "-t", s.Name)
} else {
args = append(args, "switch-client", "-t", s.Name)
}
if err := ExecCmd(args); err != nil {
return err
}
return nil
}
// Detach from current session.
func (s *Session) DettachSession() error {
args := []string{
"detach-client",
"-s", s.Name}
if err := ExecCmd(args); err != nil {
return err
}
return nil
}
// Create a new window.
func (s *Session) NewWindow(name string) (window Window, err error) {
args := []string{
"new-window",
"-d",
"-t", fmt.Sprintf("%s:", s.Name),
"-n", name,
"-F", "#{window_id}:#{window_name}", "-P"}
out, _, err_exec := RunCmd(args)
if err_exec != nil {
return window, err_exec
}
re := regexp.MustCompile(`@([0-9]+):(.+)`)
result := re.FindStringSubmatch(out)
if len(result) < 3 {
return window, errors.New("Error creating new window")
}
id, err_atoi := strconv.Atoi(result[1])
if err_atoi != nil {
return window, err_atoi
}
pane := Pane{
SessionId: s.Id,
SessionName: s.Name,
WindowId: id,
WindowName: result[2],
WindowIndex: 0}
new_window := Window{
Name: result[2],
Id: id,
SessionName: s.Name,
SessionId: s.Id,
Panes: []Pane{pane}}
return new_window, nil
}
// Return list with all panes for this session.
func (s *Session) ListPanes() ([]Pane, error) {
return ListPanes([]string{"-s", "-t", s.Name})
}
// Return name of attached tmux session.
func GetAttachedSessionName() (string, error) {
args := []string{
"display-message",
"-p", "#S"}
out, _, err := RunCmd(args)
if err != nil {
return "", err
}
// Remove trailing CR
out = out[:len(out)-1]
return out, nil
}