| 1 |
MIN_PILLAR_WIDTH <- 5L |
| 2 |
|
| 3 |
style_type <- function(x) { |
| 4 |
force(x) |
| 5 |
x <- style_subtle(x) |
| 6 |
crayon_italic(x) |
| 7 |
} |
| 8 |
|
| 9 |
#' Prepare a column type for formatting |
| 10 |
#' |
| 11 |
#' Calls [type_sum()] to format the type. |
| 12 |
#' Call [format()] on the result to render column types. |
| 13 |
#' |
| 14 |
#' @param x A vector for which the type is to be retrieved. |
| 15 |
#' @inheritParams rlang::args_dots_empty |
| 16 |
#' @export |
| 17 |
#' @examples |
| 18 |
#' format(new_pillar_type("a")) |
| 19 |
#' format(new_pillar_type(factor("a"))) |
| 20 |
new_pillar_type <- function(x, ...) { |
| 21 |
"!!!!DEBUG new_pillar_type(`v(class(x))`)" |
| 22 |
if (!missing(...)) { |
| 23 |
check_dots_empty(action = warn) |
| 24 |
} |
| 25 |
|
| 26 |
new_pillar_type_obj(get_pillar_type(x)) |
| 27 |
} |
| 28 |
|
| 29 |
new_pillar_type_obj <- function(type) { |
| 30 |
# Must wrap in a list, because type_sum() can return a classed object |
| 31 |
ret <- structure(list(type), class = "pillar_type") |
| 32 |
extent <- get_extent(format_type_sum(type, NULL)) |
| 33 |
ret <- set_width(ret, width = max(extent, MIN_PILLAR_WIDTH)) |
| 34 |
ret <- set_min_width(ret, MIN_PILLAR_WIDTH) |
| 35 |
ret |
| 36 |
} |
| 37 |
|
| 38 |
get_pillar_type <- function(x) { |
| 39 |
type <- type_sum(x) |
| 40 |
if (length(type) == 0L) type <- "?" |
| 41 |
# Can return a classed object to be formatted by format_type_sum() |
| 42 |
type[] <- as.character(type[[1L]]) |
| 43 |
type |
| 44 |
} |
| 45 |
|
| 46 |
#' @export |
| 47 |
format.pillar_type <- function(x, width = NULL, ..., is_focus = FALSE) { |
| 48 |
out <- format_type_sum(x[[1]], width) |
| 49 |
if (is_focus) { |
| 50 |
out <- crayon_underline(out) |
| 51 |
} |
| 52 |
out |
| 53 |
} |
| 54 |
|
| 55 |
format_full_pillar_type <- function(x) { |
| 56 |
type <- get_pillar_type(x) |
| 57 |
format_type_sum(type, NULL) |
| 58 |
} |
| 59 |
|
| 60 |
#' Format a type summary |
| 61 |
#' |
| 62 |
#' Called on values returned from [type_sum()] for defining the description |
| 63 |
#' in the capital. |
| 64 |
#' |
| 65 |
#' Two methods are implemented by default for this generic: the default method, |
| 66 |
#' and the method for the `"AsIs"` class. |
| 67 |
#' Return `I("type")` from your [type_sum()] implementation to format the type |
| 68 |
#' without angle brackets. |
| 69 |
#' For even more control over the formatting, implement your own method. |
| 70 |
#' |
| 71 |
#' @param x A return value from `type_sum()` |
| 72 |
#' @param width The desired total width. If the returned string still is |
| 73 |
#' wider, it will be trimmed. Can be `NULL`. |
| 74 |
#' @inheritParams rlang::args_dots_used |
| 75 |
#' |
| 76 |
#' @export |
| 77 |
#' @examples |
| 78 |
#' # Default method: show the type with angle brackets |
| 79 |
#' writeLines(format_type_sum("dbl", width = NULL)) |
| 80 |
#' pillar(1) |
| 81 |
#' |
| 82 |
#' # AsIs method: show the type without angle brackets |
| 83 |
#' type_sum.accel <- function(x) { |
| 84 |
#' I("kg m/s^2") |
| 85 |
#' } |
| 86 |
#' |
| 87 |
#' # Typically done through NAMESPACE |
| 88 |
#' # (perhaps with an @export directive in roxygen2) |
| 89 |
#' registerS3method("type_sum", "accel", type_sum.accel) |
| 90 |
#' |
| 91 |
#' accel <- structure(9.81, class = "accel") |
| 92 |
#' pillar(accel) |
| 93 |
format_type_sum <- function(x, width, ...) { |
| 94 |
if (!missing(...)) { |
| 95 |
check_dots_used(action = warn) |
| 96 |
} |
| 97 |
|
| 98 |
UseMethod("format_type_sum") |
| 99 |
} |
| 100 |
|
| 101 |
#' @export |
| 102 |
#' @rdname format_type_sum |
| 103 |
format_type_sum.default <- function(x, width, ...) { |
| 104 |
if (!is.null(width) && width - 2 < get_extent(x)) { |
| 105 |
x <- substr(x, 1, max(width - 2, 0)) |
| 106 |
} |
| 107 |
style_type(paste0("<", x, ">")) |
| 108 |
} |
| 109 |
|
| 110 |
#' @export |
| 111 |
#' @rdname format_type_sum |
| 112 |
format_type_sum.AsIs <- function(x, width, ...) { |
| 113 |
if (!is.null(width) && width < get_extent(x)) { |
| 114 |
x <- substr(x, 1, width) |
| 115 |
} |
| 116 |
style_type(unclass(x)) |
| 117 |
} |