Skip to content

Commit

Permalink
Handle all the x-r-whatever links
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Nov 1, 2024
1 parent 69180b8 commit 85578b1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
85 changes: 50 additions & 35 deletions R/ansi-hyperlink.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,17 @@ make_link_fun <- function(txt) {
if (!any(todo)) return(txt)

sprt <- ansi_hyperlink_types()$help
if (sprt) {
scheme <- if (identical(attr(sprt, "type"), "rstudio")) {
"ide:help"
} else {
"x-r-help"
}

txt[todo] <- style_hyperlink(
text = txt[todo],
url = paste0(scheme, ":", txt[todo])
)
if (!sprt) {
return(txt)
}

fmt <- get_hyperlink_format("help")
# the format has a placeholder for 'topic'
topic <- txt[todo]
done <- style_hyperlink(text = topic, url = glue(fmt))

txt[todo] <- done

txt
}

Expand All @@ -151,21 +149,16 @@ make_link_fun <- function(txt) {
make_link_help <- function(txt) {
mch <- re_match(txt, "^\\[(?<text>.*)\\]\\((?<url>.*)\\)$")
text <- ifelse(is.na(mch$text), txt, mch$text)
url <- ifelse(is.na(mch$url), txt, mch$url)
topic <- ifelse(is.na(mch$url), txt, mch$url)

sprt <- ansi_hyperlink_types()$help
if (sprt) {
scheme <- if (identical(attr(sprt, "type"), "rstudio")) {
"ide:help"
} else {
"x-r-help"
}
style_hyperlink(text = text, url = paste0(scheme, ":", url))

} else {
url2 <- vcapply(url, function(url1) format_inline("{.fun ?{url1}}"))
ifelse(text == url, url2, paste0(text, " (", url2, ")"))
if (!sprt) {
topic2 <- vcapply(topic, function(x) format_inline("{.fun ?{x}}"))
return(ifelse(text == topic, topic2, paste0(text, " (", topic2, ")")))
}

fmt <- get_hyperlink_format("help")
style_hyperlink(text = text, url = glue(fmt))
}

# -- {.href} --------------------------------------------------------------
Expand Down Expand Up @@ -225,21 +218,16 @@ make_link_run <- function(txt) {
make_link_topic <- function(txt) {
mch <- re_match(txt, "^\\[(?<text>.*)\\]\\((?<url>.*)\\)$")
text <- ifelse(is.na(mch$text), txt, mch$text)
url <- ifelse(is.na(mch$url), txt, mch$url)
topic <- ifelse(is.na(mch$url), txt, mch$url)

sprt <- ansi_hyperlink_types()$help
if (sprt) {
scheme <- if (identical(attr(sprt, "type"), "rstudio")) {
"ide:help"
} else {
"x-r-help"
}
style_hyperlink(text = text, url = paste0(scheme, ":", url))

} else {
url2 <- vcapply(url, function(url1) format_inline("{.code ?{url1}}"))
ifelse(text == url, url2, paste0(text, " (", url2, ")"))
if (!sprt) {
topic2 <- vcapply(topic, function(x) format_inline("{.code ?{x}}"))
return(ifelse(text == topic, topic2, paste0(text, " (", topic2, ")")))
}

fmt <- get_hyperlink_format("help")
style_hyperlink(text = text, url = glue(fmt))
}

# -- {.url} ---------------------------------------------------------------
Expand Down Expand Up @@ -438,6 +426,33 @@ ansi_hyperlink_types <- function() {
}
}

get_hyperlink_format <- function(type = c("run", "help", "vignette")) {
type <- match.arg(type)

key <- glue("hyperlink_{type}_url_format")
sprt <- ansi_hyperlink_types()[[type]]

custom_fmt <- get_config_chr(key)
if (is.null(custom_fmt)) {
if (identical(attr(sprt, "type"), "rstudio")) {
fmt_type <- "rstudio"
} else {
fmt_type <- "standard"
}
} else {
fmt_type <- "custom"
}

variable <- c(run = "code", help = "topic", vignette = "vignette")
fmt <- switch(
fmt_type,
custom = custom_fmt,
rstudio = glue("ide:{type}:{{{variable[type]}}}"),
standard = glue("x-r-{type}:{{{variable[type]}}}")
)
fmt
}

get_config_chr <- function(x, default = NULL) {
opt <- getOption(paste0("cli.", tolower(x)))
if (!is.null(opt)) {
Expand Down
21 changes: 21 additions & 0 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ test_that_cli <- function(desc, code,
cli.hyperlink_help = links,
cli.hyperlink_run = links,
cli.hyperlink_vignette = links,
cli.hyperlink_run_url_format = NULL,
cli.hyperlink_help_url_format = NULL,
cli.hyperlink_vignette_url_format = NULL,
)
withr::local_envvar(
R_CLI_HYPERLINKS = NA_character_,
R_CLI_HYPERLINK_RUN = NA_character_,
R_CLI_HYPERLINK_HELP = NA_character_,
R_CLI_HYPERLINK_VIGNETTE = NA_character_,
R_CLI_HYPERLINK_RUN_URL_FORMAT = NA_character_,
R_CLI_HYPERLINK_HELP_URL_FORMAT = NA_character_,
R_CLI_HYPERLINK_VIGNETTE_URL_FORMAT = NA_character_,
)
code_
}, c(conf, list(code_ = code)))
Expand All @@ -131,13 +143,22 @@ local_clean_cli_context <- function(.local_envir = parent.frame()) {
cli.hyperlink_run = NULL,
cli.hyperlink_help = NULL,
cli.hyperlink_vignette = NULL,
cli.hyperlink_run_url_format = NULL,
cli.hyperlink_help_url_format = NULL,
cli.hyperlink_vignette_url_format = NULL,
cli.num_colors = NULL,
cli.palette = NULL,
crayon.enabled = NULL
)
withr::local_envvar(
.local_envir = .local_envir,
R_CLI_HYPERLINKS = NA_character_,
R_CLI_HYPERLINK_RUN = NA_character_,
R_CLI_HYPERLINK_HELP = NA_character_,
R_CLI_HYPERLINK_VIGNETTE = NA_character_,
R_CLI_HYPERLINK_RUN_URL_FORMAT = NA_character_,
R_CLI_HYPERLINK_HELP_URL_FORMAT = NA_character_,
R_CLI_HYPERLINK_VIGNETTE_URL_FORMAT = NA_character_,
RSTUDIO_CLI_HYPERLINKS = NA_character_,
R_CLI_NUM_COLORS = NA_character_,
NO_COLOR = NA_character_,
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-ansi-hyperlink.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ test_that("iterm file links", {
})

test_that("rstudio links", {
local_clean_cli_context()
withr::local_envvar(
RSTUDIO = "1",
RSTUDIO_SESSION_PID = Sys.getpid(),
Expand All @@ -252,6 +253,7 @@ test_that("rstudio links", {
cli.hyperlink_run = TRUE,
cli.hyperlink_vignette = TRUE
)

expect_snapshot(
cli::cli_text("{.fun pkg::fun}")
)
Expand Down

0 comments on commit 85578b1

Please sign in to comment.