Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Suggests:
testthat (>= 3.2.0),
tibble,
whoami,
withr
withr,
fs
Config/Needs/website:
r-lib/asciicast,
bench,
Expand Down
8 changes: 8 additions & 0 deletions R/ansi-hyperlink.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ make_link_help <- function(txt) {
# -- {.href} --------------------------------------------------------------

make_link_href <- function(txt) {
# Addresses #681 - if already linked, strip link and re-link
linked <- grepl("\007|\033\\\\", txt)
txt[linked] <- ansi_strip(txt[linked])

mch <- re_match(txt, "^\\[(?<text>.*)\\]\\((?<url>.*)\\)$")
text <- ifelse(is.na(mch$text), txt, mch$text)
url <- ifelse(is.na(mch$url), txt, mch$url)
Expand All @@ -263,6 +267,10 @@ make_link_href <- function(txt) {
# -- {.run} ---------------------------------------------------------------

make_link_run <- function(txt) {
# Addresses #681 - if already linked, strip link and re-link
linked <- grepl("\007|\033\\\\", txt)
txt[linked] <- ansi_strip(txt[linked])

mch <- re_match(txt, "^\\[(?<text>.*)\\]\\((?<url>.*)\\)$")
text <- ifelse(is.na(mch$text), txt, mch$text)
code <- ifelse(is.na(mch$url), txt, mch$url)
Expand Down
132 changes: 132 additions & 0 deletions tests/testthat/_snaps/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -1127,3 +1127,135 @@
Message
]8;;aaa-pkgdown::accessibility-zzzpkgdown::accessibility]8;;

# {.run} and {.href} with fs_path [plain-none]

Code
cli_text("{.run ['hi mom']({fs::path(path)})}")
Message
`'hi mom'`
Code
cli_text("{.run {fs::path(path)}}")
Message
''`~/Desktop/foo.R`''

---

Code
cli_text("{.href [link]({fs::path(path)})}")
Message
link (<'~/Desktop/foo.R'>)
Code
cli_text("{.href {fs::path(path)}}")
Message
''<~/Desktop/foo.R>''

---

Code
cli_text("{.file {fs::path(path)}}")
Message
'~/Desktop/foo.R'
Code
cli_text("{.path {fs::path(path)}}")
Message
'~/Desktop/foo.R'

# {.run} and {.href} with fs_path [fancy-none]

Code
cli_text("{.run ['hi mom']({fs::path(path)})}")
Message
`'hi mom'`
Code
cli_text("{.run {fs::path(path)}}")
Message
''`~/Desktop/foo.R`''

---

Code
cli_text("{.href [link]({fs::path(path)})}")
Message
link (<~/Desktop/foo.R>)
Code
cli_text("{.href {fs::path(path)}}")
Message
''<~/Desktop/foo.R>''

---

Code
cli_text("{.file {fs::path(path)}}")
Message
~/Desktop/foo.R
Code
cli_text("{.path {fs::path(path)}}")
Message
~/Desktop/foo.R

# {.run} and {.href} with fs_path [plain-all]

Code
cli_text("{.run ['hi mom']({fs::path(path)})}")
Message
]8;;x-r-run:'~/Desktop/foo.R''hi mom']8;;
Code
cli_text("{.run {fs::path(path)}}")
Message
']8;;x-r-run:~/Desktop/foo.R~/Desktop/foo.R]8;;'

---

Code
cli_text("{.href [link]({fs::path(path)})}")
Message
]8;;'~/Desktop/foo.R'link]8;;
Code
cli_text("{.href {fs::path(path)}}")
Message
''<]8;;~/Desktop/foo.R~/Desktop/foo.R]8;;>''

---

Code
cli_text("{.file {fs::path(path)}}")
Message
']8;;file:///Users/rundel/Desktop/foo.R~/Desktop/foo.R]8;;'
Code
cli_text("{.path {fs::path(path)}}")
Message
']8;;file:///Users/rundel/Desktop/foo.R~/Desktop/foo.R]8;;'

# {.run} and {.href} with fs_path [fancy-all]

Code
cli_text("{.run ['hi mom']({fs::path(path)})}")
Message
]8;;x-r-run:~/Desktop/foo.R'hi mom']8;;
Code
cli_text("{.run {fs::path(path)}}")
Message
]8;;x-r-run:~/Desktop/foo.R~/Desktop/foo.R]8;;

---

Code
cli_text("{.href [link]({fs::path(path)})}")
Message
]8;;~/Desktop/foo.Rlink]8;;
Code
cli_text("{.href {fs::path(path)}}")
Message
''<]8;;~/Desktop/foo.R~/Desktop/foo.R]8;;>''

---

Code
cli_text("{.file {fs::path(path)}}")
Message
]8;;file:///Users/rundel/Desktop/foo.R~/Desktop/foo.R]8;;
Code
cli_text("{.path {fs::path(path)}}")
Message
]8;;file:///Users/rundel/Desktop/foo.R~/Desktop/foo.R]8;;

32 changes: 32 additions & 0 deletions tests/testthat/test-links.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ test_that_cli(configs = "plain", links = "all", ".run with custom format", {
})
})


# -- {.topic} -------------------------------------------------------------

test_that_cli(configs = "plain", links = c("all", "none"), "{.topic}", {
Expand Down Expand Up @@ -325,3 +326,34 @@ test_that_cli(
})
}
)


# -- Issue #681 - fs::path weirdness -------------------------------------

test_that_cli(
configs = c("plain", "fancy"),
links = c("all", "none"),
"{.run} and {.href} with fs_path",
{
skip_if_not_installed("fs")

path <- "~/Desktop/foo.R"

# Not working
expect_snapshot({
cli_text("{.run ['hi mom']({fs::path(path)})}")
cli_text("{.run {fs::path(path)}}")
})

expect_snapshot({
cli_text("{.href [link]({fs::path(path)})}")
cli_text("{.href {fs::path(path)}}")
})

# Working
expect_snapshot({
cli_text("{.file {fs::path(path)}}")
cli_text("{.path {fs::path(path)}}")
})
}
)
Loading