| 1 |
package vt |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
|
| 6 |
uv "github.com/charmbracelet/ultraviolet" |
| 7 |
"github.com/charmbracelet/x/ansi" |
| 8 |
) |
| 9 |
|
| 10 |
// DcsHandler is a function that handles a DCS escape sequence. |
| 11 |
type DcsHandler func(params ansi.Params, data []byte) bool |
| 12 |
|
| 13 |
// CsiHandler is a function that handles a CSI escape sequence. |
| 14 |
type CsiHandler func(params ansi.Params) bool |
| 15 |
|
| 16 |
// OscHandler is a function that handles an OSC escape sequence. |
| 17 |
type OscHandler func(data []byte) bool |
| 18 |
|
| 19 |
// ApcHandler is a function that handles an APC escape sequence. |
| 20 |
type ApcHandler func(data []byte) bool |
| 21 |
|
| 22 |
// SosHandler is a function that handles an SOS escape sequence. |
| 23 |
type SosHandler func(data []byte) bool |
| 24 |
|
| 25 |
// PmHandler is a function that handles a PM escape sequence. |
| 26 |
type PmHandler func(data []byte) bool |
| 27 |
|
| 28 |
// EscHandler is a function that handles an ESC escape sequence. |
| 29 |
type EscHandler func() bool |
| 30 |
|
| 31 |
// CcHandler is a function that handles a control character. |
| 32 |
type CcHandler func() bool |
| 33 |
|
| 34 |
// handlers contains the terminal's escape sequence handlers. |
| 35 |
type handlers struct { |
| 36 |
ccHandlers map[byte][]CcHandler |
| 37 |
dcsHandlers map[int][]DcsHandler |
| 38 |
csiHandlers map[int][]CsiHandler |
| 39 |
oscHandlers map[int][]OscHandler |
| 40 |
escHandler map[int][]EscHandler |
| 41 |
apcHandlers []ApcHandler |
| 42 |
sosHandlers []SosHandler |
| 43 |
pmHandlers []PmHandler |
| 44 |
} |
| 45 |
|
| 46 |
// RegisterDcsHandler registers a DCS escape sequence handler. |
| 47 |
func (h *handlers) RegisterDcsHandler(cmd int, handler DcsHandler) { |
| 48 |
if h.dcsHandlers == nil { |
| 49 |
h.dcsHandlers = make(map[int][]DcsHandler) |
| 50 |
} |
| 51 |
h.dcsHandlers[cmd] = append(h.dcsHandlers[cmd], handler) |
| 52 |
} |
| 53 |
|
| 54 |
// RegisterCsiHandler registers a CSI escape sequence handler. |
| 55 |
func (h *handlers) RegisterCsiHandler(cmd int, handler CsiHandler) { |
| 56 |
if h.csiHandlers == nil { |
| 57 |
h.csiHandlers = make(map[int][]CsiHandler) |
| 58 |
} |
| 59 |
h.csiHandlers[cmd] = append(h.csiHandlers[cmd], handler) |
| 60 |
} |
| 61 |
|
| 62 |
// RegisterOscHandler registers an OSC escape sequence handler. |
| 63 |
func (h *handlers) RegisterOscHandler(cmd int, handler OscHandler) { |
| 64 |
if h.oscHandlers == nil { |
| 65 |
h.oscHandlers = make(map[int][]OscHandler) |
| 66 |
} |
| 67 |
h.oscHandlers[cmd] = append(h.oscHandlers[cmd], handler) |
| 68 |
} |
| 69 |
|
| 70 |
// RegisterApcHandler registers an APC escape sequence handler. |
| 71 |
func (h *handlers) RegisterApcHandler(handler ApcHandler) { |
| 72 |
h.apcHandlers = append(h.apcHandlers, handler) |
| 73 |
} |
| 74 |
|
| 75 |
// RegisterSosHandler registers an SOS escape sequence handler. |
| 76 |
func (h *handlers) RegisterSosHandler(handler SosHandler) { |
| 77 |
h.sosHandlers = append(h.sosHandlers, handler) |
| 78 |
} |
| 79 |
|
| 80 |
// RegisterPmHandler registers a PM escape sequence handler. |
| 81 |
func (h *handlers) RegisterPmHandler(handler PmHandler) { |
| 82 |
h.pmHandlers = append(h.pmHandlers, handler) |
| 83 |
} |
| 84 |
|
| 85 |
// RegisterEscHandler registers an ESC escape sequence handler. |
| 86 |
func (h *handlers) RegisterEscHandler(cmd int, handler EscHandler) { |
| 87 |
if h.escHandler == nil { |
| 88 |
h.escHandler = make(map[int][]EscHandler) |
| 89 |
} |
| 90 |
h.escHandler[cmd] = append(h.escHandler[cmd], handler) |
| 91 |
} |
| 92 |
|
| 93 |
// registerCcHandler registers a control character handler. |
| 94 |
func (h *handlers) registerCcHandler(r byte, handler CcHandler) { |
| 95 |
if h.ccHandlers == nil { |
| 96 |
h.ccHandlers = make(map[byte][]CcHandler) |
| 97 |
} |
| 98 |
h.ccHandlers[r] = append(h.ccHandlers[r], handler) |
| 99 |
} |
| 100 |
|
| 101 |
// handleCc handles a control character. |
| 102 |
// It returns true if the control character was handled. |
| 103 |
func (h *handlers) handleCc(r byte) bool { |
| 104 |
// Reverse iterate over the handlers so that the last registered handler |
| 105 |
// is the first to be called. |
| 106 |
for i := len(h.ccHandlers[r]) - 1; i >= 0; i-- { |
| 107 |
if h.ccHandlers[r][i]() { |
| 108 |
return true |
| 109 |
} |
| 110 |
} |
| 111 |
return false |
| 112 |
} |
| 113 |
|
| 114 |
// handleDcs handles a DCS escape sequence. |
| 115 |
// It returns true if the sequence was handled. |
| 116 |
func (h *handlers) handleDcs(cmd ansi.Cmd, params ansi.Params, data []byte) bool { |
| 117 |
// Reverse iterate over the handlers so that the last registered handler |
| 118 |
// is the first to be called. |
| 119 |
if handlers, ok := h.dcsHandlers[int(cmd)]; ok { |
| 120 |
for i := len(handlers) - 1; i >= 0; i-- { |
| 121 |
if handlers[i](params, data) { |
| 122 |
return true |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
return false |
| 127 |
} |
| 128 |
|
| 129 |
// handleCsi handles a CSI escape sequence. |
| 130 |
// It returns true if the sequence was handled. |
| 131 |
func (h *handlers) handleCsi(cmd ansi.Cmd, params ansi.Params) bool { |
| 132 |
// Reverse iterate over the handlers so that the last registered handler |
| 133 |
// is the first to be called. |
| 134 |
if handlers, ok := h.csiHandlers[int(cmd)]; ok { |
| 135 |
for i := len(handlers) - 1; i >= 0; i-- { |
| 136 |
if handlers[i](params) { |
| 137 |
return true |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
return false |
| 142 |
} |
| 143 |
|
| 144 |
// handleOsc handles an OSC escape sequence. |
| 145 |
// It returns true if the sequence was handled. |
| 146 |
func (h *handlers) handleOsc(cmd int, data []byte) bool { |
| 147 |
// Reverse iterate over the handlers so that the last registered handler |
| 148 |
// is the first to be called. |
| 149 |
if handlers, ok := h.oscHandlers[cmd]; ok { |
| 150 |
for i := len(handlers) - 1; i >= 0; i-- { |
| 151 |
if handlers[i](data) { |
| 152 |
return true |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
return false |
| 157 |
} |
| 158 |
|
| 159 |
// handleApc handles an APC escape sequence. |
| 160 |
// It returns true if the sequence was handled. |
| 161 |
func (h *handlers) handleApc(data []byte) bool { |
| 162 |
// Reverse iterate over the handlers so that the last registered handler |
| 163 |
// is the first to be called. |
| 164 |
for i := len(h.apcHandlers) - 1; i >= 0; i-- { |
| 165 |
if h.apcHandlers[i](data) { |
| 166 |
return true |
| 167 |
} |
| 168 |
} |
| 169 |
return false |
| 170 |
} |
| 171 |
|
| 172 |
// handleSos handles an SOS escape sequence. |
| 173 |
// It returns true if the sequence was handled. |
| 174 |
func (h *handlers) handleSos(data []byte) bool { |
| 175 |
// Reverse iterate over the handlers so that the last registered handler |
| 176 |
// is the first to be called. |
| 177 |
for i := len(h.sosHandlers) - 1; i >= 0; i-- { |
| 178 |
if h.sosHandlers[i](data) { |
| 179 |
return true |
| 180 |
} |
| 181 |
} |
| 182 |
return false |
| 183 |
} |
| 184 |
|
| 185 |
// handlePm handles a PM escape sequence. |
| 186 |
// It returns true if the sequence was handled. |
| 187 |
func (h *handlers) handlePm(data []byte) bool { |
| 188 |
// Reverse iterate over the handlers so that the last registered handler |
| 189 |
// is the first to be called. |
| 190 |
for i := len(h.pmHandlers) - 1; i >= 0; i-- { |
| 191 |
if h.pmHandlers[i](data) { |
| 192 |
return true |
| 193 |
} |
| 194 |
} |
| 195 |
return false |
| 196 |
} |
| 197 |
|
| 198 |
// handleEsc handles an ESC escape sequence. |
| 199 |
// It returns true if the sequence was handled. |
| 200 |
func (h *handlers) handleEsc(cmd int) bool { |
| 201 |
// Reverse iterate over the handlers so that the last registered handler |
| 202 |
// is the first to be called. |
| 203 |
if handlers, ok := h.escHandler[cmd]; ok { |
| 204 |
for i := len(handlers) - 1; i >= 0; i-- { |
| 205 |
if handlers[i]() { |
| 206 |
return true |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
return false |
| 211 |
} |
| 212 |
|
| 213 |
// registerDefaultHandlers registers the default escape sequence handlers. |
| 214 |
func (e *Emulator) registerDefaultHandlers() { |
| 215 |
e.registerDefaultCcHandlers() |
| 216 |
e.registerDefaultCsiHandlers() |
| 217 |
e.registerDefaultEscHandlers() |
| 218 |
e.registerDefaultOscHandlers() |
| 219 |
} |
| 220 |
|
| 221 |
// registerDefaultCcHandlers registers the default control character handlers. |
| 222 |
func (e *Emulator) registerDefaultCcHandlers() { |
| 223 |
for i := byte(ansi.NUL); i <= ansi.US; i++ { |
| 224 |
switch i { |
| 225 |
case ansi.NUL: // Null [ansi.NUL] |
| 226 |
// Ignored |
| 227 |
e.registerCcHandler(i, func() bool { |
| 228 |
return true |
| 229 |
}) |
| 230 |
case ansi.BEL: // Bell [ansi.BEL] |
| 231 |
e.registerCcHandler(i, func() bool { |
| 232 |
if e.cb.Bell != nil { |
| 233 |
e.cb.Bell() |
| 234 |
} |
| 235 |
return true |
| 236 |
}) |
| 237 |
case ansi.BS: // Backspace [ansi.BS] |
| 238 |
e.registerCcHandler(i, func() bool { |
| 239 |
e.backspace() |
| 240 |
return true |
| 241 |
}) |
| 242 |
case ansi.HT: // Horizontal Tab [ansi.HT] |
| 243 |
e.registerCcHandler(i, func() bool { |
| 244 |
e.nextTab(1) |
| 245 |
return true |
| 246 |
}) |
| 247 |
case ansi.LF, ansi.VT, ansi.FF: |
| 248 |
// Line Feed [ansi.LF] |
| 249 |
// Vertical Tab [ansi.VT] |
| 250 |
// Form Feed [ansi.FF] |
| 251 |
e.registerCcHandler(i, func() bool { |
| 252 |
e.linefeed() |
| 253 |
return true |
| 254 |
}) |
| 255 |
case ansi.CR: // Carriage Return [ansi.CR] |
| 256 |
e.registerCcHandler(i, func() bool { |
| 257 |
e.carriageReturn() |
| 258 |
return true |
| 259 |
}) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
for i := byte(ansi.PAD); i <= byte(ansi.APC); i++ { |
| 264 |
switch i { |
| 265 |
case ansi.HTS: // Horizontal Tab Set [ansi.HTS] |
| 266 |
e.registerCcHandler(i, func() bool { |
| 267 |
e.horizontalTabSet() |
| 268 |
return true |
| 269 |
}) |
| 270 |
case ansi.RI: // Reverse Index [ansi.RI] |
| 271 |
e.registerCcHandler(i, func() bool { |
| 272 |
e.reverseIndex() |
| 273 |
return true |
| 274 |
}) |
| 275 |
case ansi.SO: // Shift Out [ansi.SO] |
| 276 |
e.registerCcHandler(i, func() bool { |
| 277 |
e.gl = 1 |
| 278 |
return true |
| 279 |
}) |
| 280 |
case ansi.SI: // Shift In [ansi.SI] |
| 281 |
e.registerCcHandler(i, func() bool { |
| 282 |
e.gl = 0 |
| 283 |
return true |
| 284 |
}) |
| 285 |
case ansi.IND: // Index [ansi.IND] |
| 286 |
e.registerCcHandler(i, func() bool { |
| 287 |
e.index() |
| 288 |
return true |
| 289 |
}) |
| 290 |
case ansi.SS2: // Single Shift 2 [ansi.SS2] |
| 291 |
e.registerCcHandler(i, func() bool { |
| 292 |
e.gsingle = 2 |
| 293 |
return true |
| 294 |
}) |
| 295 |
case ansi.SS3: // Single Shift 3 [ansi.SS3] |
| 296 |
e.registerCcHandler(i, func() bool { |
| 297 |
e.gsingle = 3 |
| 298 |
return true |
| 299 |
}) |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// registerDefaultOscHandlers registers the default OSC escape sequence handlers. |
| 305 |
func (e *Emulator) registerDefaultOscHandlers() { |
| 306 |
for _, cmd := range []int{ |
| 307 |
0, // Set window title and icon name |
| 308 |
1, // Set icon name |
| 309 |
2, // Set window title |
| 310 |
} { |
| 311 |
e.RegisterOscHandler(cmd, func(data []byte) bool { |
| 312 |
e.handleTitle(cmd, data) |
| 313 |
return true |
| 314 |
}) |
| 315 |
} |
| 316 |
|
| 317 |
e.RegisterOscHandler(7, func(data []byte) bool { |
| 318 |
// Report the shell current working directory |
| 319 |
// [ansi.NotifyWorkingDirectory]. |
| 320 |
e.handleWorkingDirectory(7, data) |
| 321 |
return true |
| 322 |
}) |
| 323 |
|
| 324 |
e.RegisterOscHandler(8, func(data []byte) bool { |
| 325 |
// Set/Query Hyperlink [ansi.SetHyperlink] |
| 326 |
e.handleHyperlink(8, data) |
| 327 |
return true |
| 328 |
}) |
| 329 |
|
| 330 |
for _, cmd := range []int{ |
| 331 |
10, // Set/Query foreground color |
| 332 |
11, // Set/Query background color |
| 333 |
12, // Set/Query cursor color |
| 334 |
110, // Reset foreground color |
| 335 |
111, // Reset background color |
| 336 |
112, // Reset cursor color |
| 337 |
} { |
| 338 |
e.RegisterOscHandler(cmd, func(data []byte) bool { |
| 339 |
e.handleDefaultColor(cmd, data) |
| 340 |
return true |
| 341 |
}) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
// registerDefaultEscHandlers registers the default ESC escape sequence handlers. |
| 346 |
func (e *Emulator) registerDefaultEscHandlers() { |
| 347 |
e.RegisterEscHandler('=', func() bool { |
| 348 |
// Keypad Application Mode [ansi.DECKPAM] |
| 349 |
e.setMode(ansi.NumericKeypadMode, ansi.ModeSet) |
| 350 |
return true |
| 351 |
}) |
| 352 |
|
| 353 |
e.RegisterEscHandler('>', func() bool { |
| 354 |
// Keypad Numeric Mode [ansi.DECKPNM] |
| 355 |
e.setMode(ansi.NumericKeypadMode, ansi.ModeReset) |
| 356 |
return true |
| 357 |
}) |
| 358 |
|
| 359 |
e.RegisterEscHandler('7', func() bool { |
| 360 |
// Save Cursor [ansi.DECSC] |
| 361 |
e.scr.SaveCursor() |
| 362 |
return true |
| 363 |
}) |
| 364 |
|
| 365 |
e.RegisterEscHandler('8', func() bool { |
| 366 |
// Restore Cursor [ansi.DECRC] |
| 367 |
e.scr.RestoreCursor() |
| 368 |
return true |
| 369 |
}) |
| 370 |
|
| 371 |
for _, cmd := range []int{ |
| 372 |
ansi.Command(0, '(', 'A'), // UK G0 |
| 373 |
ansi.Command(0, ')', 'A'), // UK G1 |
| 374 |
ansi.Command(0, '*', 'A'), // UK G2 |
| 375 |
ansi.Command(0, '+', 'A'), // UK G3 |
| 376 |
ansi.Command(0, '(', 'B'), // USASCII G0 |
| 377 |
ansi.Command(0, ')', 'B'), // USASCII G1 |
| 378 |
ansi.Command(0, '*', 'B'), // USASCII G2 |
| 379 |
ansi.Command(0, '+', 'B'), // USASCII G3 |
| 380 |
ansi.Command(0, '(', '0'), // Special G0 |
| 381 |
ansi.Command(0, ')', '0'), // Special G1 |
| 382 |
ansi.Command(0, '*', '0'), // Special G2 |
| 383 |
ansi.Command(0, '+', '0'), // Special G3 |
| 384 |
} { |
| 385 |
e.RegisterEscHandler(cmd, func() bool { |
| 386 |
// Select Character Set [ansi.SCS] |
| 387 |
c := ansi.Cmd(cmd) |
| 388 |
set := c.Intermediate() - '(' |
| 389 |
switch c.Final() { |
| 390 |
case 'A': // UK Character Set |
| 391 |
e.charsets[set] = UK |
| 392 |
case 'B': // USASCII Character Set |
| 393 |
e.charsets[set] = nil // USASCII is the default |
| 394 |
case '0': // Special Drawing Character Set |
| 395 |
e.charsets[set] = SpecialDrawing |
| 396 |
default: |
| 397 |
return false |
| 398 |
} |
| 399 |
return true |
| 400 |
}) |
| 401 |
} |
| 402 |
|
| 403 |
e.RegisterEscHandler('D', func() bool { |
| 404 |
// Index [ansi.IND] |
| 405 |
e.index() |
| 406 |
return true |
| 407 |
}) |
| 408 |
|
| 409 |
e.RegisterEscHandler('H', func() bool { |
| 410 |
// Horizontal Tab Set [ansi.HTS] |
| 411 |
e.horizontalTabSet() |
| 412 |
return true |
| 413 |
}) |
| 414 |
|
| 415 |
e.RegisterEscHandler('M', func() bool { |
| 416 |
// Reverse Index [ansi.RI] |
| 417 |
e.reverseIndex() |
| 418 |
return true |
| 419 |
}) |
| 420 |
|
| 421 |
e.RegisterEscHandler('c', func() bool { |
| 422 |
// Reset Initial State [ansi.RIS] |
| 423 |
e.fullReset() |
| 424 |
return true |
| 425 |
}) |
| 426 |
|
| 427 |
e.RegisterEscHandler('n', func() bool { |
| 428 |
// Locking Shift G2 [ansi.LS2] |
| 429 |
e.gl = 2 |
| 430 |
return true |
| 431 |
}) |
| 432 |
|
| 433 |
e.RegisterEscHandler('o', func() bool { |
| 434 |
// Locking Shift G3 [ansi.LS3] |
| 435 |
e.gl = 3 |
| 436 |
return true |
| 437 |
}) |
| 438 |
|
| 439 |
e.RegisterEscHandler('|', func() bool { |
| 440 |
// Locking Shift 3 Right [ansi.LS3R] |
| 441 |
e.gr = 3 |
| 442 |
return true |
| 443 |
}) |
| 444 |
|
| 445 |
e.RegisterEscHandler('}', func() bool { |
| 446 |
// Locking Shift 2 Right [ansi.LS2R] |
| 447 |
e.gr = 2 |
| 448 |
return true |
| 449 |
}) |
| 450 |
|
| 451 |
e.RegisterEscHandler('~', func() bool { |
| 452 |
// Locking Shift 1 Right [ansi.LS1R] |
| 453 |
e.gr = 1 |
| 454 |
return true |
| 455 |
}) |
| 456 |
} |
| 457 |
|
| 458 |
// registerDefaultCsiHandlers registers the default CSI escape sequence handlers. |
| 459 |
func (e *Emulator) registerDefaultCsiHandlers() { |
| 460 |
e.RegisterCsiHandler('@', func(params ansi.Params) bool { |
| 461 |
// Insert Character [ansi.ICH] |
| 462 |
n, _, _ := params.Param(0, 1) |
| 463 |
e.scr.InsertCell(n) |
| 464 |
return true |
| 465 |
}) |
| 466 |
|
| 467 |
e.RegisterCsiHandler('A', func(params ansi.Params) bool { |
| 468 |
// Cursor Up [ansi.CUU] |
| 469 |
n, _, _ := params.Param(0, 1) |
| 470 |
e.moveCursor(0, -n) |
| 471 |
return true |
| 472 |
}) |
| 473 |
|
| 474 |
e.RegisterCsiHandler('B', func(params ansi.Params) bool { |
| 475 |
// Cursor Down [ansi.CUD] |
| 476 |
n, _, _ := params.Param(0, 1) |
| 477 |
e.moveCursor(0, n) |
| 478 |
return true |
| 479 |
}) |
| 480 |
|
| 481 |
e.RegisterCsiHandler('C', func(params ansi.Params) bool { |
| 482 |
// Cursor Forward [ansi.CUF] |
| 483 |
n, _, _ := params.Param(0, 1) |
| 484 |
e.moveCursor(n, 0) |
| 485 |
return true |
| 486 |
}) |
| 487 |
|
| 488 |
e.RegisterCsiHandler('D', func(params ansi.Params) bool { |
| 489 |
// Cursor Backward [ansi.CUB] |
| 490 |
n, _, _ := params.Param(0, 1) |
| 491 |
e.moveCursor(-n, 0) |
| 492 |
return true |
| 493 |
}) |
| 494 |
|
| 495 |
e.RegisterCsiHandler('E', func(params ansi.Params) bool { |
| 496 |
// Cursor Next Line [ansi.CNL] |
| 497 |
n, _, _ := params.Param(0, 1) |
| 498 |
e.moveCursor(0, n) |
| 499 |
e.carriageReturn() |
| 500 |
return true |
| 501 |
}) |
| 502 |
|
| 503 |
e.RegisterCsiHandler('F', func(params ansi.Params) bool { |
| 504 |
// Cursor Previous Line [ansi.CPL] |
| 505 |
n, _, _ := params.Param(0, 1) |
| 506 |
e.moveCursor(0, -n) |
| 507 |
e.carriageReturn() |
| 508 |
return true |
| 509 |
}) |
| 510 |
|
| 511 |
e.RegisterCsiHandler('G', func(params ansi.Params) bool { |
| 512 |
// Cursor Horizontal Absolute [ansi.CHA] |
| 513 |
n, _, _ := params.Param(0, 1) |
| 514 |
_, y := e.scr.CursorPosition() |
| 515 |
e.setCursor(n-1, y) |
| 516 |
return true |
| 517 |
}) |
| 518 |
|
| 519 |
e.RegisterCsiHandler('H', func(params ansi.Params) bool { |
| 520 |
// Cursor Position [ansi.CUP] |
| 521 |
width, height := e.Width(), e.Height() |
| 522 |
row, _, _ := params.Param(0, 1) |
| 523 |
col, _, _ := params.Param(1, 1) |
| 524 |
if row < 1 { |
| 525 |
row = 1 |
| 526 |
} |
| 527 |
if col < 1 { |
| 528 |
col = 1 |
| 529 |
} |
| 530 |
y := min(height-1, row-1) |
| 531 |
x := min(width-1, col-1) |
| 532 |
e.setCursorPosition(x, y) |
| 533 |
return true |
| 534 |
}) |
| 535 |
|
| 536 |
e.RegisterCsiHandler('I', func(params ansi.Params) bool { |
| 537 |
// Cursor Horizontal Tabulation [ansi.CHT] |
| 538 |
n, _, _ := params.Param(0, 1) |
| 539 |
e.nextTab(n) |
| 540 |
return true |
| 541 |
}) |
| 542 |
|
| 543 |
e.RegisterCsiHandler('J', func(params ansi.Params) bool { |
| 544 |
// Erase in Display [ansi.ED] |
| 545 |
n, _, _ := params.Param(0, 0) |
| 546 |
width, height := e.Width(), e.Height() |
| 547 |
x, y := e.scr.CursorPosition() |
| 548 |
switch n { |
| 549 |
case 0: // Erase screen below (from after cursor position) |
| 550 |
rect1 := uv.Rect(x, y, width, 1) // cursor to end of line |
| 551 |
rect2 := uv.Rect(0, y+1, width, height-y-1) // next line onwards |
| 552 |
e.scr.FillArea(e.scr.blankCell(), rect1) |
| 553 |
e.scr.FillArea(e.scr.blankCell(), rect2) |
| 554 |
case 1: // Erase screen above (including cursor) |
| 555 |
rect := uv.Rect(0, 0, width, y+1) |
| 556 |
e.scr.FillArea(e.scr.blankCell(), rect) |
| 557 |
case 2: // erase screen |
| 558 |
fallthrough |
| 559 |
case 3: // erase display |
| 560 |
//nolint:godox |
| 561 |
// TODO: Scrollback buffer support? |
| 562 |
e.scr.Clear() |
| 563 |
default: |
| 564 |
return false |
| 565 |
} |
| 566 |
return true |
| 567 |
}) |
| 568 |
|
| 569 |
e.RegisterCsiHandler('K', func(params ansi.Params) bool { |
| 570 |
// Erase in Line [ansi.EL] |
| 571 |
n, _, _ := params.Param(0, 0) |
| 572 |
// NOTE: Erase Line (EL) erases all character attributes but not cell |
| 573 |
// bg color. |
| 574 |
x, y := e.scr.CursorPosition() |
| 575 |
w := e.scr.Width() |
| 576 |
|
| 577 |
switch n { |
| 578 |
case 0: // Erase from cursor to end of line |
| 579 |
e.eraseCharacter(w - x) |
| 580 |
case 1: // Erase from start of line to cursor |
| 581 |
rect := uv.Rect(0, y, x+1, 1) |
| 582 |
e.scr.FillArea(e.scr.blankCell(), rect) |
| 583 |
case 2: // Erase entire line |
| 584 |
rect := uv.Rect(0, y, w, 1) |
| 585 |
e.scr.FillArea(e.scr.blankCell(), rect) |
| 586 |
default: |
| 587 |
return false |
| 588 |
} |
| 589 |
return true |
| 590 |
}) |
| 591 |
|
| 592 |
e.RegisterCsiHandler('L', func(params ansi.Params) bool { |
| 593 |
// Insert Line [ansi.IL] |
| 594 |
n, _, _ := params.Param(0, 1) |
| 595 |
if e.scr.InsertLine(n) { |
| 596 |
// Move the cursor to the left margin. |
| 597 |
e.scr.setCursorX(0, true) |
| 598 |
} |
| 599 |
return true |
| 600 |
}) |
| 601 |
|
| 602 |
e.RegisterCsiHandler('M', func(params ansi.Params) bool { |
| 603 |
// Delete Line [ansi.DL] |
| 604 |
n, _, _ := params.Param(0, 1) |
| 605 |
if e.scr.DeleteLine(n) { |
| 606 |
// If the line was deleted successfully, move the cursor to the |
| 607 |
// left. |
| 608 |
// Move the cursor to the left margin. |
| 609 |
e.scr.setCursorX(0, true) |
| 610 |
} |
| 611 |
return true |
| 612 |
}) |
| 613 |
|
| 614 |
e.RegisterCsiHandler('P', func(params ansi.Params) bool { |
| 615 |
// Delete Character [ansi.DCH] |
| 616 |
n, _, _ := params.Param(0, 1) |
| 617 |
e.scr.DeleteCell(n) |
| 618 |
return true |
| 619 |
}) |
| 620 |
|
| 621 |
e.RegisterCsiHandler('S', func(params ansi.Params) bool { |
| 622 |
// Scroll Up [ansi.SU] |
| 623 |
n, _, _ := params.Param(0, 1) |
| 624 |
e.scr.ScrollUp(n) |
| 625 |
return true |
| 626 |
}) |
| 627 |
|
| 628 |
e.RegisterCsiHandler('T', func(params ansi.Params) bool { |
| 629 |
// Scroll Down [ansi.SD] |
| 630 |
n, _, _ := params.Param(0, 1) |
| 631 |
e.scr.ScrollDown(n) |
| 632 |
return true |
| 633 |
}) |
| 634 |
|
| 635 |
e.RegisterCsiHandler(ansi.Command('?', 0, 'W'), func(params ansi.Params) bool { |
| 636 |
// Set Tab at Every 8 Columns [ansi.DECST8C] |
| 637 |
if len(params) == 1 && params[0] == 5 { |
| 638 |
e.resetTabStops() |
| 639 |
return true |
| 640 |
} |
| 641 |
return false |
| 642 |
}) |
| 643 |
|
| 644 |
e.RegisterCsiHandler('X', func(params ansi.Params) bool { |
| 645 |
// Erase Character [ansi.ECH] |
| 646 |
n, _, _ := params.Param(0, 1) |
| 647 |
e.eraseCharacter(n) |
| 648 |
return true |
| 649 |
}) |
| 650 |
|
| 651 |
e.RegisterCsiHandler('Z', func(params ansi.Params) bool { |
| 652 |
// Cursor Backward Tabulation [ansi.CBT] |
| 653 |
n, _, _ := params.Param(0, 1) |
| 654 |
e.prevTab(n) |
| 655 |
return true |
| 656 |
}) |
| 657 |
|
| 658 |
e.RegisterCsiHandler('`', func(params ansi.Params) bool { |
| 659 |
// Horizontal Position Absolute [ansi.HPA] |
| 660 |
n, _, _ := params.Param(0, 1) |
| 661 |
width := e.Width() |
| 662 |
_, y := e.scr.CursorPosition() |
| 663 |
e.setCursorPosition(min(width-1, n-1), y) |
| 664 |
return true |
| 665 |
}) |
| 666 |
|
| 667 |
e.RegisterCsiHandler('a', func(params ansi.Params) bool { |
| 668 |
// Horizontal Position Relative [ansi.HPR] |
| 669 |
n, _, _ := params.Param(0, 1) |
| 670 |
width := e.Width() |
| 671 |
x, y := e.scr.CursorPosition() |
| 672 |
e.setCursorPosition(min(width-1, x+n), y) |
| 673 |
return true |
| 674 |
}) |
| 675 |
|
| 676 |
e.RegisterCsiHandler('b', func(params ansi.Params) bool { |
| 677 |
// Repeat Previous Character [ansi.REP] |
| 678 |
n, _, _ := params.Param(0, 1) |
| 679 |
e.repeatPreviousCharacter(n) |
| 680 |
return true |
| 681 |
}) |
| 682 |
|
| 683 |
e.RegisterCsiHandler('c', func(params ansi.Params) bool { |
| 684 |
// Primary Device Attributes [ansi.DA1] |
| 685 |
n, _, _ := params.Param(0, 0) |
| 686 |
if n != 0 { |
| 687 |
return false |
| 688 |
} |
| 689 |
|
| 690 |
// Do we fully support VT220? |
| 691 |
_, _ = io.WriteString(e.pw, ansi.PrimaryDeviceAttributes( |
| 692 |
62, // VT220 |
| 693 |
1, // 132 columns |
| 694 |
6, // Selective Erase |
| 695 |
22, // ANSI color |
| 696 |
)) |
| 697 |
return true |
| 698 |
}) |
| 699 |
|
| 700 |
e.RegisterCsiHandler(ansi.Command('>', 0, 'c'), func(params ansi.Params) bool { |
| 701 |
// Secondary Device Attributes [ansi.DA2] |
| 702 |
n, _, _ := params.Param(0, 0) |
| 703 |
if n != 0 { |
| 704 |
return false |
| 705 |
} |
| 706 |
|
| 707 |
// Do we fully support VT220? |
| 708 |
_, _ = io.WriteString(e.pw, ansi.SecondaryDeviceAttributes( |
| 709 |
1, // VT220 |
| 710 |
10, // Version 1.0 |
| 711 |
0, // ROM Cartridge is always zero |
| 712 |
)) |
| 713 |
return true |
| 714 |
}) |
| 715 |
|
| 716 |
e.RegisterCsiHandler('d', func(params ansi.Params) bool { |
| 717 |
// Vertical Position Absolute [ansi.VPA] |
| 718 |
n, _, _ := params.Param(0, 1) |
| 719 |
height := e.Height() |
| 720 |
x, _ := e.scr.CursorPosition() |
| 721 |
e.setCursorPosition(x, min(height-1, n-1)) |
| 722 |
return true |
| 723 |
}) |
| 724 |
|
| 725 |
e.RegisterCsiHandler('e', func(params ansi.Params) bool { |
| 726 |
// Vertical Position Relative [ansi.VPR] |
| 727 |
n, _, _ := params.Param(0, 1) |
| 728 |
height := e.Height() |
| 729 |
x, y := e.scr.CursorPosition() |
| 730 |
e.setCursorPosition(x, min(height-1, y+n)) |
| 731 |
return true |
| 732 |
}) |
| 733 |
|
| 734 |
e.RegisterCsiHandler('f', func(params ansi.Params) bool { |
| 735 |
// Horizontal and Vertical Position [ansi.HVP] |
| 736 |
width, height := e.Width(), e.Height() |
| 737 |
row, _, _ := params.Param(0, 1) |
| 738 |
col, _, _ := params.Param(1, 1) |
| 739 |
y := min(height-1, row-1) |
| 740 |
x := min(width-1, col-1) |
| 741 |
e.setCursor(x, y) |
| 742 |
return true |
| 743 |
}) |
| 744 |
|
| 745 |
e.RegisterCsiHandler('g', func(params ansi.Params) bool { |
| 746 |
// Tab Clear [ansi.TBC] |
| 747 |
value, _, _ := params.Param(0, 0) |
| 748 |
switch value { |
| 749 |
case 0: |
| 750 |
x, _ := e.scr.CursorPosition() |
| 751 |
e.tabstops.Reset(x) |
| 752 |
case 3: |
| 753 |
e.tabstops.Clear() |
| 754 |
default: |
| 755 |
return false |
| 756 |
} |
| 757 |
|
| 758 |
return true |
| 759 |
}) |
| 760 |
|
| 761 |
e.RegisterCsiHandler('h', func(params ansi.Params) bool { |
| 762 |
// Set Mode [ansi.SM] - ANSI |
| 763 |
e.handleMode(params, true, true) |
| 764 |
return true |
| 765 |
}) |
| 766 |
|
| 767 |
e.RegisterCsiHandler(ansi.Command('?', 0, 'h'), func(params ansi.Params) bool { |
| 768 |
// Set Mode [ansi.SM] - DEC |
| 769 |
e.handleMode(params, true, false) |
| 770 |
return true |
| 771 |
}) |
| 772 |
|
| 773 |
e.RegisterCsiHandler('l', func(params ansi.Params) bool { |
| 774 |
// Reset Mode [ansi.RM] - ANSI |
| 775 |
e.handleMode(params, false, true) |
| 776 |
return true |
| 777 |
}) |
| 778 |
|
| 779 |
e.RegisterCsiHandler(ansi.Command('?', 0, 'l'), func(params ansi.Params) bool { |
| 780 |
// Reset Mode [ansi.RM] - DEC |
| 781 |
e.handleMode(params, false, false) |
| 782 |
return true |
| 783 |
}) |
| 784 |
|
| 785 |
e.RegisterCsiHandler('m', func(params ansi.Params) bool { |
| 786 |
// Select Graphic Rendition [ansi.SGR] |
| 787 |
e.handleSgr(params) |
| 788 |
return true |
| 789 |
}) |
| 790 |
|
| 791 |
e.RegisterCsiHandler('n', func(params ansi.Params) bool { |
| 792 |
// Device Status Report [ansi.DSR] |
| 793 |
n, _, ok := params.Param(0, 1) |
| 794 |
if !ok || n == 0 { |
| 795 |
return false |
| 796 |
} |
| 797 |
|
| 798 |
switch n { |
| 799 |
case 5: // Operating Status |
| 800 |
// We're always ready ;) |
| 801 |
// See: https://vt100.net/docs/vt510-rm/DSR-OS.html |
| 802 |
_, _ = io.WriteString(e.pw, ansi.DeviceStatusReport(ansi.DECStatusReport(0))) |
| 803 |
case 6: // Cursor Position Report [ansi.CPR] |
| 804 |
x, y := e.scr.CursorPosition() |
| 805 |
_, _ = io.WriteString(e.pw, ansi.CursorPositionReport(y+1, x+1)) |
| 806 |
default: |
| 807 |
return false |
| 808 |
} |
| 809 |
|
| 810 |
return true |
| 811 |
}) |
| 812 |
|
| 813 |
e.RegisterCsiHandler(ansi.Command('?', 0, 'n'), func(params ansi.Params) bool { |
| 814 |
n, _, ok := params.Param(0, 1) |
| 815 |
if !ok || n == 0 { |
| 816 |
return false |
| 817 |
} |
| 818 |
|
| 819 |
switch n { |
| 820 |
case 6: // Extended Cursor Position Report [ansi.DECXCPR] |
| 821 |
x, y := e.scr.CursorPosition() |
| 822 |
_, _ = io.WriteString(e.pw, ansi.ExtendedCursorPositionReport(y+1, x+1, 0)) // We don't support page numbers //nolint:errcheck |
| 823 |
default: |
| 824 |
return false |
| 825 |
} |
| 826 |
|
| 827 |
return true |
| 828 |
}) |
| 829 |
|
| 830 |
e.RegisterCsiHandler(ansi.Command(0, '$', 'p'), func(params ansi.Params) bool { |
| 831 |
// Request Mode [ansi.DECRQM] - ANSI |
| 832 |
e.handleRequestMode(params, true) |
| 833 |
return true |
| 834 |
}) |
| 835 |
|
| 836 |
e.RegisterCsiHandler(ansi.Command('?', '$', 'p'), func(params ansi.Params) bool { |
| 837 |
// Request Mode [ansi.DECRQM] - DEC |
| 838 |
e.handleRequestMode(params, false) |
| 839 |
return true |
| 840 |
}) |
| 841 |
|
| 842 |
e.RegisterCsiHandler(ansi.Command(0, ' ', 'q'), func(params ansi.Params) bool { |
| 843 |
// Set Cursor Style [ansi.DECSCUSR] |
| 844 |
n := 1 |
| 845 |
if param, _, ok := params.Param(0, 0); ok && param > n { |
| 846 |
n = param |
| 847 |
} |
| 848 |
blink := n == 0 || n%2 == 1 |
| 849 |
style := n / 2 |
| 850 |
if !blink { |
| 851 |
style-- |
| 852 |
} |
| 853 |
e.scr.setCursorStyle(CursorStyle(style), blink) |
| 854 |
return true |
| 855 |
}) |
| 856 |
|
| 857 |
e.RegisterCsiHandler('r', func(params ansi.Params) bool { |
| 858 |
// Set Top and Bottom Margins [ansi.DECSTBM] |
| 859 |
top, _, _ := params.Param(0, 1) |
| 860 |
if top < 1 { |
| 861 |
top = 1 |
| 862 |
} |
| 863 |
|
| 864 |
height := e.Height() |
| 865 |
bottom, _ := e.parser.Param(1, height) |
| 866 |
if bottom < 1 { |
| 867 |
bottom = height |
| 868 |
} |
| 869 |
|
| 870 |
if top >= bottom { |
| 871 |
return false |
| 872 |
} |
| 873 |
|
| 874 |
// Rect is [x, y) which means y is exclusive. So the top margin |
| 875 |
// is the top of the screen minus one. |
| 876 |
e.scr.setVerticalMargins(top-1, bottom) |
| 877 |
|
| 878 |
// Move the cursor to the top-left of the screen or scroll region |
| 879 |
// depending on [ansi.DECOM]. |
| 880 |
e.setCursorPosition(0, 0) |
| 881 |
return true |
| 882 |
}) |
| 883 |
|
| 884 |
e.RegisterCsiHandler('s', func(params ansi.Params) bool { |
| 885 |
// Set Left and Right Margins [ansi.DECSLRM] |
| 886 |
// These conflict with each other. When [ansi.DECSLRM] is set, the we |
| 887 |
// set the left and right margins. Otherwise, we save the cursor |
| 888 |
// position. |
| 889 |
|
| 890 |
if e.isModeSet(ansi.LeftRightMarginMode) { |
| 891 |
// Set Left Right Margins [ansi.DECSLRM] |
| 892 |
left, _, _ := params.Param(0, 1) |
| 893 |
if left < 1 { |
| 894 |
left = 1 |
| 895 |
} |
| 896 |
|
| 897 |
width := e.Width() |
| 898 |
right, _, _ := params.Param(1, width) |
| 899 |
if right < 1 { |
| 900 |
right = width |
| 901 |
} |
| 902 |
|
| 903 |
if left >= right { |
| 904 |
return false |
| 905 |
} |
| 906 |
|
| 907 |
e.scr.setHorizontalMargins(left-1, right) |
| 908 |
|
| 909 |
// Move the cursor to the top-left of the screen or scroll region |
| 910 |
// depending on [ansi.DECOM]. |
| 911 |
e.setCursorPosition(0, 0) |
| 912 |
} else { |
| 913 |
// Save Current Cursor Position [ansi.SCOSC] |
| 914 |
e.scr.SaveCursor() |
| 915 |
} |
| 916 |
|
| 917 |
return true |
| 918 |
}) |
| 919 |
} |