diff --git a/README.md b/README.md index 4c52be3..5758813 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,20 @@ await cli.url('sometext', 'https://google.com') ![url demo](assets/url.gif) +# cli.formatUrl(text, uri) + +Format a hyperlink (if supported in the terminal). Useful when you want to make part of a line clickable. + +```typescript +await cli.formatUrl('sometext', 'https://google.com') +// returns sometext with hyperlink escape characters in supported terminals +// returns https://google.com in unsupported terminals +``` + +# cli.supportsUrls() + +Check to see if the terminal supports URLs. + # cli.open Open a url in the browser diff --git a/src/index.ts b/src/index.ts index 4bb8ab7..1e21b32 100644 --- a/src/index.ts +++ b/src/index.ts @@ -84,13 +84,21 @@ export const ux = { }, url(text: string, uri: string, params = {}) { - const supports = require('supports-hyperlinks') - if (supports.stdout) { + this.log(this.formatUrl(text, uri, params)) + }, + + formatUrl(text: string, uri: string, params = {}) { + if (this.supportsUrls()) { const hyperlinker = require('hyperlinker') - this.log(hyperlinker(text, uri, params)) - } else { - this.log(uri) + return hyperlinker(text, uri, params) } + + return uri + }, + + supportsUrls(): boolean { + const supports = require('supports-hyperlinks') + return supports.stdout }, annotation(text: string, annotation: string) { diff --git a/test/format-url.test.ts b/test/format-url.test.ts new file mode 100644 index 0000000..554855c --- /dev/null +++ b/test/format-url.test.ts @@ -0,0 +1,12 @@ +import ux from '../src' + +import {expect, fancy} from './fancy' + +process.env.FORCE_HYPERLINK = '1' + +describe('prompt', () => { + fancy + .it('formats hyperlinks for rendering', async () => { + expect(ux.formatUrl('sometext', 'https://google.com')).to.contain('ttps://google.com\u0007sometext') + }) +})