| 1 |
#' Components of a pillar |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' `r lifecycle::badge("experimental")` |
| 5 |
#' |
| 6 |
#' `new_pillar_component()` constructs an object of class `"pillar_component"`. |
| 7 |
#' It is used by custom [ctl_new_pillar()] methods to create pillars with |
| 8 |
#' nonstandard components. |
| 9 |
#' |
| 10 |
#' @details |
| 11 |
#' Objects of class `"pillar"` are internally a named lists of their components. |
| 12 |
#' The default components for pillars created by [pillar()] are: |
| 13 |
#' `title` (may be missing), `type`, and `data`. |
| 14 |
#' Each component is a `"pillar_component"` object. |
| 15 |
#' |
| 16 |
#' This class captures contents that can be fitted in a simple column. |
| 17 |
#' Compound columns are represented by multiple pillar objects, each with their |
| 18 |
#' own components. |
| 19 |
#' |
| 20 |
#' @inheritParams rlang::args_dots_empty |
| 21 |
#' @param x A bare list of length one (for `new_pillar_component()`), |
| 22 |
#' or an object with `"width"` and `"min_width"` attributes |
| 23 |
#' (for `pillar_component()`). |
| 24 |
#' @param width,min_width Width and minimum width for the new component. |
| 25 |
#' If `min_width` is `NULL`, it is assumed to match `width`. |
| 26 |
#' @export |
| 27 |
#' @examples |
| 28 |
#' new_pillar_component(list(letters[1:3]), width = 1) |
| 29 |
#' pillar_component(new_pillar_title("letters")) |
| 30 |
#' pillar_component(new_pillar_type(letters)) |
| 31 |
#' pillar_component(pillar_shaft(letters[1:3])) |
| 32 |
new_pillar_component <- function(x, ..., width, min_width = NULL) { |
| 33 |
"!!!!DEBUG new_pillar_component(`v(x)`, `v(width)`, `v(min_width)`)" |
| 34 |
|
| 35 |
check_dots_empty() |
| 36 |
stopifnot(rlang::is_bare_list(x)) |
| 37 |
stopifnot(length(x) == 1) |
| 38 |
stopifnot(is_integerish(width), length(width) == 1L) |
| 39 |
if (!is.null(min_width)) { |
| 40 |
stopifnot(is_integerish(min_width), length(min_width) == 1L) |
| 41 |
} |
| 42 |
|
| 43 |
structure( |
| 44 |
x, |
| 45 |
width = as.integer(width), |
| 46 |
min_width = as.integer(min_width %||% width), |
| 47 |
class = "pillar_component" |
| 48 |
) |
| 49 |
} |
| 50 |
|
| 51 |
#' pillar_component() |
| 52 |
#' |
| 53 |
#' `pillar_component()` is a convenience helper that wraps the input in a list |
| 54 |
#' and extracts width and minimum width. |
| 55 |
#' |
| 56 |
#' @export |
| 57 |
#' @rdname new_pillar_component |
| 58 |
pillar_component <- function(x) { |
| 59 |
# FIXME: No longer need to wrap in a list, cell concept abandoned |
| 60 |
new_pillar_component(list(x), width = get_width(x), min_width = get_min_width(x)) |
| 61 |
} |
| 62 |
|
| 63 |
pillar_get_width <- function(x) { |
| 64 |
as.integer(max(map_int(x, get_width))) |
| 65 |
} |
| 66 |
|
| 67 |
pillar_get_min_width <- function(x) { |
| 68 |
as.integer(max(map_int(x, get_min_width))) |
| 69 |
} |
| 70 |
|
| 71 |
pillar_format_parts_2 <- function(x, width, is_focus = FALSE, footnote_idx = 1L) { |
| 72 |
formatted <- map(x, function(.x) format( |
| 73 |
.x[[1L]], |
| 74 |
width = min(width, get_width(.x)), |
| 75 |
is_focus = is_focus, |
| 76 |
footnote_idx = footnote_idx |
| 77 |
)) |
| 78 |
|
| 79 |
align <- attr(formatted[["data"]], "align", exact = TRUE) %||% "left" |
| 80 |
|
| 81 |
flat <- unlist(formatted) |
| 82 |
extent <- get_extent(flat) |
| 83 |
max_extent <- max(extent) |
| 84 |
if (max_extent > width) { |
| 85 |
flat <- str_trunc(flat, width) |
| 86 |
max_extent <- width |
| 87 |
} |
| 88 |
aligned <- align_impl(flat, max_extent, align, " ", extent) |
| 89 |
|
| 90 |
list(max_extent = max_extent, aligned = aligned, components = names(x)) |
| 91 |
} |