Skip to content

Commit c0807ad

Browse files
authored
Display terminal links in cursor (#1998)
* add terminal link as cli module so we can more easily patch it * apply cursor patch * add license info * remove terminal-link package and add deprecation notice * remove old patch * remove terminal-link from sdk * changeset
1 parent cf9460a commit c0807ad

File tree

10 files changed

+410
-186
lines changed

10 files changed

+410
-186
lines changed

.changeset/itchy-games-sort.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
"trigger.dev": patch
4+
---
5+
6+
Display clickable links in Cursor terminal

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@
7878
"engine.io-parser@5.2.2": "patches/engine.io-parser@5.2.2.patch",
7979
"graphile-worker@0.16.6": "patches/graphile-worker@0.16.6.patch",
8080
"redlock@5.0.0-beta.2": "patches/redlock@5.0.0-beta.2.patch",
81-
"supports-hyperlinks@2.3.0": "patches/supports-hyperlinks@2.3.0.patch",
8281
"@kubernetes/client-node@1.0.0": "patches/@kubernetes__client-node@1.0.0.patch"
8382
}
8483
}
85-
}
84+
}

packages/cli-v3/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"@opentelemetry/semantic-conventions": "1.25.1",
9292
"@trigger.dev/build": "workspace:4.0.0-v4-beta.11",
9393
"@trigger.dev/core": "workspace:4.0.0-v4-beta.11",
94+
"ansi-escapes": "^7.0.0",
9495
"c12": "^1.11.1",
9596
"chalk": "^5.2.0",
9697
"chokidar": "^3.6.0",
@@ -103,6 +104,7 @@
103104
"evt": "^2.4.13",
104105
"fast-npm-meta": "^0.2.2",
105106
"gradient-string": "^2.0.2",
107+
"has-flag": "^5.0.1",
106108
"import-in-the-middle": "1.11.0",
107109
"import-meta-resolve": "^4.1.0",
108110
"jsonc-parser": "3.2.1",
@@ -123,7 +125,7 @@
123125
"socket.io-client": "4.7.5",
124126
"source-map-support": "0.5.21",
125127
"std-env": "^3.7.0",
126-
"terminal-link": "^3.0.0",
128+
"supports-color": "^10.0.0",
127129
"tiny-invariant": "^1.2.0",
128130
"tinyexec": "^0.3.1",
129131
"tinyglobby": "^0.2.10",

packages/cli-v3/src/utilities/cliOutput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { log } from "@clack/prompts";
22
import chalk from "chalk";
3-
import terminalLink, { Options as TerminalLinkOptions } from "terminal-link";
3+
import { terminalLink, TerminalLinkOptions } from "./terminalLink.js";
44
import { hasTTY } from "std-env";
55

