Skip to content
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

Turn HTML output into links #12888

Closed
epage opened this issue Oct 27, 2023 · 3 comments · Fixed by #12889
Closed

Turn HTML output into links #12888

epage opened this issue Oct 27, 2023 · 3 comments · Fixed by #12889
Labels
A-timings Area: timings C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-doc S-triage Status: This issue is waiting on initial triage.

Comments

@epage
Copy link
Contributor

epage commented Oct 27, 2023

Problem

In most terminals, if a URL is printed it is turned into a link you can click on for easy opening. This is less true for file paths.

This is less of a problem for cargo doc --open as cargo will open the page up for you.

Places where we are falling short include

  • cargo doc --target foo --target bar (--open doesn't work)
  • Wanting to a cargo doc invocation that you can run multiple times, using shell history, without editing in/out --open
  • cargo doc calls wrapped in other tools, like make
  • cargo build --timings (it'd be awkward to add a --open just for --timings)

Proposed Solution

Turn the file paths into links using ANSI escape codes

Notes

From #12859 (review)

This looks great. Have you consider integrating something like hyperlinks in terminal emulators?

ripgrep just got a similar feature, though not by using OSC escape sequence:

@epage epage added Command-doc C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` A-timings Area: timings S-triage Status: This issue is waiting on initial triage. labels Oct 27, 2023
@epage
Copy link
Contributor Author

epage commented Oct 27, 2023

Things we'd need to work through:

In theory, terminals that support ANSI escape codes should either support or gracefully degrade in the presence of hyperlinks and so we could do this based on term.color. However, not all do.
We could leverage supports-hyperlinks. We could maybe even add a term.hyperlinks that is "always", "auto", and "never" that gets ANDed with term.color.

anstream strips ANSI escape codes in an all-or-nothing way, so if we want control besides term.color, we'd need to track that state ourselves. At the most basic level, this could be Shell::supports_hyperlinks. A more advanced version would be

impl Shell {
    fn hyperlink(&self, link: &str) -> Hyperlink {...}
}

struct Hyperlink { ... }

impl Hyperlink {
    fn open(&self) -> impl Display;
    fn close(&self) -> impl Display;
}

format!("Some text with a {}{}{}", link.open(), path.display(), link.close());

Another tricky aspect, from the ripgrep discussion, is that you shouldn't leave the hostname as default for hyperlinks due to SSH ... except on Windows due to bugs.

@epage
Copy link
Contributor Author

epage commented Oct 27, 2023

Also, iiuc ripgrep went above and beyond and supports hyperlink patterns for opening files in editors to a specific file/line which requires knowing what editor is being used. This is done with a specific flag.

That means just for our use cases, we can skip that. If we want to extend this to cargo and rustc diagnostic messages, then we'd need to reconsider this.

epage added a commit to epage/cargo that referenced this issue Oct 28, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Oct 28, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
@epage
Copy link
Contributor Author

epage commented Oct 28, 2023

@Muscraft this could have interesting ramifications for diagnostics. At the most basic level, we could make links out of references to the manifest (like package.version linking to the documentation for that field). If we apply ripgrep's concept of "editor links" than we could make the file paths + line + column information openable for editing.

EDIT: Also, if we rewrote rustc's messages, then we could link to docs.rs for crates.io dependencies in error messages related to their API

epage added a commit to epage/cargo that referenced this issue Oct 30, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Oct 30, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Oct 30, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Oct 30, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Oct 30, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Oct 30, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Oct 31, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Oct 31, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Nov 1, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Nov 3, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Nov 6, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
epage added a commit to epage/cargo that referenced this issue Nov 7, 2023
This provides an alternative to `--open`, where supported.

Fixes rust-lang#12888
bors added a commit that referenced this issue Nov 8, 2023
feat: Make browser links out of HTML file paths

This provides an alternative to `--open`, where supported.

Note: because we are relying on `supports-hyperlinks`, we are getting `FORCE_HYPERLINK` for "free".  Unsure whether it and the current policy (it gets overridden by `term.hyperlinks`) is something we want.  `FORCE_HYPERLINK` mirrors the npm package's behavior of the same name (see zkat/supports-hyperlinks#2) though though I also found reading of it in ohmyzsh, a go terminal library and many more places.  Similarly, #12751 added indirect, undocumented support for community environment variables.

Fixes #12888
@bors bors closed this as completed in 2130a0f Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-timings Area: timings C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-doc S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant