Skip to contents

Takes a vector or list and enumerates its elements in a single string. Convenience function combining wrap_chr() and cli::ansi_collapse() with slightly differing defaults (last defaults to the value of sep2).

Usage

enum_str(
  x,
  sep = ", ",
  sep2 = " and ",
  last = sep2,
  trunc = Inf,
  width = Inf,
  ellipsis = cli::symbol$ellipsis,
  style = c("both-ends", "head"),
  wrap = ""
)

Arguments

x

Character vector, or an object with an as.character() method to collapse.

sep

Separator. A character string.

sep2

Separator for the special case that x contains only two elements. A character string.

last

Last separator, if there is no truncation. E.g. use ", and " for the serial comma. A character string.

trunc

Maximum number of elements to show. For style = "head" at least trunc = 1 is used. For style = "both-ends" at least trunc = 5 is used, even if a smaller number is specified.

width

Limit for the display width of the result, in characters. This is a hard limit, and the output will never exceed it. This argument is not implemented for the "both-ends" style, which always uses Inf, with a warning if a finite width value is set.

ellipsis

Character string to use at the place of the truncation. By default, the Unicode ellipsis character is used if the console is UTF-8, and three dots otherwise.

style

Truncation style:

  • both-ends: the default, shows the beginning and end of the vector, and skips elements in the middle if needed.

  • head: shows the beginning of the vector, and skips elements at the end, if needed.

wrap

Character sequence x is to be wrapped in. A character vector or something coercible to.

Value

A character scalar.

Examples

# by default, `last` defaults to `sep2`
pal::enum_str(1:2)
#> [1] "1 and 2"
pal::enum_str(1:3)
#> [1] "1, 2 and 3"

# input is optionally wrapped in a character sequence
pal::enum_str(letters[1:3],
              wrap = "`")
#> [1] "`a`, `b` and `c`"