1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/glue.R
\name{ansi_collapse}
\alias{ansi_collapse}
\title{Collapse a vector into a string scalar}
\usage{
ansi_collapse(
x,
sep = ", ",
sep2 = sub("^,", "", last),
last = ", and ",
trunc = Inf,
width = Inf,
ellipsis = symbol$ellipsis,
style = c("both-ends", "head")
)
}
\arguments{
\item{x}{Character vector, or an object with an \code{as.character()} method
to collapse.}
\item{sep}{Separator. A character string.}
\item{sep2}{Separator for the special case that \code{x} contains only two
elements. A character string. Defaults to the value of \code{last} without the
serial comma.}
\item{last}{Last separator, if there is no truncation. E.g. use
\code{", and "} for the \href{https://en.wikipedia.org/wiki/Serial_comma}{serial comma}. A character string.}
\item{trunc}{Maximum number of elements to show. For \code{style = "head"}
at least \code{trunc = 1} is used. For \code{style = "both-ends"} at least
\code{trunc = 5} is used, even if a smaller number is specified.}
\item{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 \code{"both-ends"} style, which
always uses \code{Inf}, with a warning if a finite \code{width} value is set.}
\item{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.}
\item{style}{Truncation style:
\itemize{
\item \code{both-ends}: the default, shows the beginning and end of the vector,
and skips elements in the middle if needed.
\item \code{head}: shows the beginning of the vector, and skips elements at the
end, if needed.
}}
}
\value{
Character scalar. It is \code{NA_character_} if any elements in \code{x}
are \code{NA}.
}
\description{
Features:
\itemize{
\item custom separator (\code{sep}),
\item custom separator for length-two input (\code{sep2}),
\item custom last separator (\code{last}),
\item adds ellipsis to truncated strings,
\item uses Unicode ellipsis character on UTF-8 console,
\item can collapse "from both ends", with \code{style = "both-ends"},
\item can consider a limit for the display width of the result, in characters,
\item handles ANSI control sequences correctly when measuring display width.
}
}
\examples{
ansi_collapse(letters)
# truncate
ansi_collapse(letters, trunc = 5)
# head style
ansi_collapse(letters, trunc = 5, style = "head")
}
\seealso{
\code{glue_collapse} in the glue package inspired this function.
}
|