-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
323 lines (269 loc) · 5.98 KB
/
terminal.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package termemu
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"github.com/creack/pty"
)
// Terminal is a wrapper around a pty that sends rendering commands
// to a provided Frontend.
type Terminal interface {
SetFrontend(f Frontend)
Lock()
Unlock()
WithLock(func())
StartCommand(c *exec.Cmd) error
Write(b []byte) (int, error)
Size() (int, int)
Resize(int, int) error
Line(int) []rune
ANSILine(y int) string
LineColors(int) ([]Color, []Color)
StyledLine(x, w, y int) *Line
StyledLines(r Region) []*Line
DupTo(*os.File)
PrintTerminal() // for debugging
}
type terminal struct {
sync.Mutex
frontend Frontend
onAltScreen bool
mainScreen *screen
altScreen *screen
pty, tty *os.File
dup *os.File
viewFlags []bool
viewInts []int
viewStrings []string
}
// New makes a new terminal using the provided Frontend
func New(f Frontend) Terminal {
if f == nil {
f = &EmptyFrontend{}
}
t := &terminal{
frontend: f,
mainScreen: newScreen(f),
altScreen: newScreen(f),
viewFlags: make([]bool, viewFlagCount),
viewInts: make([]int, viewIntCount),
viewStrings: make([]string, viewStringCount),
}
var err error
t.pty, t.tty, err = pty.Open()
if err != nil {
return nil
}
t.Resize(t.Size())
go t.ptyReadLoop()
return t
}
func (t *terminal) SetFrontend(f Frontend) {
t.frontend = f
t.mainScreen.frontend = f
t.altScreen.frontend = f
}
// func NewNoPTY(f Frontend) Terminal {
// return &terminal{
// frontend: f,
// screen: screen{
// frontend: f,
// },
// }
// }
const termStr = "TERM=xterm-256color"
func (t *terminal) StartCommand(c *exec.Cmd) error {
if t.pty == nil {
return errors.New("pty not initialized")
}
if c.Env == nil {
c.Env = os.Environ()
}
found := false
for i, v := range c.Env {
if strings.HasPrefix(v, "TERM=") {
found = true
c.Env[i] = termStr
break
}
}
if !found {
c.Env = append(c.Env, termStr)
}
c.Stdout = t.tty
c.Stdin = t.tty
c.Stderr = t.tty
if c.SysProcAttr == nil {
c.SysProcAttr = &syscall.SysProcAttr{}
}
c.SysProcAttr.Setctty = true
c.SysProcAttr.Setsid = true
c.SysProcAttr.Ctty = int(t.tty.Fd())
err := c.Start()
if err != nil {
return err
}
return nil
}
func (t *terminal) Write(b []byte) (int, error) {
return t.pty.Write(b)
}
func (t *terminal) Size() (w, h int) {
return t.screen().size.X, t.screen().size.Y
}
type winsize struct {
wsRow uint16
wsCol uint16
wsXPixel uint16
wsYPixel uint16
}
func (t *terminal) Resize(w, h int) error {
err := pty.Setsize(t.pty, &pty.Winsize{
Rows: uint16(h),
Cols: uint16(w),
X: uint16(w * 8),
Y: uint16(h * 16),
})
if err != nil {
return err
}
t.mainScreen.setSize(w, h)
t.altScreen.setSize(w, h)
return nil
}
func (t *terminal) Line(y int) []rune {
if y >= t.screen().size.Y {
return nil
}
return t.screen().getLine(y)
}
func (t *terminal) ANSILine(y int) string {
if y >= t.screen().size.Y {
return ""
}
return t.screen().renderLineANSI(y)
}
func (t *terminal) LineColors(y int) (fg []Color, bg []Color) {
if y >= t.screen().size.Y {
return nil, nil
}
return t.screen().getLineColors(y)
}
func (t *terminal) StyledLine(x, w, y int) *Line {
return t.screen().StyledLine(x, w, y)
}
func (t *terminal) StyledLines(r Region) []*Line {
return t.screen().StyledLines(r)
}
func (t *terminal) DupTo(f *os.File) {
t.dup = f
}
func (t *terminal) PrintTerminal() {
t.screen().printScreen()
}
type MouseBtn byte
type MouseFlag byte
const (
// low 2 bits
MBtn1 MouseBtn = 0
MBtn2 MouseBtn = 1
MBtn3 MouseBtn = 2
MRelease MouseBtn = 3
mWhichBtn byte = 3
// flags
MShift MouseFlag = 4
MMeta MouseFlag = 8
MControl MouseFlag = 16
MMotion MouseFlag = 32
MWheel MouseFlag = 64
)
// x and y should start at 1
// wheel events should use btn1 for wheel up, btn2 for wheel down, true for press, and M_wheel for mods
func (t *terminal) SendMouseRaw(btn MouseBtn, press bool, mods MouseFlag, x, y int) {
switch t.viewInts[VIMouseMode] {
case MMNone:
return
case MMPress:
if !press {
return
}
case MMPressRelease:
if mods&MMotion != 0 {
return
}
case MMPressReleaseMove:
if byte(mods)&mWhichBtn == byte(MRelease) {
return
}
case MMPressReleaseMoveAll:
}
switch t.viewInts[VIMouseEncoding] {
case MEX10:
btnByte := (byte(btn) & mWhichBtn) | byte(mods)
if !press {
btnByte |= byte(MRelease)
}
if 32+x > 255 {
x = 255 - 32
}
if 32+y > 255 {
y = 255 - 32
}
t.Write([]byte("\033[M" + string(32+btnByte) + string(byte(32+x)) + string(byte(32+y))))
case MEUTF8:
btnByte := (byte(btn) & mWhichBtn) | byte(mods)
if !press {
btnByte |= byte(MRelease)
}
t.Write([]byte("\033[M" + string(32+btnByte) + string(rune(32+x)) + string(rune(32+y))))
case MESGR:
btnByte := (byte(btn) & mWhichBtn) | byte(mods)
pressByte := 'M'
if !press {
pressByte = 'm'
}
t.Write([]byte(fmt.Sprintf("\033[<%v;%v;%v%c", btnByte, x, y, pressByte)))
}
}
func (t *terminal) setViewFlag(flag ViewFlag, value bool) {
t.viewFlags[flag] = value
t.frontend.ViewFlagChanged(flag, value)
}
func (t *terminal) GetViewFlag(flag ViewFlag) bool {
return t.viewFlags[flag]
}
func (t *terminal) setViewInt(flag ViewInt, value int) {
t.viewInts[flag] = value
t.frontend.ViewIntChanged(flag, value)
}
func (t *terminal) GetViewInt(flag ViewInt) int {
return t.viewInts[flag]
}
func (t *terminal) setViewString(flag ViewString, value string) {
t.viewStrings[flag] = value
t.frontend.ViewStringChanged(flag, value)
}
func (t *terminal) GetViewString(flag ViewString) string {
return t.viewStrings[flag]
}
// returns screen and unlock fn, must defer the unlock fn
func (t *terminal) screen() *screen {
if t.onAltScreen {
return t.altScreen
}
return t.mainScreen
}
// calls f with screen locked
func (t *terminal) WithLock(f func()) {
t.Lock()
defer t.Unlock()
f()
}
func (t *terminal) switchScreen() {
t.onAltScreen = !t.onAltScreen
t.frontend.RegionChanged(Region{X: 0, Y: 0, X2: t.screen().size.X, Y2: t.screen().size.Y}, CRScreenSwitch)
}