Skip to content

Support ansi hyperlinks - #10416

Merged
kevinushey merged 74 commits into
rstudio:mainfrom
romainfrancois:ansi_hyperlink_v2
Feb 8, 2022
Merged

kevinushey merged 74 commits into
rstudio:mainfrom
romainfrancois:ansi_hyperlink_v2

Conversation

@romainfrancois

@romainfrancois romainfrancois commented Jan 24, 2022 •

Copy link
Copy Markdown
Contributor

Intent

Adresses #1941 (replaces #10299)

Approach

The hyperlink escape code described in https://iterm2.com/documentation-escape-codes.html are recognized, and the text between an opening anchor code (with a url) and a closing one (w/o) is displayed as a clickable link.

The action when such a link is clicked depends on its 3 components url, text, params :

  • if the url starts with rstudio:viewer: then the remaining is used as an url to open in the viewer: rstudio:viewer:http://example.com
  • if the url starts with file:// the file is navigated to, params may contain line=3:col=4 to navigate to line 3 and column 4 of the file.
  • if the url is rstudio:help then params$topic and (optionally) params$package are used to open the relevant help page in the help pane.
  • if the url is rstudio:vignette, same thing but for vignettes.
  • if the url is rstudio:run then the text is sent to the console, params may contain parameters supported by .rs.api.sendToConsole
  • if the url starts with rstudio:run: then the remaining is sent to the console, e.g. rstudio:run:head(mtcars)

For ease, the url may be generated by cli::style_hyperlink() (>= 3.1.1)

Automated Tests

This has unit tests to assert that the right thing is written in the Dom:

   public void testHyperlinkNoParams()
   {
      PreElement ele = Document.get().createPreElement();
      VirtualConsole vc = getVC(ele);
      String testInput = "\u001b]8;;http://www.example.com\7text\u001b]8;;\7";
      vc.submit(testInput);
      String expected = "<a class=\"xtermUnderline xtermColor5 xtermHyperlink\" title=\"http://www.example.com\">text</a>";
      Assert.assertEquals(expected, ele.getInnerHTML());
   }

   public void testHyperlinkWithParams()
   {
      PreElement ele = Document.get().createPreElement();
      VirtualConsole vc = getVC(ele);
      String testInput = "\u001b]8;name1=value1:name2=value2;http://www.example.com\7text\u001b]8;;\7";
      vc.submit(testInput);
      String expected = "<a class=\"xtermUnderline xtermColor5 xtermHyperlink\" title=\"http://www.example.com\">text</a>";
      Assert.assertEquals(expected, ele.getInnerHTML());
   }

QA Notes

library(cli)
library(glue)

tf <- tempfile()
writeLines(c("bla bla", "yada yada yada"), tf)

links <- c(
  style_hyperlink('dplyr::summarise()', 'https://dplyr.tidyverse.org/reference/summarise.html' ),
  style_hyperlink('dplyr::summarise()', 'rstudio:viewer:https://dplyr.tidyverse.org/reference/summarise.html'),
  
  style_hyperlink('open file'                             , glue('file://{tf}')),
  style_hyperlink('open file at line 2'                   , glue('file://{tf}'), c(line = 2)),
  style_hyperlink('open file at line 2, column 6'         , glue('file://{tf}'), c(line = 2, col = 5)),
  
  style_hyperlink('?stats::rnorm' , "rstudio:help", c(topic = "rnorm", package = "stats")),
  style_hyperlink('?mean', "rstudio:help", c(topic = "mean")),
  
  style_hyperlink('vignette', "rstudio:vignette", c(topic = "type-size", package = "vctrs")),
  
  style_hyperlink("head(mtcars)", "rstudio:run", c(execute = FALSE)), 
  style_hyperlink("mtcars", "rstudio:run:head(mtcars)", c(animate = TRUE))
)

writeLines(links)

This should display links:

image

clicking on these links will:

  • open the webpage in a browser

  • open the webpage in the viewer pane

  • navigate to the file

  • navigate to the 2nd line of the file

  • navigate to the 2nd line on column 6 of the file

  • open the help page for stats::rnorm()

  • open the help for mean()

  • open the type size vignette

  • put the code head(mtcars) in the console w/o executing it

  • put the code head(mtcars) in the console one character at a time, and then execute it.

