-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosc.ts
More file actions
243 lines (216 loc) · 5.85 KB
/
osc.ts
File metadata and controls
243 lines (216 loc) · 5.85 KB
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
/**
* OSC (Operating System Command) escape sequences for terminal features.
*
* Provides utilities for creating clickable hyperlinks in terminal output using
* OSC 8 sequences, detecting terminal hyperlink support across various terminal
* emulators, and auto-linkifying URLs in text.
*
* The hyperlink detection logic is adapted from supports-hyperlinks by Sindre
* Sorhus, used under the MIT License.
*
* @license MIT (supports-hyperlinks portions)
* @packageDocumentation
* @see {@link https://github.com/chalk/supports-hyperlinks}
*/
import process from 'node:process';
/**
* OSC escape sequence start.
*/
const OSC = '\x1b]';
/**
* Bell character - terminates OSC sequence.
*/
const BEL = '\x07';
/**
* Separator for OSC parameters.
*/
const SEP = ';';
/**
* Check if running inside tmux.
*
* @function
*/
const isTmux = (): boolean => 'TMUX' in process.env;
/**
* Wrap an OSC sequence for tmux compatibility. Tmux requires OSC sequences to
* be wrapped with DCS tmux; <sequence> ST and all ESCs in <sequence> to be
* replaced with ESC ESC.
*
* @function
*/
const wrapOsc = (sequence: string): string => {
if (isTmux()) {
return '\x1BPtmux;' + sequence.replaceAll('\x1B', '\x1B\x1B') + '\x1B\\';
}
return sequence;
};
/**
* Parse a version string into major/minor/patch components. Handles both dotted
* versions (1.72.0) and compact versions (4601 -> 46.1.0).
*
* @function
*/
const parseVersion = (
versionString = '',
): { major: number; minor: number; patch: number } => {
// Handle compact version format (e.g., 4601 => 46.1.0)
if (/^\d{3,4}$/.test(versionString)) {
const match = /(\d{1,2})(\d{2})/.exec(versionString) ?? [];
return {
major: 0,
minor: Number.parseInt(match[1] ?? '0', 10),
patch: Number.parseInt(match[2] ?? '0', 10),
};
}
const versions = (versionString ?? '')
.split('.')
.map((n) => Number.parseInt(n, 10));
return {
major: versions[0] ?? 0,
minor: versions[1] ?? 0,
patch: versions[2] ?? 0,
};
};
/**
* Detect if the terminal supports hyperlinks (OSC 8). Based on the logic from
* supports-hyperlinks package but implemented inline without dependencies.
*
* @function
* @group Terminal
*/
export const supportsHyperlinks = (
stream: NodeJS.WriteStream = process.stdout,
): boolean => {
const {
CI,
CURSOR_TRACE_ID,
FORCE_HYPERLINK,
NETLIFY,
TEAMCITY_VERSION,
TERM,
TERM_PROGRAM,
TERM_PROGRAM_VERSION,
VTE_VERSION,
} = process.env;
// Explicit force flag
if (FORCE_HYPERLINK) {
return !(
FORCE_HYPERLINK.length > 0 && Number.parseInt(FORCE_HYPERLINK, 10) === 0
);
}
// Netlify always supports hyperlinks (no TTY needed)
if (NETLIFY) {
return true;
}
// No TTY, no hyperlinks
if (!stream.isTTY) {
return false;
}
// Windows Terminal supports hyperlinks
if ('WT_SESSION' in process.env) {
return true;
}
// Windows (non-Windows Terminal) doesn't support hyperlinks
if (process.platform === 'win32') {
return false;
}
// CI environments generally don't support hyperlinks
if (CI) {
return false;
}
// TeamCity doesn't support hyperlinks
if (TEAMCITY_VERSION) {
return false;
}
// Check terminal program
if (TERM_PROGRAM) {
const version = parseVersion(TERM_PROGRAM_VERSION);
switch (TERM_PROGRAM) {
case 'ghostty':
case 'zed': {
return true;
}
case 'iTerm.app': {
// iTerm 3.1+ supports hyperlinks
if (version.major === 3) {
return version.minor >= 1;
}
return version.major > 3;
}
case 'vscode': {
// Cursor (VS Code fork) supports hyperlinks
if (CURSOR_TRACE_ID) {
return true;
}
// VS Code 1.72+ supports hyperlinks
return (
version.major > 1 || (version.major === 1 && version.minor >= 72)
);
}
case 'WezTerm': {
// WezTerm packaged by Nix uses their own version scheme
if (/^0-unstable-\d{4}-\d{2}-\d{2}$/.test(TERM_PROGRAM_VERSION ?? '')) {
const date = (TERM_PROGRAM_VERSION ?? '').slice('0-unstable-'.length);
return date >= '2020-06-20';
}
// WezTerm version is a date (YYYYMMDD)
return version.major >= 20200620;
}
}
}
// VTE-based terminals (GNOME Terminal, etc.)
if (VTE_VERSION) {
// VTE 0.50.0 was supposed to support hyperlinks but segfaults.
// Check both string format ("0.50.0") and parsed version to catch
// compact format ("5000") which parseVersion converts to { 0, 50, 0 }.
if (VTE_VERSION === '0.50.0') {
return false;
}
const version = parseVersion(VTE_VERSION);
if (version.major === 0 && version.minor === 50 && version.patch === 0) {
return false;
}
return version.major > 0 || version.minor >= 50;
}
// Check TERM variable
switch (TERM) {
case 'alacritty':
case 'xterm-kitty': {
return true;
}
}
return false;
};
/**
* Create an OSC 8 hyperlink. The link text is displayed, and clicking it opens
* the URL in supported terminals.
*
* @function
* @group Terminal
*/
export const link = (text: string, url: string): string => {
const openLink = wrapOsc(`${OSC}8${SEP}${SEP}${url}${BEL}`);
const closeLink = wrapOsc(`${OSC}8${SEP}${SEP}${BEL}`);
return openLink + text + closeLink;
};
/**
* URL regex pattern for matching URLs in text. Matches http:// and https://
* URLs.
*/
const URL_PATTERN = /https?:\/\/[^\s<>"\])}]+/g;
/**
* Auto-linkify URLs in text. If terminal supports hyperlinks, URLs become
* clickable. Otherwise, text is returned unchanged.
*
* @function
* @group Terminal
*/
export const linkifyUrls = (
text: string,
stream: NodeJS.WriteStream = process.stdout,
): string => {
if (!supportsHyperlinks(stream)) {
return text;
}
return text.replace(URL_PATTERN, (url) => link(url, url));
};