1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cliapp-docs.R
\name{links}
\alias{links}
\title{cli hyperlinks}
\description{
Certain cli styles create clickable links, if your IDE or terminal
supports them.
}
\section{Note: hyperlinks are currently experimental}{
The details of the styles that create hyperlinks will prrobably change
in the near future, based on user feedback.
}
\section{About the links in this manual page}{
The hyperlinks that are included in this manual are demonstrative
only, except for the \verb{https:} links. They look like a hyperlink, and
you can click on them, but they do nothing. I.e. a \code{.run} link will
not run the linked expression if you click on it.
}
\section{Hyperlink Support}{
As of today, the latest release of RStudio (version v2022.07.0+548)
supports all hyperlink types discussed here. Certain terminals, e.g.
iTerm on macOS, Linux terminals based on VTE (GNOME terminal) support
\code{.href}, \code{.email} and \code{.file} links.
You can use \code{\link[=ansi_has_hyperlink_support]{ansi_has_hyperlink_support()}} to check if your terminal or
IDE has hyperlink support in general, and \code{\link[=ansi_hyperlink_types]{ansi_hyperlink_types()}} to
check if various types of hyperlinks are supported.
If your hyperlink support is not detected properly in your IDE or
terminal, please open a cli issue at
\url{https://github.com/r-lib/cli/issues}.
}
\section{Link text}{
Before we delve into the various types of hyperlinks, a general comment
about link texts. Some link styles support a custom link text:
\itemize{
\item \code{.href}
\item \code{.help}
\item \code{.topic}
\item \code{.vignette}
\item \code{.run}
}
Others, i.e. \code{.email}, \code{.file}, \code{.fun} and \code{.url} do not support custom
link text.
The generic syntax for link text is the same as for Markdown hyperlinks:
\if{html}{\out{<div class="sourceCode">}}\preformatted{\{.style [link text](url)\}
}\if{html}{\out{</div>}}
\subsection{Vectorization}{
Note that it is not possible to add link text to a vector of URLs. E.g.
this will create a list of three URLs, all clickable:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{urls <- paste0("https://httpbin.org/status/", c(200, 403, 404))
cli::cli_text("Some httpbin URLs: \{.url \{urls\}\}.")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> Some httpbin URLs: <span style="font-style: italic;color: #268BD2;"><</span><span style="font-style: italic;color: #268BD2;"><a href="https://httpbin.org/status/200">https://httpbin.org/status/200</a></span><span style="font-style: italic;color: #268BD2;">></span>,
#> <span style="font-style: italic;color: #268BD2;"><</span><span style="font-style: italic;color: #268BD2;"><a href="https://httpbin.org/status/403">https://httpbin.org/status/403</a></span><span style="font-style: italic;color: #268BD2;">></span>, and
#> <span style="font-style: italic;color: #268BD2;"><</span><span style="font-style: italic;color: #268BD2;"><a href="https://httpbin.org/status/404">https://httpbin.org/status/404</a></span><span style="font-style: italic;color: #268BD2;">></span>.
</pre></div>
But it is not possible to use a different link text for them.
}}
}
\subsection{What if hyperlinks are not available?}{
If ANSI hyperlinks are not available, then the link text for of these
styles outputs both the link text and the URL in a (hopefully) helpful
way. See examples below.
}
}
\section{URLs}{
There are two cli styles to link to generic URLs. \code{.url} does not
allow custom link text, but \verb{\\href} does.
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli_text(
"See the cli homepage at \{.url https://cli.r-lib.org\} for details."
)
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> See the cli homepage at <span style="font-style: italic;color: #268BD2;"><</span><span style="font-style: italic;color: #268BD2;"><a href="https://cli.r-lib.org">https://cli.r-lib.org</a></span><span style="font-style: italic;color: #268BD2;">></span> for details.
</pre></div>
}}
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli_text(
"See the \{.href [cli homepage](https://cli.r-lib.org)\} for details."
)
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> See the <a href="https://cli.r-lib.org">cli homepage</a> for details.
</pre></div>
}}
\subsection{Without hyperlink support}{
This is how these links look without hyperlink support:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{local(\{
withr::local_options(cli.hyperlink = FALSE)
cli_text(
"See the cli homepage at \{.url https://cli.r-lib.org\} for details."
)
cli_text(
"See the \{.href [cli homepage](https://cli.r-lib.org)\} for details."
)
\})
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> See the cli homepage at <span style="font-style: italic;color: #268BD2;"><https://cli.r-lib.org></span> for details.
#> See the cli homepage (<span style="font-style: italic;color: #268BD2;"><https://cli.r-lib.org></span>) for details.
</pre></div>
}}
}
\subsection{URL encoding}{
Note that cli does not encode the url, so you might need to call
\code{utils::URLencode()} on it, especially, if it is substituted in
via \code{{}}.
\if{html}{\out{<div class="sourceCode r">}}\preformatted{weirdurl <- utils::URLencode("https://example.com/has some spaces")
cli_text("See more at \{.url \{weirdurl\}\}.")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> See more at <span style="font-style: italic;color: #268BD2;"><</span><span style="font-style: italic;color: #268BD2;"><a href="https://example.com/has%20some%20spaces">https://example.com/has%20some%20spaces</a></span><span style="font-style: italic;color: #268BD2;">></span>.
</pre></div>
}}
}
}
\section{Files}{
The \code{.file} style now automatically creates a \verb{file:} hyperlink.
Because \verb{file:} hyperlinks must contain an absolute path, cli tries to
convert relative paths, and paths starting with \code{~} to aboslute path.
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli_text("... edit your \{.file ~/.Rprofile\} file.\}")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... edit your <span style="color: #268BD2;"><a href="file:///Users/gaborcsardi/.Rprofile">~/.Rprofile</a></span> file.\}
</pre></div>
}}
\subsection{Link text}{
\code{.file} cannot use a custom link text. If you custom link text, then
you can use \code{.href} with a \verb{file:} URL.
\if{html}{\out{<div class="sourceCode r">}}\preformatted{prof <- path.expand("~/.Rprofile")
cli_text("... edit your \{.href [R profile](file://\{prof\})\}.")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... edit your <a href="file:///Users/gaborcsardi/.Rprofile">R profile</a>.
</pre></div>
}}
}
\subsection{Line and column numbers}{
You may add a line number to a file name, separated by \code{:}. Handlers
typically place the cursor at that line after opening the file.
You may also add a column number, after the line number, separated by
another \code{:}.
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli_text("... see line 5 in \{.file ~/.Rprofile:5\}.")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... see line 5 in <span style="color: #268BD2;"><a href="file:///Users/gaborcsardi/.Rprofile">~/.Rprofile:5</a></span>.
</pre></div>
}}
}
\subsection{Default handler}{
In RStudio \verb{file:} URLs open within RStudio. If you click on a file
link outside of RStudio, typically the operating system is consulted
for the application to open it.
}
\subsection{Without hyperlink support}{
One issue with using \code{.href} file files is that it does not look great
if hyperlinks are not available. This will be improved in the future:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{local(\{
withr::local_options(cli.hyperlink = FALSE)
prof <- path.expand("~/.Rprofile")
cli_text("... edit your \{.href [R profile](file://\{prof\})\}.")
\})
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... edit your R profile (<span style="font-style: italic;color: #268BD2;"><file:///Users/gaborcsardi/.Rprofile></span>).
</pre></div>
}}
}
}
\section{Links to the manual}{
\code{.fun} automatically creates links to the manual page of the function,
provided the function name is in the \code{packagename::functionname} form:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli::cli_text("... see \{.fun stats::lm\} to learn more.")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... see `<a href="x-r-help:stats::lm">stats::lm</a>()` to learn more.
</pre></div>
}}
\subsection{Link text}{
For a custom link text, use \code{.help} instead of \code{.fun}.
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli::cli_text("... see \{.help [\{.fun lm\}](stats::lm)\} to learn more.")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... see <a href="x-r-help:stats::lm">`lm()`</a> to learn more.
</pre></div>
}}
}
\subsection{Without hyperlink support}{
The same message without hyperlink support looks like this:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{local(\{
withr::local_options(cli.hyperlink = FALSE)
cli::cli_text("... see \{.help [\{.fun lm\}](stats::lm)\} to learn more.")
\})
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... see `lm()` (`?stats::lm()`) to learn more.
</pre></div>
}}
}
\subsection{Topics}{
To link to a help topic that is not a function, use \code{.topic}:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli::cli_text("... the tibble options at \{.topic tibble::tibble_options\}.")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... the tibble options at <a href="x-r-help:tibble::tibble_options">tibble::tibble_options</a>.
</pre></div>
}}
\code{.topic} support link text.
}
\subsection{Vignettes}{
To link to a vignette, use \code{.vignette}:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli::cli_text("... see the \{.vignette tibble::types\} vignette.")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> ... see the <a href="x-r-vignette:tibble::types">tibble::types</a> vignette.
</pre></div>
}}
}
}
\section{Click to run code}{
RStudio also supports a special link type that runs R code in the
current R session upon clicking.
You can create these links with \code{.run}:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli::cli_text("Run \{.run testthat::snapshot_review()\} to review")
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> Run <a href="x-r-run:testthat::snapshot_review()">testthat::snapshot_review()</a> to review
</pre></div>
}}
\subsection{Link text}{
Sometimes you want to show a slightly different expression in the link,
than the one that is evaluated. E.g. the evaluated expression probably
needs to qualify packages with \code{::}, but you might not want to show this:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{cli::cli_text(
"Run \{.run [snapshot_review()](testthat::snapshot_review())\} to review"
)
}\if{html}{\out{</div>}}\if{html}{\out{
<div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre>
#> Run <a href="x-r-run:testthat::snapshot_review()">snapshot_review()</a> to review
</pre></div>
}}
}
\subsection{Security considerations}{
To make \code{.run} hyperlinks more secure, RStudio will not run code
\itemize{
\item that is not in the \code{pkg::fun(args)} form,
\item if \code{args} contains \code{(}, \verb{)} or \verb{;},
\item if it calls a core package (base, stats, etc.),
\item if it calls a package that is not loaded, and it is not one of
testthat, devtools, usethis, rlang, pkgload, or pkgdown which are explicitly allowed.
}
When RStudio does not run a \code{.run} hyperlink, then it shows the code
and the user can copy and paste it to the console, if they consider
it safe to run.
Note that depending on your version of RStudio, the behavior can change.
}
}
|