Checklist

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

romainfrancois and others added 30 commits January 24, 2022 09:10

@gtritchie gtritchie left a comment

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.

Minor comment on the NEWS entry, but otherwise looks great!

Comment thread NEWS.md Resolved
Comment on lines +656 to +657
".xtermHyperlink { cursor: pointer; }",
".xtermHyperlink:hover { text-decoration: underline; }",

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.

Minor edge case; if a user had previously imported a theme into RStudio (.tmtheme), then upgraded to RStudio with this new feature, the saved .rstheme won't have these two lines. I think they'd have to delete the theme and reimport it to correct that. Don't think we need to do anything about that, just mentioning it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it's a minor issue, because the link already get added xtermUnderline and xtermColor5 so they do stand out.

The :hover bit could be removed I suppose, it's only useful if the xtermUnderline style was cancelled.

I had to add the cursor: pointer because it would only be inherited from <a> when there is an href= attribute, but then the linked page opened in the same tab in addition to triggering the rpc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could add anchor.setAttribute("style", "cursor: pointer;"); to be on the safe side.

@MariaSemple
MariaSemple changed the base branch from spotted-wakerobin to main January 30, 2022 20:51
@MariaSemple

Copy link
Copy Markdown
Collaborator

Rebasing to main as that is now the SW branch.

@kevinushey

Copy link
Copy Markdown
Collaborator

@romainfrancois, is this ready for merge now?

@romainfrancois

Copy link
Copy Markdown
Contributor Author

Yes, I think it can be merged.

@kevinushey

Copy link
Copy Markdown
Collaborator

It looks like unfortunately there's a merge conflict now -- @romainfrancois are you able to take a look?

@kevinushey
kevinushey merged commit a912e07 into rstudio:main Feb 8, 2022
@romainfrancois
romainfrancois deleted the ansi_hyperlink_v2 branch February 8, 2022 18:17
matheustavaresdev added a commit that referenced this pull request Feb 9, 2022
…on-test

* 'main' of github.com:rstudio/rstudio:
  Fix duplicate menu separators (#10533)
  use RSTUDIO_CLI_HYPERLIKNS env var
  Mention issue number in NEWS.
  stamp version into Electron app (macOS)
  fix NEWS
  fix NEWS year
  produce separate UTF-8 rsession binary for ucrt builds (#10524)
  Check if load from base is masked before calling load workspace
  alternate mechanism for R_ResetConsole on Windows (#10510)
  rotate news files (#10528)
  Move old NEWS versions in version/news subdirectory
  Change name back to NEWS.md
  Add RSW section
  Add version numbering to NEWS file to avoid further conflicts/overwrites
  Remove GO NEWS files
  NEWS for Spotted Wakerobin only
  Support ansi hyperlinks (#10416)
  Update to Quarto v0.3.114
@jgutman

jgutman commented Mar 14, 2022

Copy link
Copy Markdown
Contributor

Using RStudio Desktop 2022.06.0-daily+208 and the latest CLI from Github 3.2.0.9000 2022-03-14 Github (r-lib/cli@476cf06), these all worked for me except for
style_hyperlink('dplyr::summarise()', 'rstudio:viewer:https://dplyr.tidyverse.org/reference/summarise.html')

which did not load in the viewer -- only worked if I clicked the icon to Show in new window and opened it in my browser.
image

@romainfrancois

Copy link
Copy Markdown
Contributor Author

I can confirm this does not work on a desktop build (2022.06.0-daily+208), I believe this is because the viewer can't handle the link, e.g.

.rs.api.viewer("https://dplyr.tidyverse.org/reference/summarise.html")

Because the viewer is not meant to open these links anyway:

image

It is my misunderstanding that it would work. Problem is it does work when using server, e.g. rendering on chrome. I'll open a follow up issue.

@jgutman

jgutman commented Mar 18, 2022

Copy link
Copy Markdown
Contributor

Ah okay thanks! In that case I think we can consider this issue verified, will update the issue

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.

5 participants