-
-
Notifications
You must be signed in to change notification settings - Fork 144
Add a Link type for printing OSC 8 hyperlinks #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChrisJr404
wants to merge
1
commit into
console-rs:main
Choose a base branch
from
ChrisJr404:add-hyperlink
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -875,6 +875,87 @@ impl fmt::Display for Emoji<'_, '_> { | |
| } | ||
| } | ||
|
|
||
| /// A clickable terminal hyperlink. | ||
| /// | ||
| /// This pairs a piece of text with a URL and renders it using the | ||
| /// [OSC 8 escape sequence](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda), | ||
| /// which supporting terminals show as a clickable link. Terminals that do not | ||
| /// understand the sequence simply display the text. | ||
| /// | ||
| /// Hyperlinks follow the same enablement rules as colors: the escape sequence | ||
| /// is only emitted when colors are enabled for the target stream (see | ||
| /// [`colors_enabled`]). That way piped or redirected output stays free of | ||
| /// escape codes and just shows the text. Use [`Link::force`] to override this. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```rust | ||
| /// use console::Link; | ||
| /// println!("Read the {}.", Link::new("docs", "https://docs.rs/console")); | ||
| /// ``` | ||
| #[derive(Copy, Clone, Debug)] | ||
| pub struct Link<'a, D> { | ||
| text: D, | ||
| url: &'a str, | ||
| for_stderr: bool, | ||
| force: Option<bool>, | ||
| } | ||
|
|
||
| impl<'a, D> Link<'a, D> { | ||
| /// Creates a new hyperlink that points `text` at `url`. | ||
| pub fn new(text: D, url: &'a str) -> Link<'a, D> { | ||
| Link { | ||
| text, | ||
| url, | ||
| for_stderr: false, | ||
| force: None, | ||
| } | ||
| } | ||
|
|
||
| /// Renders this hyperlink for stderr instead of stdout. | ||
| /// | ||
| /// This only changes which stream's color configuration decides whether | ||
| /// the escape sequence is emitted. | ||
| pub fn for_stderr(mut self) -> Link<'a, D> { | ||
| self.for_stderr = true; | ||
| self | ||
| } | ||
|
|
||
| /// Renders this hyperlink for stdout. | ||
| /// | ||
| /// This is the default. | ||
| pub fn for_stdout(mut self) -> Link<'a, D> { | ||
| self.for_stderr = false; | ||
| self | ||
| } | ||
|
|
||
| /// Forces emitting the escape sequence on or off. | ||
| /// | ||
| /// This overrides the automatic detection that otherwise follows the color | ||
| /// configuration of the target stream. | ||
| pub fn force(mut self, value: bool) -> Link<'a, D> { | ||
| self.force = Some(value); | ||
| self | ||
| } | ||
|
|
||
| fn enabled(&self) -> bool { | ||
| self.force.unwrap_or_else(|| match self.for_stderr { | ||
| true => colors_enabled_stderr(), | ||
| false => colors_enabled(), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<D: fmt::Display> fmt::Display for Link<'_, D> { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| if self.enabled() { | ||
| write!(f, "\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", self.url, self.text) | ||
| } else { | ||
| self.text.fmt(f) | ||
| } | ||
|
Comment on lines
+951
to
+955
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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),
} |
||
| } | ||
| } | ||
|
|
||
| fn str_width(s: &str) -> usize { | ||
| #[cfg(feature = "unicode-width")] | ||
| { | ||
|
|
@@ -1227,3 +1308,30 @@ fn test_style_from_non_ascii_bg() { | |
| // silently ignores non-ascii | ||
| assert_eq!(parsed_style, Style::default()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_link_enabled() { | ||
| let link = Link::new("example", "https://example.com").force(true); | ||
| assert_eq!( | ||
| link.to_string(), | ||
| "\x1b]8;;https://example.com\x1b\\example\x1b]8;;\x1b\\" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_link_disabled() { | ||
| // When emission is turned off only the text is rendered. | ||
| let link = Link::new("example", "https://example.com").force(false); | ||
| assert_eq!(link.to_string(), "example"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_link_for_stderr() { | ||
| let link = Link::new(42, "https://example.com") | ||
| .for_stderr() | ||
| .force(true); | ||
| assert_eq!( | ||
| link.to_string(), | ||
| "\x1b]8;;https://example.com\x1b\\42\x1b]8;;\x1b\\" | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.