Skip to content

New {.bullets } style #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 9 additions & 3 deletions R/cliapp-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions R/inline.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion R/themes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
)
}

Expand Down
12 changes: 9 additions & 3 deletions man/themes.Rd

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

192 changes: 192 additions & 0 deletions tests/testthat/_snaps/inline-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cliMessage>
Some values * a
* b
* c
* d
* e blah blah

---

Code
cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah")
Message <cliMessage>
Some values
* a
* b
* c
* d
* e
blah blah

---

Code
cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah")
Message <cliMessage>
Some values * "a"
* "b"
* "c"
* "d"
* "e" blah blah

---

Code
cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah")
Message <cliMessage>
Some values
* "a"
* "b"
* "c"
* "d"
* "e"
blah blah

# span.bullets [ansi]

Code
cli_text("Some values {.bullets {letters[1:5]}} blah blah")
Message <cliMessage>
Some values * a
* b
* c
* d
* e blah blah

---

Code
cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah")
Message <cliMessage>
Some values
* a
* b
* c
* d
* e
blah blah

---

Code
cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah")
Message <cliMessage>
Some values * "a"
* "b"
* "c"
* "d"
* "e" blah blah

---

Code
cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah")
Message <cliMessage>
Some values
* "a"
* "b"
* "c"
* "d"
* "e"
blah blah

# span.bullets [unicode]

Code
cli_text("Some values {.bullets {letters[1:5]}} blah blah")
Message <cliMessage>
Some values • a
• b
• c
• d
• e blah blah

---

Code
cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah")
Message <cliMessage>
Some values
• a
• b
• c
• d
• e
blah blah

---

Code
cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah")
Message <cliMessage>
Some values • "a"
• "b"
• "c"
• "d"
• "e" blah blah

---

Code
cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah")
Message <cliMessage>
Some values
• "a"
• "b"
• "c"
• "d"
• "e"
blah blah

# span.bullets [fancy]

Code
cli_text("Some values {.bullets {letters[1:5]}} blah blah")
Message <cliMessage>
Some values • a
• b
• c
• d
• e blah blah

---

Code
cli_text("Some values\f{.bullets {letters[1:5]}}\fblah blah")
Message <cliMessage>
Some values
• a
• b
• c
• d
• e
blah blah

---

Code
cli_text("Some values {.bullets {.val {letters[1:5]}}} blah blah")
Message <cliMessage>
Some values • "a"
• "b"
• "c"
• "d"
• "e" blah blah

---

Code
cli_text("Some values\f{.bullets {.val {letters[1:5]}}}\fblah blah")
Message <cliMessage>
Some values
• "a"
• "b"
• "c"
• "d"
• "e"
blah blah

15 changes: 15 additions & 0 deletions tests/testthat/test-inline-2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
})