Permalink
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (44 sloc) 1.47 KB
#' @include shaft.R
#' @description
#' `new_pillar_shaft_simple()` provides an implementation of the `pillar_shaft`
#' class suitable for output that has a fixed formatting, which will be
#' truncated with a continuation character (ellipsis or `~`) if it doesn't fit
#' the available width.
#' By default, the required width is computed from the natural width of the
#' `formatted` argument.
#'
#' @details
#' The `formatted` argument may also contain ANSI escapes to change color
#' or other attributes of the text, see [crayon].
#'
#' @param formatted An object coercible to [character].
#' @param align Alignment of the column.
#' @param na_indent Indention of `NA` values.
#' @export
#' @rdname new_pillar_shaft
new_pillar_shaft_simple <- function(formatted, ..., width = NULL, align = "left",
min_width = NULL, na_indent = 0L) {
if (is.null(width)) {
width <- get_max_extent(as.character(formatted))
}
new_pillar_shaft(
list(formatted),
...,
width = width,
min_width = min_width,
align = align,
na_indent = na_indent,
subclass = "pillar_shaft_simple"
)
}
#' @export
format.pillar_shaft_simple <- function(x, width, ...) {
align <- attr(x, "align")
desired_width <- get_width(x)
shaft <- as.character(x[[1]])
if (width < desired_width) {
shaft <- str_trunc(shaft, width)
}
shaft[is.na(shaft)] <- paste0(strrep(" ", attr(x, "na_indent")), pillar_na())
new_ornament(shaft, width = width, align = align)
}