-
Notifications
You must be signed in to change notification settings - Fork 0
/
pane.go
127 lines (105 loc) · 2.31 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
package main
import (
"regexp"
"strings"
)
var reEscapeSequence = regexp.MustCompile(`\x1b\[([^m]+)m`)
type Pane struct {
ID string `json:"id,omitempty"`
Lines []string `json:"lines,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}
func CapturePane(tmux *Tmux, id string, args ...string) (*Pane, error) {
contents, err := tmux.CapturePane(append([]string{"-t", id}, args...)...)
if err != nil {
return nil, err
}
width, height, err := tmux.GetPaneSize()
if err != nil {
return nil, err
}
return &Pane{
ID: id,
Lines: strings.Split(strings.TrimRight(contents, "\n"), "\n"),
Width: width,
Height: height,
}, nil
}
func (pane *Pane) GetBufferXY(lines []string, x, y int) (int, int) {
for row, line := range lines {
offset := (len([]rune(line)) - 1) / pane.Width
if row+offset >= y {
x = x + (y-row)*pane.Width
y = row
break
}
y -= offset
}
return x, y
}
func (pane *Pane) GetScreenXY(lines []string, x, y int) (int, int) {
offset := 0
for row, line := range lines {
if row == y {
return x % pane.Width, y + x/pane.Width + offset
} else {
offset += (len([]rune(line)) - 1) / pane.Width
}
}
return x, y
}
func (pane *Pane) GetPrintable() []string {
printable := []string{}
for _, line := range pane.Lines {
line = reEscapeSequence.ReplaceAllLiteralString(line, ``)
inGrid := false
symbols := []rune(line)
for i := 0; i < len(symbols); i++ {
symbol := symbols[i]
if symbol == '\x0e' {
inGrid = true
symbols = append(symbols[:i], symbols[i+1:]...)
i--
continue
}
if symbol == '\x0f' {
inGrid = false
symbols = append(symbols[:i], symbols[i+1:]...)
i--
continue
}
if inGrid {
switch symbol {
case 'l':
symbols[i] = '┌'
case 'q':
symbols[i] = '─'
case 'w':
symbols[i] = '┬'
case 'k':
symbols[i] = '┐'
case 'x':
symbols[i] = '│'
case 't':
symbols[i] = '├'
case 'u':
symbols[i] = '┤'
case 'n':
symbols[i] = '┼'
case 'v':
symbols[i] = '┴'
case 'm':
symbols[i] = '└'
case 'j':
symbols[i] = '┘'
}
}
}
printable = append(printable, string(symbols))
}
return printable
}
func (pane *Pane) String() string {
return strings.Join(pane.Lines, "\n")
}