Skip to content

Commit

Permalink
{.href}: use vector names as link text
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Aug 27, 2022
1 parent 063b28f commit 4b5b889
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
17 changes: 11 additions & 6 deletions R/ansi-hyperlink.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,24 @@ abs_path <- function(x) {
vcapply(x, abs_path1, USE.NAMES = FALSE)
}

make_link_href1 <- function(txt) {
url <- parse_spaced_tag1(txt)
make_link_href1 <- function(txt, name = NULL) {
if (is.null(name)) {
url <- parse_spaced_tag1(txt)
} else {
url <- list(link_text = name, url = txt)
}
if (ansi_has_hyperlink_support()) {
style_hyperlink(url$link_text, url$url)
} else if (url$url == txt) {
txt
} else {
} else if (url$link_text != url$url) {
paste0(url$link_text, " (", url$url, ")")
} else {
txt
}
}

make_link_href <- function(txt) {
vcapply(txt, make_link_href1)
nms <- names(txt)
vcapply(seq_along(txt), function(i) make_link_href1(txt[i], nms[i]))
}

# if txt already contains a hyperlink, then we do not add another link
Expand Down
2 changes: 2 additions & 0 deletions R/cliapp-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#' * `fun` for a function name.
#' * `href` creates a hyperlink, potentially with a link text. The first
#' word is the URL (until the first space), and the rest is the link text.
#' If the `.href` tag consists of a single substitution, which evaluates
#' to a named vector, then the names are used as link texts.
#' If the terminal supports ANSI hyperlinks (e.g. RStudio, iTerm2, etc.),
#' then cli creates a clickable link.
#' * `key` for a keyboard key.
Expand Down
4 changes: 3 additions & 1 deletion man/inline-markup.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testthat/_snaps/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,15 @@
Message
https://cli.r-lib.org/1, https://cli.r-lib.org/2, and https://cli.r-lib.org/3

---

Code
url <- structure(paste0("https://cli.r-lib.org/", 1:3), names = letters[1:3])
cli_text("{.href {url}}")
Message
a (https://cli.r-lib.org/1), b (https://cli.r-lib.org/2), and c
(https://cli.r-lib.org/3)

# {.href} vectors [plain-all]

Code
Expand All @@ -460,3 +469,11 @@
Message
]8;;https://cli.r-lib.org/1https://cli.r-lib.org/1]8;;, ]8;;https://cli.r-lib.org/2https://cli.r-lib.org/2]8;;, and ]8;;https://cli.r-lib.org/3https://cli.r-lib.org/3]8;;

---

Code
url <- structure(paste0("https://cli.r-lib.org/", 1:3), names = letters[1:3])
cli_text("{.href {url}}")
Message
]8;;https://cli.r-lib.org/1a]8;;, ]8;;https://cli.r-lib.org/2b]8;;, and ]8;;https://cli.r-lib.org/3c]8;;

8 changes: 8 additions & 0 deletions tests/testthat/test-links.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,12 @@ test_that_cli(config = "plain", links = c("all", "none"),
url <- paste0("https://cli.r-lib.org/", 1:3)
cli_text("{.href {url}}")
})

expect_snapshot({
url <- structure(
paste0("https://cli.r-lib.org/", 1:3),
names = letters[1:3]
)
cli_text("{.href {url}}")
})
})

0 comments on commit 4b5b889

Please sign in to comment.