tinky-termcap 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ /**
2
+ * @fileoverview Terminal feature query definitions for capability detection.
3
+ *
4
+ * This module defines the query sequences and response patterns for detecting
5
+ * various terminal capabilities. Each feature is represented as a `TermFeature`
6
+ * object containing the ANSI escape sequence to query the feature and a regex
7
+ * pattern to match the terminal's response.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { KittyFeature, Osc11Feature } from "tinky-termcap/utils/term-features";
12
+ *
13
+ * // Send query to terminal
14
+ * process.stdout.write(KittyFeature.query);
15
+ *
16
+ * // Parse response
17
+ * const response = "\\x1b[?1u";
18
+ * if (KittyFeature.responseRegex.test(response)) {
19
+ * console.log("Kitty keyboard protocol is supported!");
20
+ * }
21
+ * ```
22
+ *
23
+ * @packageDocumentation
24
+ */
25
+ /**
26
+ * Interface for terminal feature query definitions.
27
+ *
28
+ * A `TermFeature` encapsulates the information needed to detect a specific
29
+ * terminal capability:
30
+ * - The ANSI escape sequence query to send to the terminal
31
+ * - A regex pattern to match and parse the terminal's response
32
+ *
33
+ * @example Creating a custom feature detector
34
+ * ```typescript
35
+ * import type { TermFeature } from "tinky-termcap";
36
+ *
37
+ * // Define a custom feature to detect sixel graphics support
38
+ * const SixelFeature: TermFeature = {
39
+ * query: "\x1b[c", // Request device attributes
40
+ * responseRegex: /\x1b\[\?[\d;]*4[\d;]*c/, // Check for "4" in response
41
+ * };
42
+ * ```
43
+ *
44
+ * @see {@link KittyFeature} for Kitty keyboard protocol detection
45
+ * @see {@link Osc11Feature} for background color detection
46
+ * @see {@link TerminalNameFeature} for terminal name detection
47
+ * @see {@link DeviceAttributesFeature} for device attributes detection
48
+ * @see {@link ModifyOtherKeysFeature} for modifyOtherKeys support detection
49
+ */
50
+ export interface TermFeature {
51
+ /**
52
+ * ANSI escape sequence query string to send to the terminal.
53
+ *
54
+ * This sequence requests information from the terminal. The terminal
55
+ * will respond with data matching the `responseRegex` pattern if the
56
+ * feature is supported.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Send the query to stdout
61
+ * process.stdout.write(feature.query);
62
+ * ```
63
+ */
64
+ query: string;
65
+ /**
66
+ * Regular expression pattern to match the terminal's response.
67
+ *
68
+ * The regex may contain capture groups to extract specific values
69
+ * from the response (e.g., color components, version numbers).
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * // Parse response data
74
+ * const match = data.match(feature.responseRegex);
75
+ * if (match) {
76
+ * console.log("Feature detected, captured groups:", match.slice(1));
77
+ * }
78
+ * ```
79
+ */
80
+ responseRegex: RegExp;
81
+ }
82
+ /**
83
+ * Kitty Keyboard Protocol feature definition.
84
+ *
85
+ * The Kitty keyboard protocol provides enhanced keyboard input handling,
86
+ * including:
87
+ * - Distinguishing between key press and key release events
88
+ * - Reporting modifier keys as separate events
89
+ * - Providing Unicode code points for keys
90
+ *
91
+ * **Query sequence:** `ESC [ ? u`
92
+ *
93
+ * **Response format:** `ESC [ ? <flags> u`
94
+ * - `flags` is a decimal number indicating supported features
95
+ *
96
+ * @example Detecting Kitty protocol support
97
+ * ```typescript
98
+ * import { KittyFeature } from "tinky-termcap";
99
+ *
100
+ * // In a detection routine:
101
+ * stdout.write(KittyFeature.query);
102
+ *
103
+ * // When response arrives:
104
+ * const response = "\x1b[?1u";
105
+ * if (KittyFeature.responseRegex.test(response)) {
106
+ * console.log("Kitty keyboard protocol is supported");
107
+ * }
108
+ * ```
109
+ *
110
+ * @see https://sw.kovidgoyal.net/kitty/keyboard-protocol/
111
+ */
112
+ export declare const KittyFeature: TermFeature;
113
+ /**
114
+ * Background Color detection feature (OSC 11).
115
+ *
116
+ * Queries the terminal's current background color using Operating System
117
+ * Command (OSC) 11. This is useful for:
118
+ * - Adapting UI colors to light/dark themes
119
+ * - Ensuring sufficient contrast for text
120
+ * - Detecting the user's color scheme preference
121
+ *
122
+ * **Query sequence:** `ESC ] 11 ; ? ESC \`
123
+ *
124
+ * **Response format:** `ESC ] 11 ; rgb:<r>/<g>/<b> ST`
125
+ * - `r`, `g`, `b` are hex values (1-4 digits each)
126
+ * - `ST` (String Terminator) is either `ESC \` or `BEL` (0x07)
127
+ *
128
+ * @example Detecting background color
129
+ * ```typescript
130
+ * import { Osc11Feature } from "tinky-termcap";
131
+ *
132
+ * stdout.write(Osc11Feature.query);
133
+ *
134
+ * // Response might be: "\x1b]11;rgb:1a1a/1a1a/1a1a\x1b\\"
135
+ * const match = response.match(Osc11Feature.responseRegex);
136
+ * if (match) {
137
+ * const [, r, g, b] = match;
138
+ * console.log(`Background color: rgb(${r}, ${g}, ${b})`);
139
+ * }
140
+ * ```
141
+ *
142
+ * @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
143
+ */
144
+ export declare const Osc11Feature: TermFeature;
145
+ /**
146
+ * Terminal Name detection feature (XTVERSION).
147
+ *
148
+ * Queries the terminal emulator's name and version using the XTVERSION
149
+ * control sequence. This information can be used for:
150
+ * - Enabling terminal-specific features
151
+ * - Logging and diagnostics
152
+ * - Adapting behavior for known terminal quirks
153
+ *
154
+ * **Query sequence:** `ESC [ > q`
155
+ *
156
+ * **Response format:** `DCS > | <name> ST`
157
+ * - `DCS` is Device Control String (`ESC P`)
158
+ * - `name` is the terminal name/version string
159
+ * - `ST` is String Terminator (`ESC \` or `BEL`)
160
+ *
161
+ * @example Detecting terminal name
162
+ * ```typescript
163
+ * import { TerminalNameFeature } from "tinky-termcap";
164
+ *
165
+ * stdout.write(TerminalNameFeature.query);
166
+ *
167
+ * // Response might be: "\x1bP>|xterm(388)\x1b\\"
168
+ * const match = response.match(TerminalNameFeature.responseRegex);
169
+ * if (match) {
170
+ * console.log(`Terminal: ${match[1]}`); // "xterm(388)"
171
+ * }
172
+ * ```
173
+ *
174
+ * @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys
175
+ */
176
+ export declare const TerminalNameFeature: TermFeature;
177
+ /**
178
+ * Device Attributes detection feature (Primary DA).
179
+ *
180
+ * Queries the terminal's primary device attributes. This is commonly used
181
+ * as a "sentinel" query - terminals always respond to this, making it
182
+ * useful to determine when the terminal has finished responding to all
183
+ * capability queries.
184
+ *
185
+ * **Query sequence:** `ESC [ c`
186
+ *
187
+ * **Response format:** `ESC [ ? <params> c`
188
+ * - `params` is a semicolon-separated list of attribute codes
189
+ *
190
+ * @example Using as detection sentinel
191
+ * ```typescript
192
+ * import { DeviceAttributesFeature, KittyFeature, Osc11Feature } from "tinky-termcap";
193
+ *
194
+ * // Send multiple queries, with DA last as sentinel
195
+ * stdout.write(KittyFeature.query + Osc11Feature.query + DeviceAttributesFeature.query);
196
+ *
197
+ * // When DA response arrives, all other responses have been received
198
+ * if (DeviceAttributesFeature.responseRegex.test(buffer)) {
199
+ * console.log("Detection complete");
200
+ * }
201
+ * ```
202
+ *
203
+ * @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Device-Status-Report
204
+ */
205
+ export declare const DeviceAttributesFeature: TermFeature;
206
+ /**
207
+ * Modify Other Keys feature detection.
208
+ *
209
+ * Queries the current modifyOtherKeys mode level. When enabled at level 2
210
+ * or higher, the terminal sends enhanced key sequences that distinguish
211
+ * between different key combinations (e.g., `Ctrl+I` vs `Tab`).
212
+ *
213
+ * **Query sequence:** `ESC [ > 4 ; ? m`
214
+ *
215
+ * **Response format:** `ESC [ > 4 ; <level> m`
216
+ * - `level` indicates the current modifyOtherKeys mode (0, 1, or 2)
217
+ *
218
+ * Level values:
219
+ * - `0`: Disabled
220
+ * - `1`: Basic mode (some enhanced sequences)
221
+ * - `2`: Full mode (all enhanced sequences)
222
+ *
223
+ * @example Detecting modifyOtherKeys support
224
+ * ```typescript
225
+ * import { ModifyOtherKeysFeature } from "tinky-termcap";
226
+ *
227
+ * stdout.write(ModifyOtherKeysFeature.query);
228
+ *
229
+ * // Response might be: "\x1b[>4;2m"
230
+ * const match = response.match(ModifyOtherKeysFeature.responseRegex);
231
+ * if (match) {
232
+ * const level = parseInt(match[1], 10);
233
+ * console.log(`modifyOtherKeys level: ${level}`);
234
+ * if (level >= 2) {
235
+ * console.log("Full keyboard disambiguation available");
236
+ * }
237
+ * }
238
+ * ```
239
+ *
240
+ * @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-Desktop-Notification
241
+ */
242
+ export declare const ModifyOtherKeysFeature: TermFeature;
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+ /**
3
+ * @fileoverview Terminal feature query definitions for capability detection.
4
+ *
5
+ * This module defines the query sequences and response patterns for detecting
6
+ * various terminal capabilities. Each feature is represented as a `TermFeature`
7
+ * object containing the ANSI escape sequence to query the feature and a regex
8
+ * pattern to match the terminal's response.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { KittyFeature, Osc11Feature } from "tinky-termcap/utils/term-features";
13
+ *
14
+ * // Send query to terminal
15
+ * process.stdout.write(KittyFeature.query);
16
+ *
17
+ * // Parse response
18
+ * const response = "\\x1b[?1u";
19
+ * if (KittyFeature.responseRegex.test(response)) {
20
+ * console.log("Kitty keyboard protocol is supported!");
21
+ * }
22
+ * ```
23
+ *
24
+ * @packageDocumentation
25
+ */
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.ModifyOtherKeysFeature = exports.DeviceAttributesFeature = exports.TerminalNameFeature = exports.Osc11Feature = exports.KittyFeature = void 0;
28
+ /**
29
+ * Escape character constant used in ANSI escape sequences.
30
+ * @internal
31
+ */
32
+ var ESC = "\x1b";
33
+ /**
34
+ * Kitty Keyboard Protocol feature definition.
35
+ *
36
+ * The Kitty keyboard protocol provides enhanced keyboard input handling,
37
+ * including:
38
+ * - Distinguishing between key press and key release events
39
+ * - Reporting modifier keys as separate events
40
+ * - Providing Unicode code points for keys
41
+ *
42
+ * **Query sequence:** `ESC [ ? u`
43
+ *
44
+ * **Response format:** `ESC [ ? <flags> u`
45
+ * - `flags` is a decimal number indicating supported features
46
+ *
47
+ * @example Detecting Kitty protocol support
48
+ * ```typescript
49
+ * import { KittyFeature } from "tinky-termcap";
50
+ *
51
+ * // In a detection routine:
52
+ * stdout.write(KittyFeature.query);
53
+ *
54
+ * // When response arrives:
55
+ * const response = "\x1b[?1u";
56
+ * if (KittyFeature.responseRegex.test(response)) {
57
+ * console.log("Kitty keyboard protocol is supported");
58
+ * }
59
+ * ```
60
+ *
61
+ * @see https://sw.kovidgoyal.net/kitty/keyboard-protocol/
62
+ */
63
+ exports.KittyFeature = {
64
+ query: "".concat(ESC, "[?u"),
65
+ responseRegex: new RegExp("".concat(ESC, "\\[\\?(\\d+)u")),
66
+ };
67
+ /**
68
+ * Background Color detection feature (OSC 11).
69
+ *
70
+ * Queries the terminal's current background color using Operating System
71
+ * Command (OSC) 11. This is useful for:
72
+ * - Adapting UI colors to light/dark themes
73
+ * - Ensuring sufficient contrast for text
74
+ * - Detecting the user's color scheme preference
75
+ *
76
+ * **Query sequence:** `ESC ] 11 ; ? ESC \`
77
+ *
78
+ * **Response format:** `ESC ] 11 ; rgb:<r>/<g>/<b> ST`
79
+ * - `r`, `g`, `b` are hex values (1-4 digits each)
80
+ * - `ST` (String Terminator) is either `ESC \` or `BEL` (0x07)
81
+ *
82
+ * @example Detecting background color
83
+ * ```typescript
84
+ * import { Osc11Feature } from "tinky-termcap";
85
+ *
86
+ * stdout.write(Osc11Feature.query);
87
+ *
88
+ * // Response might be: "\x1b]11;rgb:1a1a/1a1a/1a1a\x1b\\"
89
+ * const match = response.match(Osc11Feature.responseRegex);
90
+ * if (match) {
91
+ * const [, r, g, b] = match;
92
+ * console.log(`Background color: rgb(${r}, ${g}, ${b})`);
93
+ * }
94
+ * ```
95
+ *
96
+ * @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
97
+ */
98
+ exports.Osc11Feature = {
99
+ query: "".concat(ESC, "]11;?").concat(ESC, "\\"),
100
+ responseRegex: new RegExp("".concat(ESC, "\\]11;rgb:([0-9a-fA-F]{1,4})\\/([0-9a-fA-F]{1,4})\\/([0-9a-fA-F]{1,4})(").concat(ESC, "\\\\|\\x07)?")),
101
+ };
102
+ /**
103
+ * Terminal Name detection feature (XTVERSION).
104
+ *
105
+ * Queries the terminal emulator's name and version using the XTVERSION
106
+ * control sequence. This information can be used for:
107
+ * - Enabling terminal-specific features
108
+ * - Logging and diagnostics
109
+ * - Adapting behavior for known terminal quirks
110
+ *
111
+ * **Query sequence:** `ESC [ > q`
112
+ *
113
+ * **Response format:** `DCS > | <name> ST`
114
+ * - `DCS` is Device Control String (`ESC P`)
115
+ * - `name` is the terminal name/version string
116
+ * - `ST` is String Terminator (`ESC \` or `BEL`)
117
+ *
118
+ * @example Detecting terminal name
119
+ * ```typescript
120
+ * import { TerminalNameFeature } from "tinky-termcap";
121
+ *
122
+ * stdout.write(TerminalNameFeature.query);
123
+ *
124
+ * // Response might be: "\x1bP>|xterm(388)\x1b\\"
125
+ * const match = response.match(TerminalNameFeature.responseRegex);
126
+ * if (match) {
127
+ * console.log(`Terminal: ${match[1]}`); // "xterm(388)"
128
+ * }
129
+ * ```
130
+ *
131
+ * @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys
132
+ */
133
+ exports.TerminalNameFeature = {
134
+ query: "".concat(ESC, "[>q"),
135
+ responseRegex: new RegExp("".concat(ESC, "P>\\|(.+?)(").concat(ESC, "\\\\|\\x07)")),
136
+ };
137
+ /**
138
+ * Device Attributes detection feature (Primary DA).
139
+ *
140
+ * Queries the terminal's primary device attributes. This is commonly used
141
+ * as a "sentinel" query - terminals always respond to this, making it
142
+ * useful to determine when the terminal has finished responding to all
143
+ * capability queries.
144
+ *
145
+ * **Query sequence:** `ESC [ c`
146
+ *
147
+ * **Response format:** `ESC [ ? <params> c`
148
+ * - `params` is a semicolon-separated list of attribute codes
149
+ *
150
+ * @example Using as detection sentinel
151
+ * ```typescript
152
+ * import { DeviceAttributesFeature, KittyFeature, Osc11Feature } from "tinky-termcap";
153
+ *
154
+ * // Send multiple queries, with DA last as sentinel
155
+ * stdout.write(KittyFeature.query + Osc11Feature.query + DeviceAttributesFeature.query);
156
+ *
157
+ * // When DA response arrives, all other responses have been received
158
+ * if (DeviceAttributesFeature.responseRegex.test(buffer)) {
159
+ * console.log("Detection complete");
160
+ * }
161
+ * ```
162
+ *
163
+ * @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Device-Status-Report
164
+ */
165
+ exports.DeviceAttributesFeature = {
166
+ query: "".concat(ESC, "[c"),
167
+ responseRegex: new RegExp("".concat(ESC, "\\[\\?(\\d+)(;\\d+)*c")),
168
+ };
169
+ /**
170
+ * Modify Other Keys feature detection.
171
+ *
172
+ * Queries the current modifyOtherKeys mode level. When enabled at level 2
173
+ * or higher, the terminal sends enhanced key sequences that distinguish
174
+ * between different key combinations (e.g., `Ctrl+I` vs `Tab`).
175
+ *
176
+ * **Query sequence:** `ESC [ > 4 ; ? m`
177
+ *
178
+ * **Response format:** `ESC [ > 4 ; <level> m`
179
+ * - `level` indicates the current modifyOtherKeys mode (0, 1, or 2)
180
+ *
181
+ * Level values:
182
+ * - `0`: Disabled
183
+ * - `1`: Basic mode (some enhanced sequences)
184
+ * - `2`: Full mode (all enhanced sequences)
185
+ *
186
+ * @example Detecting modifyOtherKeys support
187
+ * ```typescript
188
+ * import { ModifyOtherKeysFeature } from "tinky-termcap";
189
+ *
190
+ * stdout.write(ModifyOtherKeysFeature.query);
191
+ *
192
+ * // Response might be: "\x1b[>4;2m"
193
+ * const match = response.match(ModifyOtherKeysFeature.responseRegex);
194
+ * if (match) {
195
+ * const level = parseInt(match[1], 10);
196
+ * console.log(`modifyOtherKeys level: ${level}`);
197
+ * if (level >= 2) {
198
+ * console.log("Full keyboard disambiguation available");
199
+ * }
200
+ * }
201
+ * ```
202
+ *
203
+ * @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-Desktop-Notification
204
+ */
205
+ exports.ModifyOtherKeysFeature = {
206
+ query: "".concat(ESC, "[>4;?m"),
207
+ responseRegex: new RegExp("".concat(ESC, "\\[>4;(\\d+)m")),
208
+ };
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "tinky-termcap",
3
+ "version": "0.1.0",
4
+ "description": "Terminal capability detection for Tinky - React hooks for Kitty protocol, background color, terminal name detection",
5
+ "keywords": [
6
+ "terminal",
7
+ "capability",
8
+ "kitty",
9
+ "xterm",
10
+ "tinky",
11
+ "react",
12
+ "hooks"
13
+ ],
14
+ "homepage": "https://github.com/ByteLandTechnology/tinky-termcap#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/ByteLandTechnology/tinky-termcap/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/ByteLandTechnology/tinky-termcap.git"
21
+ },
22
+ "license": "Apache-2.0",
23
+ "author": {
24
+ "name": "ByteLand Technology Limited"
25
+ },
26
+ "type": "module",
27
+ "main": "./lib/index.js",
28
+ "typings": "./lib/index.d.ts",
29
+ "files": [
30
+ "./lib/*"
31
+ ],
32
+ "scripts": {
33
+ "test": "bun test",
34
+ "build": "tsc && npm run docs",
35
+ "lint": "eslint src tests",
36
+ "prepublish": "npm run build",
37
+ "prepare": "husky",
38
+ "docs": "typedoc --plugin typedoc-plugin-markdown --disableSources --out docs/api && prettier --write docs/api"
39
+ },
40
+ "peerDependencies": {
41
+ "tinky": "^1.1.2"
42
+ },
43
+ "devDependencies": {
44
+ "@commitlint/cli": "^20.3.0",
45
+ "@commitlint/config-conventional": "^20.3.0",
46
+ "@semantic-release/changelog": "^6.0.3",
47
+ "@semantic-release/git": "^10.0.1",
48
+ "@semantic-release/github": "^12.0.0",
49
+ "@semantic-release/npm": "^13.1.3",
50
+ "@types/bun": "^1.3.6",
51
+ "@types/react": "^19.2.8",
52
+ "@types/react-dom": "^19.2.3",
53
+ "eslint": "^9.39.2",
54
+ "eslint-plugin-react": "^7.37.5",
55
+ "husky": "^9.1.7",
56
+ "jiti": "^2.6.1",
57
+ "lint-staged": "^16.2.7",
58
+ "prettier": "^3.7.4",
59
+ "react": "^19.2.3",
60
+ "react-dom": "^19.2.3",
61
+ "typedoc": "^0.28.16",
62
+ "typedoc-plugin-markdown": "^4.9.0",
63
+ "typescript-eslint": "^8.53.0"
64
+ },
65
+ "commitlint": {
66
+ "extends": [
67
+ "@commitlint/config-conventional"
68
+ ]
69
+ },
70
+ "lint-staged": {
71
+ "*.{js,ts,jsx,tsx,json,md,yaml,yml}": "prettier --write"
72
+ }
73
+ }