| 1 |
#' @description |
| 2 |
#' `new_pillar_shaft_simple()` provides an implementation of the `pillar_shaft` |
| 3 |
#' class suitable for output that has a fixed formatting, which will be |
| 4 |
#' truncated with a continuation character (ellipsis or `~`) if it doesn't fit |
| 5 |
#' the available width. |
| 6 |
#' By default, the required width is computed from the natural width of the |
| 7 |
#' `formatted` argument. |
| 8 |
#' |
| 9 |
#' @details |
| 10 |
#' The `formatted` argument may also contain ANSI escapes to change color |
| 11 |
#' or other attributes of the text, provided e.g. by the \pkg{cli} package. |
| 12 |
#' |
| 13 |
#' @inheritParams tibble::char |
| 14 |
#' @param ... Passed on to [new_pillar_shaft()]. |
| 15 |
#' @param formatted The data to show, an object coercible to [character]. |
| 16 |
#' @param align Alignment of the column. |
| 17 |
#' @param na String to use as `NA` value, defaults to `"NA"` styled with |
| 18 |
#' [style_na()] with fallback if color is not available. |
| 19 |
#' @param na_indent Indentation of `NA` values. |
| 20 |
#' @param short_formatted If provided, a character vector of the same length as |
| 21 |
#' `formatted`, to be used when the available width is insufficient to show |
| 22 |
#' the full output. |
| 23 |
#' @export |
| 24 |
#' @rdname new_pillar_shaft |
| 25 |
new_pillar_shaft_simple <- function(formatted, ..., width = NULL, align = "left", |
| 26 |
min_width = NULL, na = NULL, na_indent = 0L, |
| 27 |
shorten = c("back", "front", "mid", "abbreviate"), |
| 28 |
short_formatted = NULL) { |
| 29 |
formatted <- as.character(formatted) |
| 30 |
|
| 31 |
if (is.null(width)) { |
| 32 |
width <- get_max_extent(formatted) |
| 33 |
} |
| 34 |
|
| 35 |
if (is.null(na)) { |
| 36 |
na <- pillar_na() |
| 37 |
} |
| 38 |
|
| 39 |
if (missing(shorten)) { |
| 40 |
shorten <- NULL |
| 41 |
} else if (!is.null(shorten)) { |
| 42 |
shorten <- arg_match(shorten) |
| 43 |
} |
| 44 |
|
| 45 |
if (!is.null(short_formatted)) { |
| 46 |
# stopifnot(get_extent(short_formatted) <= get_extent(formatted)) |
| 47 |
if (is.null(min_width)) { |
| 48 |
min_width <- get_max_extent(short_formatted) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
new_pillar_shaft( |
| 53 |
list(formatted), |
| 54 |
..., |
| 55 |
width = width, |
| 56 |
min_width = min_width, |
| 57 |
align = align, |
| 58 |
na = na, |
| 59 |
na_indent = na_indent, |
| 60 |
shorten = shorten, |
| 61 |
short_formatted = short_formatted, |
| 62 |
class = "pillar_shaft_simple" |
| 63 |
) |
| 64 |
} |
| 65 |
|
| 66 |
#' @export |
| 67 |
format.pillar_shaft_simple <- function(x, width, ...) { |
| 68 |
align <- attr(x, "align", exact = TRUE) |
| 69 |
shorten <- attr(x, "shorten", exact = TRUE) |
| 70 |
short_formatted <- attr(x, "short_formatted", exact = TRUE) |
| 71 |
desired_width <- get_width(x) |
| 72 |
shaft <- as.character(x[[1]]) |
| 73 |
if (width < desired_width) { |
| 74 |
if (is.null(short_formatted)) { |
| 75 |
shaft <- str_trunc(shaft, width, shorten) |
| 76 |
} else { |
| 77 |
short_idx <- !is.na(shaft) & (get_extent(shaft) > width) |
| 78 |
shaft[short_idx] <- short_formatted[short_idx] |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
shaft[is.na(shaft)] <- paste0( |
| 83 |
strrep(" ", attr(x, "na_indent", exact = TRUE)), |
| 84 |
attr(x, "na", exact = TRUE) |
| 85 |
) |
| 86 |
|
| 87 |
new_ornament(shaft, width = width, align = align) |
| 88 |
} |