From 0f40c718b8f0aa0a515e3e451c0caaf3a7b57d8b Mon Sep 17 00:00:00 2001 From: Max Brosnahan Date: Mon, 26 Jul 2021 14:08:02 +0200 Subject: [PATCH 1/2] feat: make url formatting helper available Why: So that making part of a line a clickable url is possible. --- README.md | 10 ++++++++++ src/index.ts | 10 +++++++--- test/format-url.test.ts | 12 ++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/format-url.test.ts diff --git a/README.md b/README.md index 4c52be37..709b80cd 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,16 @@ 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.open Open a url in the browser diff --git a/src/index.ts b/src/index.ts index 4bb8ab75..6c01aca4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -84,13 +84,17 @@ export const ux = { }, url(text: string, uri: string, params = {}) { + this.log(this.formatUrl(text, uri, params)) + }, + + formatUrl(text: string, uri: string, params = {}) { const supports = require('supports-hyperlinks') if (supports.stdout) { const hyperlinker = require('hyperlinker') - this.log(hyperlinker(text, uri, params)) - } else { - this.log(uri) + return hyperlinker(text, uri, params) } + + return uri }, annotation(text: string, annotation: string) { diff --git a/test/format-url.test.ts b/test/format-url.test.ts new file mode 100644 index 00000000..554855cd --- /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') + }) +}) From 3625c1115aa739f51c757f9990da3c0b75150c17 Mon Sep 17 00:00:00 2001 From: Max Brosnahan Date: Mon, 26 Jul 2021 14:43:39 +0200 Subject: [PATCH 2/2] feat: add helper to check for URL support --- README.md | 4 ++++ src/index.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 709b80cd..57588139 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,10 @@ await cli.formatUrl('sometext', 'https://google.com') // 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 6c01aca4..1e21b326 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,8 +88,7 @@ export const ux = { }, formatUrl(text: string, uri: string, params = {}) { - const supports = require('supports-hyperlinks') - if (supports.stdout) { + if (this.supportsUrls()) { const hyperlinker = require('hyperlinker') return hyperlinker(text, uri, params) } @@ -97,6 +96,11 @@ export const ux = { return uri }, + supportsUrls(): boolean { + const supports = require('supports-hyperlinks') + return supports.stdout + }, + annotation(text: string, annotation: string) { const supports = require('supports-hyperlinks') if (supports.stdout) {