Chalk: Support for hyperlinks

Created on 27 Dec 2018  Â·  8Comments  Â·  Source: chalk/chalk

EDIT: See sindresorhus/terminal-link


Trivial implementation:

function hyperlink(text, url, attrs) {
  return `\u001b]8;${attrs || ''};${url || text}\u0007${text}\u001b]8;;\u0007`;
}

Usage:

console.log(
  `See issue ${
    hyperlink('#319 on GitHub', 'https://github.com/chalk/chalk/issues/319')
  }`
);

For more info see Hyperlinks (a.k.a. HTML-like anchors) in terminal emulators

Most helpful comment

@langpavel I would be happy to add support for stderr in terminal-link ;)

https://github.com/sindresorhus/terminal-link/issues/4

All 8 comments

Detection for this is a nightmare, even moreso than truecolor...

I just want open discussion about this awesome feature.
Especially about fallback API when colors are not supported.
I have suggestion about third argument which may be:

  • true — write text followed by URL in parenthesis: text(url)
  • false — ignore, write text only
  • string — write this string instead of both; this is ambiguous with attributes
  • default, undefined — like false if text === url else like true
  • object with key fallback — like above, other keys are serialized into attributes (only id is now defined, but there is no reason for limitation)

@Qix- I don't really know if detection for this feature is even possible.
It may be enabled on level 3 by default.

And there should be new key in output of supports-color with overrides:

  • ENV variable
  • command line flag
  • module override
  • instance override + possibility for custom fallback formatter (new configuration beside level on Chalk constructor)

Oh, now I found sindresorhus/terminal-link

I think it's way too early to add this to Chalk, if at all. I like to see it more widely supported first. For now, we can just recommend terminal-link. Honestly not even sure if it makes sense to have this in Chalk. Chalk is for visual styling, while hyperlinks are functional. This is a slippery slope. If we accept this, we should expect to get requests to support other ansi escape codes, like hiding the cursor, etc.

terminal-link is great, however I wish use it with stderr :-D And many tools may want too because of possibility to link local files (I like vscode:/file/${path}:${line}:${column}) and issues on the web...
All diagnostics should go to stderr

But definitely I will be very happy if you mention terminal-link in readme…

Agreed, I think it's out of scope for chalk.

@langpavel I would be happy to add support for stderr in terminal-link ;)

https://github.com/sindresorhus/terminal-link/issues/4

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DawnImpulse picture DawnImpulse  Â·  7Comments

juanmnl picture juanmnl  Â·  4Comments

HillTravis picture HillTravis  Â·  8Comments

LeoAref picture LeoAref  Â·  8Comments

StfBauer picture StfBauer  Â·  4Comments