diff --git a/DESCRIPTION b/DESCRIPTION index 5f8c86f5..ca5ce751 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -43,7 +43,8 @@ Suggests: testthat (>= 3.2.0), tibble, whoami, - withr + withr, + fs Config/Needs/website: r-lib/asciicast, bench, diff --git a/R/ansi-hyperlink.R b/R/ansi-hyperlink.R index c138c50e..7e7471ac 100644 --- a/R/ansi-hyperlink.R +++ b/R/ansi-hyperlink.R @@ -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 <- ifelse(is.na(mch$text), txt, mch$text) url <- ifelse(is.na(mch$url), txt, mch$url) @@ -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 <- ifelse(is.na(mch$text), txt, mch$text) code <- ifelse(is.na(mch$url), txt, mch$url) diff --git a/tests/testthat/_snaps/links.md b/tests/testthat/_snaps/links.md index e42524e3..61e644ee 100644 --- a/tests/testthat/_snaps/links.md +++ b/tests/testthat/_snaps/links.md @@ -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;; + diff --git a/tests/testthat/test-links.R b/tests/testthat/test-links.R index 60f16f8d..e464bf68 100644 --- a/tests/testthat/test-links.R +++ b/tests/testthat/test-links.R @@ -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}", { @@ -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)}}") + }) + } +)