| 1 | // Copyright 2026 The TCell Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package vt provides common definitions for VT derived terminals and applications. |
| 16 | // This includes the venerable VT100, XTerm, and newer emulators such as Kitty and |
| 17 | // the Windows Terminal. |
| 18 | package vt |
| 19 | |
| 20 | import "fmt" |
| 21 | |
| 22 | // PrivateMode describes a DEC Private Mode. |
| 23 | type PrivateMode int |
| 24 | |
| 25 | const ( |
| 26 | PmAppCursor PrivateMode = 1 // Application cursor keys. |
| 27 | PmVT52 PrivateMode = 2 // Clear to enable VT52 compatibility (not supported). |
| 28 | PmColumns PrivateMode = 3 // Set to enable 132 columns, reset for 80 columns. |
| 29 | PmScrolling PrivateMode = 4 // Smooth scrolling (jump by default). |
| 30 | PmScreen PrivateMode = 5 // Set to reverse dark and light on screen. |
| 31 | PmOrigin PrivateMode = 6 // Coordinates are relative to margins. |
| 32 | PmAutoMargin PrivateMode = 7 // Automatically wrap at margin. |
| 33 | PmAutoRepeat PrivateMode = 8 // Enable automatic key repeat. |
| 34 | PmMouseX10 PrivateMode = 9 // Legacy (X10) mouse reporting. |
| 35 | PmBlinkCursor PrivateMode = 12 // Blinking (on) or steady (off) cursor. |
| 36 | PmPrintFF PrivateMode = 18 // Print form feed after printing screen. |
| 37 | PmPrintExtent PrivateMode = 19 // Print full screen (on) or scrolling region (off). |
| 38 | PmShowCursor PrivateMode = 25 // Show the cursor (default on). |
| 39 | PmCharSet PrivateMode = 42 // Enable national (on) or multinational (off) character sets. |
| 40 | PmLeftRightMargin PrivateMode = 69 // Enable left and right margins |
| 41 | PmMouseButton PrivateMode = 1000 // Report mouse button events. |
| 42 | PmMouseDrag PrivateMode = 1002 // Report mouse motion events when button depressed, requires PmMouseButton. |
| 43 | PmMouseMotion PrivateMode = 1003 // Report mouse motion events, requires PmMouseButton. |
| 44 | PmFocusReports PrivateMode = 1004 // Send focus gained or lost reports. |
| 45 | PmMouseSgr PrivateMode = 1006 // Use SGR sequences for mouse reports. |
| 46 | PmMouseSgrPixel PrivateMode = 1016 // Use SGR sequences for mouse reports, using pixel-level coordinates. |
| 47 | PmAltScreen PrivateMode = 1049 // 47 and 1047 are alternates, but we use 1049 |
| 48 | PmBracketedPaste PrivateMode = 2004 // Bracket pasted text with bracketed paste escape sequences. |
| 49 | PmSyncOutput PrivateMode = 2026 // Buffer output when enabled, updating screen when reset. |
| 50 | PmGraphemeClusters PrivateMode = 2027 // Support for grapheme cluster handling. |
| 51 | PmResizeReports PrivateMode = 2048 // Send in-band resize reports. |
| 52 | PmWin32Input PrivateMode = 9001 // Use Win32-Input-Mode for keyboard reports |
| 53 | ) |
| 54 | |
| 55 | // Enable returns the string used to enable this private mode. |
| 56 | func (pm PrivateMode) Enable() string { |
| 57 | return fmt.Sprintf("\x1b[?%dh", pm) |
| 58 | } |
| 59 | |
| 60 | // Disable returns the string used to disable this private mode. |
| 61 | func (pm PrivateMode) Disable() string { |
| 62 | return fmt.Sprintf("\x1b[?%dl", pm) |
| 63 | } |
| 64 | |
| 65 | // Query returns the string used to query the state of this private mode. |
| 66 | func (pm PrivateMode) Query() string { |
| 67 | return fmt.Sprintf("\x1b[?%d$p", pm) |
| 68 | } |
| 69 | |
| 70 | // Reply returns a string representing a query reply for the given mode and status. |
| 71 | func (pm PrivateMode) Reply(status ModeStatus) string { |
| 72 | return fmt.Sprintf("\x1b[?%d;%d$y", pm, status) |
| 73 | } |
| 74 | |
| 75 | // ModeStatus represents the status of the mode. |
| 76 | type ModeStatus int |
| 77 | |
| 78 | // AnsiMode are modes standardized in ECMA-48. |
| 79 | // They use CSI-h and CSI-l (no question mark). |
| 80 | type AnsiMode int |
| 81 | |
| 82 | // Enable returns the string used to enable this ANSI mode. |
| 83 | func (pm AnsiMode) Enable() string { |
| 84 | return fmt.Sprintf("\x1b[%dh", pm) |
| 85 | } |
| 86 | |
| 87 | // Disable returns the string used to disable this ANSI mode. |
| 88 | func (pm AnsiMode) Disable() string { |
| 89 | return fmt.Sprintf("\x1b[%dl", pm) |
| 90 | } |
| 91 | |
| 92 | // Query returns the string used to query the state of this ANSI mode. |
| 93 | func (pm AnsiMode) Query() string { |
| 94 | return fmt.Sprintf("\x1b[%d$p", pm) |
| 95 | } |
| 96 | |
| 97 | // Reply returns a string representing a query reply for the given mode and status. |
| 98 | func (pm AnsiMode) Reply(status ModeStatus) string { |
| 99 | return fmt.Sprintf("\x1b[%d;%d$y", pm, status) |
| 100 | } |
| 101 | |
| 102 | const ( |
| 103 | AmKeyboardAction AnsiMode = 2 // Lock the keyboard. |
| 104 | AmInsertReplace AnsiMode = 4 // Insert or replace characters when a new character is added. |
| 105 | AmSendReceive AnsiMode = 12 // XON or XOFF. |
| 106 | AmNewLineMode AnsiMode = 20 // If true, LF emits CR as well, and Return sends both CR and LF. |
| 107 | ) |
| 108 | |
| 109 | const ( |
| 110 | ModeNA ModeStatus = 0 // Mode is not supported (or unknown) |
| 111 | ModeOn ModeStatus = 1 // Mode is on (e.g. via CSI-h) |
| 112 | ModeOff ModeStatus = 2 // Mode is off (e.g. via CSI-l) |
| 113 | ModeOnLocked ModeStatus = 3 // Mode is hardwired on |
| 114 | ModeOffLocked ModeStatus = 4 // Mode is hardwired off |
| 115 | ) |
| 116 | |
| 117 | // Changeable indicates that the mode may be changed. |
| 118 | func (ms ModeStatus) Changeable() bool { |
| 119 | return ms == ModeOn || ms == ModeOff |
| 120 | } |