From 7c4c6123be5841b52c37f7e267fc45787d0c4c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Thu, 21 Oct 2021 14:28:47 +0200 Subject: [PATCH 1/2] New {.bullets } style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To collapse vectors into bullets: ``` ❯ cli_text("Some values {.bullets {letters[1:5]}} blah blah") Some values • a • b • c • d • e blah blah ``` Closes #265. --- R/cliapp-docs.R | 12 +++++++++--- R/inline.R | 16 +++++++++++----- R/themes.R | 9 ++++++++- man/themes.Rd | 12 +++++++++--- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/R/cliapp-docs.R b/R/cliapp-docs.R index 586f5d39b..6e5e9549b 100644 --- a/R/cliapp-docs.R +++ b/R/cliapp-docs.R @@ -328,10 +328,16 @@ NULL #' * `transform`: A function to call on glue substitutions, before #' collapsing them. Note that `transform` is applied prior to #' implementing color via ANSI sequences. -#' * `vec_last`: The last separator when collapsing vectors. -#' * `vec_sep`: The separator to use when collapsing vectors. +#' * `vec_last`: The last separator when collapsing vectors. It may be a +#' function. +#' * `vec_after`: Added after vector elements. It may be a function. +#' * `vec_before`: Added before vector elements. It may be a function. +#' * `vec_sep`: The separator to use when collapsing vectors. It may be +#' a function. It may be a function. +#' * `vec_sep2`: The separator to use when collapsing a length 2 vector. +#' It may be a function. #' * `vec_trunc`: Vectors longer than this will be truncated. Defaults to -#' 100. +#' 100. It may be a function that returns the limit. #' #' More properties might be added later. If you think that a property is #' not applied properly to an element, please open an issue about it in diff --git a/R/inline.R b/R/inline.R index 290ba1989..72a517c2c 100644 --- a/R/inline.R +++ b/R/inline.R @@ -32,18 +32,24 @@ inline_generic <- function(app, x, style) { } inline_collapse <- function(x, style = list()) { - sep <- style[["vec_sep"]] %||% ", " + sep <- call_if_fun(style[["vec_sep"]]) %||% ", " if (length(x) >= 3) { - last <- style$vec_last %||% ", and " + last <- call_if_fun(style$vec_last) %||% ", and " } else { - last <- style$vec_sep2 %||% style$vec_last %||% " and " + last <- call_if_fun(style$vec_sep2) %||% + call_if_fun(style$vec_last) %||% + " and " } - trunc <- style$vec_trunc %||% 100L + trunc <- call_if_fun(style$vec_trunc) %||% 100L if (length(x) > trunc) { x <- c(x[1:trunc], cli::symbol$ellipsis) last <- sep } - glue::glue_collapse(as.character(x), sep = sep, last = last) + + before <- call_if_fun(style[["vec_before"]]) %||% "" + after <- call_if_fun(style[["vec_after"]]) %||% "" + ft <- paste0(before, as.character(x), after) + glue::glue_collapse(ft, sep = sep, last = last) } #' This glue transformer performs the inline styling of cli diff --git a/R/themes.R b/R/themes.R index 582a3458f..b72c25ae9 100644 --- a/R/themes.R +++ b/R/themes.R @@ -224,7 +224,14 @@ builtin_theme <- function(dark = getOption("cli_theme_dark", "auto")) { color = "green" ), span.or = list(vec_sep2 = " or ", vec_last = ", or "), - span.timestamp = list(before = "[", after = "]", color = "grey") + span.timestamp = list(before = "[", after = "]", color = "grey"), + + # bullet list + span.bullets = list( + vec_before = function() paste0(symbol$bullet, " "), + vec_sep = "\f", + vec_last = "\f" + ) ) } diff --git a/man/themes.Rd b/man/themes.Rd index 42edbf96e..53821709a 100644 --- a/man/themes.Rd +++ b/man/themes.Rd @@ -109,10 +109,16 @@ text. \item \code{transform}: A function to call on glue substitutions, before collapsing them. Note that \code{transform} is applied prior to implementing color via ANSI sequences. -\item \code{vec_last}: The last separator when collapsing vectors. -\item \code{vec_sep}: The separator to use when collapsing vectors. +\item \code{vec_last}: The last separator when collapsing vectors. It may be a +function. +\item \code{vec_after}: Added after vector elements. It may be a function. +\item \code{vec_before}: Added before vector elements. It may be a function. +\item \code{vec_sep}: The separator to use when collapsing vectors. It may be +a function. It may be a function. +\item \code{vec_sep2}: The separator to use when collapsing a length 2 vector. +It may be a function. \item \code{vec_trunc}: Vectors longer than this will be truncated. Defaults to -100. +100. It may be a function that returns the limit. } More properties might be added later. If you think that a property is From bfaabf3658c229d406591b4436991e37720476cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Thu, 21 Oct 2021 14:45:51 +0200 Subject: [PATCH 2/2] span.bullets test cases --- tests/testthat/_snaps/inline-2.md | 192 ++++++++++++++++++++++++++++++ tests/testthat/test-inline-2.R | 15 +++ 2 files changed, 207 insertions(+) diff --git a/tests/testthat/_snaps/inline-2.md b/tests/testthat/_snaps/inline-2.md index a09b6fbfe..3c9e36d6d 100644 --- a/tests/testthat/_snaps/inline-2.md +++ b/tests/testthat/_snaps/inline-2.md @@ -230,3 +230,195 @@ [5] fugiat cupidatat laboris officia esse ex aliqua. Ullamco [6] mollit adipisicing anim. +# span.bullets [plain] + + Code + cli_text("Some values {.bullets {letters[1:5]}} blah blah") + Message + Some values * a + * b + * c + * d + * e blah blah + +--- + + Code + cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah") + Message + Some values + * a + * b + * c + * d + * e + blah blah + +--- + + Code + cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah") + Message + Some values * "a" + * "b" + * "c" + * "d" + * "e" blah blah + +--- + + Code + cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah") + Message + Some values + * "a" + * "b" + * "c" + * "d" + * "e" + blah blah + +# span.bullets [ansi] + + Code + cli_text("Some values {.bullets {letters[1:5]}} blah blah") + Message + Some values * a + * b + * c + * d + * e blah blah + +--- + + Code + cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah") + Message + Some values + * a + * b + * c + * d + * e + blah blah + +--- + + Code + cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah") + Message + Some values * "a" + * "b" + * "c" + * "d" + * "e" blah blah + +--- + + Code + cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah") + Message + Some values + * "a" + * "b" + * "c" + * "d" + * "e" + blah blah + +# span.bullets [unicode] + + Code + cli_text("Some values {.bullets {letters[1:5]}} blah blah") + Message + Some values • a + • b + • c + • d + • e blah blah + +--- + + Code + cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah") + Message + Some values + • a + • b + • c + • d + • e + blah blah + +--- + + Code + cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah") + Message + Some values • "a" + • "b" + • "c" + • "d" + • "e" blah blah + +--- + + Code + cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah") + Message + Some values + • "a" + • "b" + • "c" + • "d" + • "e" + blah blah + +# span.bullets [fancy] + + Code + cli_text("Some values {.bullets {letters[1:5]}} blah blah") + Message + Some values • a + • b + • c + • d + • e blah blah + +--- + + Code + cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah") + Message + Some values + • a + • b + • c + • d + • e + blah blah + +--- + + Code + cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah") + Message + Some values • "a" + • "b" + • "c" + • "d" + • "e" blah blah + +--- + + Code + cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah") + Message + Some values + • "a" + • "b" + • "c" + • "d" + • "e" + blah blah + diff --git a/tests/testthat/test-inline-2.R b/tests/testthat/test-inline-2.R index 09ef430b5..f40ab793f 100644 --- a/tests/testthat/test-inline-2.R +++ b/tests/testthat/test-inline-2.R @@ -105,3 +105,18 @@ test_that("line breaks", { txt2 <- paste0(txt, "\f", txt) expect_snapshot(ansi_strwrap(txt2, width = 60)) }) + +test_that_cli("span.bullets", { + expect_snapshot( + cli_text("Some values {.bullets {letters[1:5]}} blah blah") + ) + expect_snapshot( + cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah") + ) + expect_snapshot( + cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah") + ) + expect_snapshot( + cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah") + ) +})