66
export const isInteractive = hasTTY;
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
MIT License
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4+
Copyright (c) James Talmage <james@talmage.io> (https://github.com/jamestalmage)
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8+
*/
9+
10+
import { createSupportsColor } from "supports-color";
11+
import hasFlag from "has-flag";
12+
13+
function parseVersion(versionString = ""): { major: number; minor: number; patch: number } {
14+
if (/^\d{3,4}$/.test(versionString)) {
15+
// Env var doesn't always use dots. example: 4601 => 46.1.0
16+
const match = /(\d{1,2})(\d{2})/.exec(versionString) ?? [];
17+
return {
18+
major: 0,
19+
minor: Number.parseInt(match[1] ?? "0", 10),
20+
patch: Number.parseInt(match[2] ?? "0", 10),
21+
};
22+
}
23+
24+
const versions = (versionString ?? "").split(".").map((n) => Number.parseInt(n, 10));
25+
return {
26+
major: versions[0] ?? 0,
27+
minor: versions[1] ?? 0,
28+
patch: versions[2] ?? 0,
29+
};
30+
}
31+
32+
/**
33+
Creates a supports hyperlinks check for a given stream.
34+
35+
@param stream - Optional stream to check for hyperlink support.
36+
@returns boolean indicating whether hyperlinks are supported.
37+
*/
38+
export function createSupportsHyperlinks(stream: NodeJS.WriteStream): boolean {
39+
const {
40+
CI,
41+
CURSOR_TRACE_ID,
42+
FORCE_HYPERLINK,
43+
NETLIFY,
44+
TEAMCITY_VERSION,
45+
TERM_PROGRAM,
46+
TERM_PROGRAM_VERSION,
47+
VTE_VERSION,
48+
TERM,
49+
} = process.env;
50+
51+
if (FORCE_HYPERLINK) {
52+
return !(FORCE_HYPERLINK.length > 0 && Number.parseInt(FORCE_HYPERLINK, 10) === 0);
53+
}
54+
55+
if (
56+
hasFlag("no-hyperlink") ||
57+
hasFlag("no-hyperlinks") ||
58+
hasFlag("hyperlink=false") ||
59+
hasFlag("hyperlink=never")
60+
) {
61+
return false;
62+
}
63+
64+
if (hasFlag("hyperlink=true") || hasFlag("hyperlink=always")) {
65+
return true;
66+
}
67+
68+
// Netlify does not run a TTY, it does not need `supportsColor` check
69+
if (NETLIFY) {
70+
return true;
71+
}
72+
73+
// If they specify no colors, they probably don't want hyperlinks.
74+
if (!createSupportsColor(stream)) {
75+
return false;
76+
}
77+
78+
if (stream && !stream.isTTY) {
79+
return false;
80+
}
81+
82+
// Windows Terminal
83+
if ("WT_SESSION" in process.env) {
84+
return true;
85+
}
86+
87+
if (process.platform === "win32") {
88+
return false;
89+
}
90+
91+
if (CI) {
92+
return false;
93+
}
94+
95+
if (TEAMCITY_VERSION) {
96+
return false;
97+
}
98+
99+
if (CURSOR_TRACE_ID) {
100+
return true;
101+
}
102+
103+
if (TERM_PROGRAM) {
104+
const version = parseVersion(TERM_PROGRAM_VERSION);
105+
106+
switch (TERM_PROGRAM) {
107+
case "iTerm.app": {
108+
if (version.major === 3) {
109+
return version.minor >= 1;
110+
}
111+
112+
return version.major > 3;
113+
}
114+
115+
case "WezTerm": {
116+
return version.major >= 20_200_620;
117+
}
118+
119+
case "vscode": {
120+
// eslint-disable-next-line no-mixed-operators
121+
return version.major > 1 || (version.major === 1 && version.minor >= 72);
122+
}
123+
124+
case "ghostty": {
125+
return true;
126+
}
127+
// No default
128+
}
129+
}
130+
131+
if (VTE_VERSION) {
132+
// 0.50.0 was supposed to support hyperlinks, but throws a segfault
133+
if (VTE_VERSION === "0.50.0") {
134+
return false;
135+
}
136+
137+
const version = parseVersion(VTE_VERSION);
138+
return version.major > 0 || version.minor >= 50;
139+
}
140+
141+
switch (TERM) {
142+
case "alacritty": {
143+
// Support added in v0.11 (2022-10-13)
144+
return true;
145+
}
146+
// No default
147+
}
148+
149+
return false;
150+
}
151+
152+
/** Object containing hyperlink support status for stdout and stderr. */
153+
const supportsHyperlinks = {
154+
/** Whether stdout supports hyperlinks. */
155+
stdout: createSupportsHyperlinks(process.stdout),
156+
/** Whether stderr supports hyperlinks. */
157+
stderr: createSupportsHyperlinks(process.stderr),
158+
};
159+
160+
export default supportsHyperlinks;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
MIT License
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7+
*/
8+
9+
import ansiEscapes from "ansi-escapes";
10+
import supportsHyperlinks from "./supportsHyperlinks.js";
11+
12+
export type TerminalLinkOptions = {
13+
/**
14+
Override the default fallback. If false, the fallback will be disabled.
15+
@default `${text} (${url})`
16+
*/
17+
readonly fallback?: ((text: string, url: string) => string) | boolean;
18+
};
19+
20+
/**
21+
Create a clickable link in the terminal's stdout.
22+
23+
[Supported terminals.](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda)
24+
For unsupported terminals, the link will be printed in parens after the text: `My website (https://sindresorhus.com)`,
25+
unless the fallback is disabled by setting the `fallback` option to `false`.
26+
27+
@param text - Text to linkify.
28+
@param url - URL to link to.
29+
30+
@example
31+
```
32+
import terminalLink from 'terminal-link';
33+
34+
const link = terminalLink('My Website', 'https://sindresorhus.com');
35+
console.log(link);
36+
```
37+
38+
@deprecated The default fallback is broken in some terminals. Please use `cliLink` instead.
39+
*/
40+
function terminalLink(
41+
text: string,
42+
url: string,
43+
{ target = "stdout", ...options }: { target?: "stdout" | "stderr" } & TerminalLinkOptions = {}
44+
) {
45+
if (!supportsHyperlinks[target]) {
46+
// If the fallback has been explicitly disabled, don't modify the text itself.
47+
if (options.fallback === false) {
48+
return text;
49+
}
50+
51+
return typeof options.fallback === "function"
52+
? options.fallback(text, url)
53+
: `${text} (\u200B${url}\u200B)`;
54+
}
55+
56+
return ansiEscapes.link(text, url);
57+
}
58+
/**
59+
Check whether the terminal supports links.
60+
61+
Prefer just using the default fallback or the `fallback` option whenever possible.
62+
*/
63+
terminalLink.isSupported = supportsHyperlinks.stdout;
64+
terminalLink.stderr = terminalLinkStderr;
65+
66+
/**
67+
Create a clickable link in the terminal's stderr.
68+
69+
[Supported terminals.](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda)
70+
For unsupported terminals, the link will be printed in parens after the text: `My website (https://sindresorhus.com)`.
71+
72+
@param text - Text to linkify.
73+
@param url - URL to link to.
74+
75+
@example
76+
```
77+
import terminalLink from 'terminal-link';
78+
79+
const link = terminalLink.stderr('My Website', 'https://sindresorhus.com');
80+
console.error(link);
81+
```
82+
*/
83+
function terminalLinkStderr(text: string, url: string, options: TerminalLinkOptions = {}) {
84+
return terminalLink(text, url, { target: "stderr", ...options });
85+
}
86+
87+
/**
88+
Check whether the terminal's stderr supports links.
89+
90+
Prefer just using the default fallback or the `fallback` option whenever possible.
91+
*/
92+
terminalLinkStderr.isSupported = supportsHyperlinks.stderr;
93+
94+
export { terminalLink };

packages/cli-v3/types.d.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/trigger-sdk/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"debug": "^4.3.4",
5959
"evt": "^2.4.13",
6060
"slug": "^6.0.0",
61-
"terminal-link": "^3.0.0",
6261
"ulid": "^2.3.0",
6362
"uncrypto": "^0.1.3",
6463
"uuid": "^9.0.0",

patches/supports-hyperlinks@2.3.0.patch

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)