Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Make url formatting helper available #389

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions test/format-url.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})