Skip to content

Add a Link type for printing OSC 8 hyperlinks - #295

Open
ChrisJr404 wants to merge 1 commit into
console-rs:mainfrom
ChrisJr404:add-hyperlink
Open

Add a Link type for printing OSC 8 hyperlinks#295
ChrisJr404 wants to merge 1 commit into
console-rs:mainfrom
ChrisJr404:add-hyperlink

Conversation

@ChrisJr404

Copy link
Copy Markdown

Closes #132.

console already recognizes OSC 8 hyperlinks when stripping/measuring text, but there was no way to emit one. This adds a small Link wrapper, in the same spirit as Emoji, that prints text as an OSC 8 hyperlink on terminals that support it.

Like styling, the escape is only written when colors are enabled for the target stream, so piped or redirected output just shows the plain text. You can force it on or off with .force(..) and target stderr with .for_stderr().

use console::Link;
println!("Read the {}.", Link::new("docs", "https://docs.rs/console"));

I went with Link::new(text, url) so it reads a bit like a Markdown link, but I'm happy to flip the order if you'd rather have the url first. epage linked anstyle-hyperlink on the issue as a reference; I kept this minimal for now and left out the optional id= parameter.

Comment thread src/utils.rs
}

fn enabled(&self) -> bool {
self.force.unwrap_or_else(|| match self.for_stderr {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this isn't really about color, I don't think we should disable the links for this.

Comment thread src/utils.rs
Comment on lines +951 to +955
if self.enabled() {
write!(f, "\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", self.url, self.text)
} else {
self.text.fmt(f)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: suggest writing this as

match self.enabled() {
    true => write!(..),
    false => self.text.fmt(f),
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Print hyperlinks

2 participants