-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli] Add
Output#link()
function to format terminal hyperlinks (#8370)
This adds a `link()` helper function to the `Output` class that is inspired by the `terminal-link` npm package. The main difference with this version is that it's more tightly integrated with the `Output` class for the purposes of being able to toggle hyperlinks support on/off for [unit tests](https://github.com/vercel/vercel/blob/4a54b19f46953af2a808bad3e461798e5a40e350/packages/cli/test/unit/util/output/create-output.test.ts) by setting the `output.supportsHyperlink` boolean. > **Note:** Since hyperlinks are still a relatively new feature, and users might not yet understand how to interact with them, we should only use this function for progressive enhancement scenarios at this time, and _not_ as part of a critical UX.
- Loading branch information
1 parent
2a492fa
commit 21f25f5
Showing
5 changed files
with
114 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import stripAnsi from 'strip-ansi'; | ||
import { client } from '../../../mocks/client'; | ||
|
||
describe('Output', () => { | ||
describe('link()', () => { | ||
it('should return hyperlink ANSI codes when `supportsHyperlink=true`', () => { | ||
client.output.supportsHyperlink = true; | ||
const val = client.output.link('Click Here', 'https://example.com'); | ||
expect(val).toEqual( | ||
'\x1B]8;;https://example.com\x07Click Here\x1B]8;;\x07' | ||
); | ||
expect(stripAnsi(val)).toEqual('Click Here'); | ||
}); | ||
|
||
it('should return default fallback when `supportsHyperlink=false`', () => { | ||
client.output.supportsHyperlink = false; | ||
const val = client.output.link('Click Here', 'https://example.com'); | ||
expect(val).toEqual('Click Here (https://example.com)'); | ||
}); | ||
|
||
it('should return text fallback when `supportsHyperlink=false` with `fallback: false`', () => { | ||
client.output.supportsHyperlink = false; | ||
const val = client.output.link('Click Here', 'https://example.com', { | ||
fallback: false, | ||
}); | ||
expect(val).toEqual('Click Here'); | ||
}); | ||
|
||
it('should return fallback when `supportsHyperlink=false` with `fallback` function', () => { | ||
client.output.supportsHyperlink = false; | ||
const val = client.output.link('Click Here', 'https://example.com', { | ||
fallback: () => 'other', | ||
}); | ||
expect(val).toEqual('other'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module 'supports-hyperlinks' { | ||
import { Writable } from 'stream'; | ||
export function supportsHyperlink(stream: Writable): boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters