forked from liamg/darktile
/
terminal.go
301 lines (246 loc) · 6.99 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
package terminal
import (
"bufio"
"context"
"fmt"
"io"
"os"
"sync"
"syscall"
"unsafe"
"github.com/liamg/aminal/buffer"
"github.com/liamg/aminal/config"
"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
activeBufferIndex uint8
lock sync.Mutex
pty *os.File
logger *zap.SugaredLogger
title string
size Winsize
config *config.Config
titleHandlers []chan bool
pauseChan chan bool
resumeChan chan bool
modes Modes
mouseMode MouseMode
bracketedPasteMode bool
isDirty bool
charWidth float32
charHeight float32
lastBuffer uint8
}
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 *os.File, logger *zap.SugaredLogger, config *config.Config) *Terminal {
t := &Terminal{
buffers: []*buffer.Buffer{
buffer.NewBuffer(1, 1, buffer.CellAttributes{
FgColour: config.ColourScheme.Foreground,
BgColour: config.ColourScheme.Background,
}),
buffer.NewBuffer(1, 1, buffer.CellAttributes{
FgColour: config.ColourScheme.Foreground,
BgColour: config.ColourScheme.Background,
}),
buffer.NewBuffer(1, 1, buffer.CellAttributes{
FgColour: config.ColourScheme.Foreground,
BgColour: config.ColourScheme.Background,
}),
},
pty: pty,
logger: logger,
config: config,
titleHandlers: []chan bool{},
pauseChan: make(chan bool, 1),
resumeChan: make(chan bool, 1),
modes: Modes{
ShowCursor: true,
},
}
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) UseMainBuffer() {
terminal.activeBufferIndex = MainBuffer
terminal.SetSize(uint(terminal.size.Width), uint(terminal.size.Height))
}
func (terminal *Terminal) UseAltBuffer() {
terminal.activeBufferIndex = AltBuffer
terminal.SetSize(uint(terminal.size.Width), uint(terminal.size.Height))
}
func (terminal *Terminal) UseInternalBuffer() {
terminal.pauseChan <- true
terminal.activeBufferIndex = InternalBuffer
terminal.SetSize(uint(terminal.size.Width), uint(terminal.size.Height))
}
func (terminal *Terminal) ExitInternalBuffer() {
terminal.activeBufferIndex = terminal.lastBuffer
terminal.resumeChan <- true
}
func (terminal *Terminal) ActiveBuffer() *buffer.Buffer {
return terminal.buffers[terminal.activeBufferIndex]
}
func (terminal *Terminal) UsingMainBuffer() bool {
return terminal.activeBufferIndex == 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) 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) 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().CursorLine() + 1
}
return terminal.ActiveBuffer().CursorLine()
}
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) 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)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go terminal.processInput(ctx, buffer)
for {
r, size, err := reader.ReadRune()
if err != nil {
if err == io.EOF {
break
}
return err
} else if size > 0 {
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()
terminal.size.Width = uint16(newCols)
terminal.size.Height = uint16(newLines)
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(terminal.pty.Fd()),
uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(&terminal.size)))
if err != 0 {
return fmt.Errorf("Failed to set terminal size vai ioctl: Error no %d", err)
}
terminal.ActiveBuffer().ResizeView(terminal.size.Width, terminal.size.Height)
return nil
}