forked from liamg/darktile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
376 lines (305 loc) · 9.04 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package terminal
import (
"bufio"
"fmt"
"io"
"sync"
"github.com/liamg/aminal/buffer"
"github.com/liamg/aminal/config"
"github.com/liamg/aminal/platform"
"go.uber.org/zap"
)
const (
MainBuffer uint8 = 0
AltBuffer uint8 = 1
InternalBuffer uint8 = 2
)
type MouseMode uint
const (
MouseModeNone MouseMode = iota
MouseModeX10
MouseModeVT200
MouseModeVT200Highlight
MouseModeButtonEvent
MouseModeAnyEvent
)
type Terminal struct {
program uint32
buffers []*buffer.Buffer
activeBuffer *buffer.Buffer
lock sync.Mutex
pty platform.Pty
logger *zap.SugaredLogger
title string
size Winsize
config *config.Config
titleHandlers []chan bool
resizeHandlers []chan bool
reverseHandlers []chan bool
modes Modes
mouseMode MouseMode
bracketedPasteMode bool
isDirty bool
charWidth float32
charHeight float32
lastBuffer uint8
terminalState *buffer.TerminalState
platformDependentSettings platform.PlatformDependentSettings
}
type Modes struct {
ShowCursor bool
ApplicationCursorKeys bool
BlinkingCursor bool
}
type Winsize struct {
Height uint16
Width uint16
x uint16 //ignored, but necessary for ioctl calls
y uint16 //ignored, but necessary for ioctl calls
}
func New(pty platform.Pty, logger *zap.SugaredLogger, config *config.Config) *Terminal {
t := &Terminal{
terminalState: buffer.NewTerminalState(1, 1, buffer.CellAttributes{
FgColour: config.ColourScheme.Foreground,
BgColour: config.ColourScheme.Background,
}, config.MaxLines),
pty: pty,
logger: logger,
config: config,
titleHandlers: []chan bool{},
modes: Modes{
ShowCursor: true,
},
platformDependentSettings: pty.GetPlatformDependentSettings(),
}
t.buffers = []*buffer.Buffer{
buffer.NewBuffer(t.terminalState),
buffer.NewBuffer(t.terminalState),
buffer.NewBuffer(t.terminalState),
}
t.activeBuffer = t.buffers[0]
return t
}
func (terminal *Terminal) SetProgram(program uint32) {
terminal.program = program
}
func (terminal *Terminal) SetBracketedPasteMode(enabled bool) {
terminal.bracketedPasteMode = enabled
}
func (terminal *Terminal) CheckDirty() bool {
d := terminal.isDirty
terminal.isDirty = false
return d || terminal.ActiveBuffer().IsDirty()
}
func (terminal *Terminal) SetDirty() {
terminal.isDirty = true
}
func (terminal *Terminal) IsApplicationCursorKeysModeEnabled() bool {
return terminal.modes.ApplicationCursorKeys
}
func (terminal *Terminal) SetMouseMode(mode MouseMode) {
terminal.mouseMode = mode
}
func (terminal *Terminal) GetMouseMode() MouseMode {
return terminal.mouseMode
}
func (terminal *Terminal) IsOSCTerminator(char rune) bool {
_, ok := terminal.platformDependentSettings.OSCTerminators[char]
return ok
}
func (terminal *Terminal) UseMainBuffer() {
terminal.activeBuffer = terminal.buffers[MainBuffer]
terminal.SetSize(uint(terminal.size.Width), uint(terminal.size.Height))
}
func (terminal *Terminal) UseAltBuffer() {
terminal.activeBuffer = terminal.buffers[AltBuffer]
terminal.SetSize(uint(terminal.size.Width), uint(terminal.size.Height))
}
func (terminal *Terminal) UseInternalBuffer() {
terminal.activeBuffer = terminal.buffers[InternalBuffer]
terminal.SetSize(uint(terminal.size.Width), uint(terminal.size.Height))
}
func (terminal *Terminal) ExitInternalBuffer() {
terminal.activeBuffer = terminal.buffers[terminal.lastBuffer]
}
func (terminal *Terminal) ActiveBuffer() *buffer.Buffer {
return terminal.activeBuffer
}
func (terminal *Terminal) UsingMainBuffer() bool {
return terminal.activeBuffer == terminal.buffers[MainBuffer]
}
func (terminal *Terminal) GetScrollOffset() uint {
return terminal.ActiveBuffer().GetScrollOffset()
}
func (terminal *Terminal) ScrollDown(lines uint16) {
terminal.ActiveBuffer().ScrollDown(lines)
}
func (terminal *Terminal) SetCharSize(w float32, h float32) {
terminal.charWidth = w
terminal.charHeight = h
}
func (terminal *Terminal) ScrollUp(lines uint16) {
terminal.ActiveBuffer().ScrollUp(lines)
}
func (terminal *Terminal) ScrollPageDown() {
terminal.ActiveBuffer().ScrollPageDown()
}
func (terminal *Terminal) ScrollPageUp() {
terminal.ActiveBuffer().ScrollPageUp()
}
func (terminal *Terminal) ScrollToEnd() {
terminal.ActiveBuffer().ScrollToEnd()
}
func (terminal *Terminal) GetVisibleLines() []buffer.Line {
return terminal.ActiveBuffer().GetVisibleLines()
}
func (terminal *Terminal) GetCell(col uint16, row uint16) *buffer.Cell {
return terminal.ActiveBuffer().GetCell(col, row)
}
func (terminal *Terminal) AttachTitleChangeHandler(handler chan bool) {
terminal.titleHandlers = append(terminal.titleHandlers, handler)
}
func (terminal *Terminal) AttachResizeHandler(handler chan bool) {
terminal.resizeHandlers = append(terminal.resizeHandlers, handler)
}
func (terminal *Terminal) AttachReverseHandler(handler chan bool) {
terminal.reverseHandlers = append(terminal.reverseHandlers, handler)
}
func (terminal *Terminal) Modes() Modes {
return terminal.modes
}
func (terminal *Terminal) emitTitleChange() {
for _, h := range terminal.titleHandlers {
go func(c chan bool) {
c <- true
}(h)
}
}
func (terminal *Terminal) emitResize() {
for _, h := range terminal.resizeHandlers {
go func(c chan bool) {
c <- true
}(h)
}
}
func (terminal *Terminal) emitReverse(reverse bool) {
for _, h := range terminal.reverseHandlers {
go func(c chan bool) {
c <- reverse
}(h)
}
}
func (terminal *Terminal) GetLogicalCursorX() uint16 {
if terminal.ActiveBuffer().CursorColumn() >= terminal.ActiveBuffer().Width() {
return 0
}
return terminal.ActiveBuffer().CursorColumn()
}
func (terminal *Terminal) GetLogicalCursorY() uint16 {
if terminal.ActiveBuffer().CursorColumn() >= terminal.ActiveBuffer().Width() {
return terminal.ActiveBuffer().CursorLineAbsolute() + 1
}
return terminal.ActiveBuffer().CursorLineAbsolute()
}
func (terminal *Terminal) GetTitle() string {
return terminal.title
}
func (terminal *Terminal) SetTitle(title string) {
terminal.title = title
terminal.emitTitleChange()
}
// Write sends data, i.e. locally typed keystrokes to the pty
func (terminal *Terminal) Write(data []byte) error {
_, err := terminal.pty.Write(data)
return err
}
func (terminal *Terminal) WriteReturn() error {
if terminal.terminalState.IsNewLineMode() {
return terminal.Write([]byte{0x0d, 0x0a})
} else {
return terminal.Write([]byte{0x0d})
}
}
func (terminal *Terminal) Paste(data []byte) error {
if terminal.bracketedPasteMode {
data = []byte(fmt.Sprintf("\x1b[200~%s\x1b[201~", string(data)))
}
_, err := terminal.pty.Write(data)
return err
}
// Read needs to be run on a goroutine, as it continually reads output to set on the terminal
func (terminal *Terminal) Read() error {
buffer := make(chan rune, 0xffff)
reader := bufio.NewReader(terminal.pty)
go terminal.processInput(buffer)
for {
r, _, err := reader.ReadRune()
if err != nil {
if err == io.EOF {
break
}
return err
}
buffer <- r
}
//clean exit
return nil
}
func (terminal *Terminal) Clear() {
terminal.ActiveBuffer().Clear()
}
func (terminal *Terminal) GetSize() (int, int) {
return int(terminal.size.Width), int(terminal.size.Height)
}
func (terminal *Terminal) SetSize(newCols uint, newLines uint) error {
terminal.lock.Lock()
defer terminal.lock.Unlock()
if terminal.size.Width == uint16(newCols) && terminal.size.Height == uint16(newLines) {
return nil
}
err := terminal.pty.Resize(int(newCols), int(newLines))
if err != nil {
return fmt.Errorf("Failed to set terminal size vai ioctl: Error no %d", err)
}
terminal.size.Width = uint16(newCols)
terminal.size.Height = uint16(newLines)
terminal.ActiveBuffer().ResizeView(terminal.size.Width, terminal.size.Height)
terminal.emitResize()
return nil
}
func (terminal *Terminal) SetAutoWrap(enabled bool) {
terminal.terminalState.AutoWrap = enabled
}
func (terminal *Terminal) IsAutoWrap() bool {
return terminal.terminalState.AutoWrap
}
func (terminal *Terminal) SetOriginMode(enabled bool) {
terminal.terminalState.OriginMode = enabled
terminal.ActiveBuffer().SetPosition(0, 0)
}
func (terminal *Terminal) SetInsertMode() {
terminal.terminalState.ReplaceMode = false
}
func (terminal *Terminal) SetReplaceMode() {
terminal.terminalState.ReplaceMode = true
}
func (terminal *Terminal) SetNewLineMode() {
terminal.terminalState.LineFeedMode = false
}
func (terminal *Terminal) SetLineFeedMode() {
terminal.terminalState.LineFeedMode = true
}
func (terminal *Terminal) ResetVerticalMargins() {
terminal.terminalState.ResetVerticalMargins()
}
func (terminal *Terminal) SetScreenMode(enabled bool) {
if terminal.terminalState.ScreenMode == enabled {
return
}
terminal.terminalState.ScreenMode = enabled
terminal.terminalState.CursorAttr.ReverseVideo()
for _, buffer := range terminal.buffers {
buffer.ReverseVideo()
}
terminal.emitReverse(enabled)
}