forked from jubnzv/go-tmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpane.go
129 lines (108 loc) · 2.5 KB
/
pane.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
// The MIT License (MIT)
// Copyright (C) 2019 Georgy Komarov <[email protected]>
package tmux
import (
"fmt"
"regexp"
"strconv"
"strings"
)
const (
paneParts = 7
)
type Pane struct {
ID int
SessionId int
SessionName string
WindowId int
WindowName string
WindowIndex int
Active bool
}
// Return list of panes. Optional arguments are define the search scope with
// tmux command keys (see tmux(1) manpage):
// list-panes [-as] [-F format] [-t target]
//
// * `-a`: target is ignored and all panes on the server are listed
// * `-s`: target is a session. If neither is given, target is a window (or
// the current window).
func ListPanes(args []string) ([]Pane, error) {
format := strings.Join([]string{
"#{session_id}",
"#{session_name}",
"#{window_id}",
"#{window_name}",
"#{window_index}",
"#{pane_id}",
"#{pane_active}",
}, ":")
args = append([]string{"list-panes", "-F", format}, args...)
out, _, err := RunCmd(args)
if err != nil {
return nil, err
}
outLines := strings.Split(out, "\n")
panes := []Pane{}
re := regexp.MustCompile(`\$([0-9]+):(.+):@([0-9]+):(.+):([0-9]+):%([0-9]+):([01])`)
for _, line := range outLines {
result := re.FindStringSubmatch(line)
if len(result) <= paneParts {
continue
}
sessionID, errAtoi := strconv.Atoi(result[1])
if errAtoi != nil {
return nil, errAtoi
}
windowID, errAtoi := strconv.Atoi(result[3])
if errAtoi != nil {
return nil, errAtoi
}
windowIndex, errAtoi := strconv.Atoi(result[5])
if errAtoi != nil {
return nil, errAtoi
}
paneIndex, errAtoi := strconv.Atoi(result[6])
if errAtoi != nil {
return nil, errAtoi
}
panes = append(panes, Pane{
SessionId: sessionID,
SessionName: result[2],
WindowId: windowID,
WindowName: result[4],
WindowIndex: windowIndex,
ID: paneIndex,
Active: result[7] == "1",
})
}
return panes, nil
}
// Returns current path for this pane.
func (p *Pane) GetCurrentPath() (string, error) {
args := []string{
"display-message",
"-P", "-F", "#{pane_current_path}"}
out, _, err := RunCmd(args)
if err != nil {
return "", err
}
// Remove trailing CR
out = out[:len(out)-1]
return out, nil
}
func (p *Pane) Capture() (string, error) {
args := []string{
"capture-pane",
"-t",
fmt.Sprintf("%%%d", p.ID),
"-p",
}
out, stdErr, err := RunCmd(args)
if err != nil {
return stdErr, err
}
// Do not remove the tailing CR,
// maybe it's important for the caller
// for capture-pane.
return out, nil
}