Skip to content

Commit

Permalink
New {.bullets } style
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gaborcsardi committed Oct 21, 2021
1 parent 582dd41 commit 7c4c612
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
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.

0 comments on commit 7c4c612

Please sign in to comment.