| 1 |
#' Format a character vector in a tibble |
| 2 |
#' |
| 3 |
#' These functions are reexported as [tibble::char()] and [tibble::set_char_opts()]. |
| 4 |
#' |
| 5 |
#' @keywords internal |
| 6 |
#' @export |
| 7 |
char <- function(x, ..., min_chars = NULL, |
| 8 |
shorten = c("back", "front", "mid", "abbreviate")) { |
| 9 |
stopifnot(is.character(x)) |
| 10 |
check_dots_empty() |
| 11 |
|
| 12 |
if (missing(shorten)) { |
| 13 |
shorten <- NULL |
| 14 |
} |
| 15 |
|
| 16 |
out <- set_char_opts( |
| 17 |
x, |
| 18 |
min_chars = min_chars, |
| 19 |
shorten = shorten |
| 20 |
) |
| 21 |
|
| 22 |
new_class <- c("pillar_char", "pillar_vctr", "vctrs_vctr", "character") |
| 23 |
class(out) <- new_class |
| 24 |
|
| 25 |
out |
| 26 |
} |
| 27 |
|
| 28 |
#' @export |
| 29 |
vec_ptype_full.pillar_char <- function(x, ...) { |
| 30 |
# No longer inherited in vctrs > 0.4.1, |
| 31 |
# https://github.com/r-lib/vctrs/pull/1571 |
| 32 |
format(attr(x, "pillar", exact = TRUE)) |
| 33 |
} |
| 34 |
|
| 35 |
#' @export |
| 36 |
vec_ptype_abbr.pillar_char <- function(x, ...) { |
| 37 |
pillar_attr <- attr(x, "pillar", exact = TRUE) |
| 38 |
|
| 39 |
out <- "char" |
| 40 |
|
| 41 |
shorten <- pillar_attr$shorten |
| 42 |
if (!is.null(shorten) && shorten == "abbr") { |
| 43 |
out <- "abbr" |
| 44 |
} |
| 45 |
|
| 46 |
out |
| 47 |
} |
| 48 |
|
| 49 |
#' @export |
| 50 |
format.pillar_char <- function(x, trim = FALSE, ...) { |
| 51 |
"!!!!DEBUG format.pillar_char()" |
| 52 |
|
| 53 |
shaft <- pillar_shaft(x) |
| 54 |
out <- format(shaft, width = get_width(shaft)) |
| 55 |
if (trim) { |
| 56 |
attributes(out) <- NULL |
| 57 |
} else { |
| 58 |
out <- format(out, align = "left") |
| 59 |
} |
| 60 |
out |
| 61 |
} |
| 62 |
|
| 63 |
#' @export |
| 64 |
#' @rdname char |
| 65 |
set_char_opts <- function(x, ..., min_chars = NULL, |
| 66 |
shorten = c("back", "front", "mid", "abbreviate")) { |
| 67 |
check_dots_empty() |
| 68 |
|
| 69 |
if (missing(shorten)) { |
| 70 |
shorten <- NULL |
| 71 |
} else if (!is.null(shorten)) { |
| 72 |
shorten <- arg_match(shorten) |
| 73 |
} |
| 74 |
|
| 75 |
pillar_attr <- structure( |
| 76 |
list( |
| 77 |
min_chars = min_chars, |
| 78 |
shorten = shorten |
| 79 |
), |
| 80 |
class = c("pillar_char_attr", "pillar_vctr_attr", "tibble_vec_attr") |
| 81 |
) |
| 82 |
attr(x, "pillar") <- pillar_attr |
| 83 |
x |
| 84 |
} |
| 85 |
|
| 86 |
#' @export |
| 87 |
format.pillar_char_attr <- function(x, ...) { |
| 88 |
out <- "pillar_char" |
| 89 |
|
| 90 |
min_chars <- x$min_chars |
| 91 |
if (!is.null(min_chars)) { |
| 92 |
out <- paste0(out, "(", min_chars, ")") |
| 93 |
} |
| 94 |
|
| 95 |
shorten <- x$shorten |
| 96 |
if (!is.null(shorten)) { |
| 97 |
extra <- switch(shorten, |
| 98 |
back = ">", |
| 99 |
front = "<", |
| 100 |
mid = "|", |
| 101 |
never = "=", |
| 102 |
abbr = "&" |
| 103 |
) |
| 104 |
|
| 105 |
out <- paste0(out, extra) |
| 106 |
} |
| 107 |
|
| 108 |
out |
| 109 |
} |
| 110 |
|
| 111 |
#' @export |
| 112 |
vec_ptype2.pillar_char.pillar_char <- function(x, y, ...) { |
| 113 |
x |
| 114 |
} |
| 115 |
#' @export |
| 116 |
vec_ptype2.pillar_char.character <- function(x, y, ...) { |
| 117 |
x |
| 118 |
} |
| 119 |
#' @export |
| 120 |
vec_ptype2.character.pillar_char <- function(x, y, ...) { |
| 121 |
y |
| 122 |
} |
| 123 |
|
| 124 |
#' @export |
| 125 |
vec_cast.pillar_char.pillar_char <- function(x, to, ...) { |
| 126 |
pillar_x <- attr(x, "pillar", exact = TRUE) |
| 127 |
pillar_to <- attr(to, "pillar", exact = TRUE) |
| 128 |
|
| 129 |
pillar_x_shorten <- pillar_x$shorten |
| 130 |
pillar_to_shorten <- pillar_to$shorten |
| 131 |
|
| 132 |
if (!is.null(pillar_x_shorten) && !is.null(pillar_to_shorten)) { |
| 133 |
if (!identical(pillar_x$shorten, pillar_to$shorten)) { |
| 134 |
abort("Only `pillar_char` objects with the same shortening setting can be combined.") |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
pillar_x_min_chars <- pillar_x$min_chars |
| 139 |
pillar_to_min_chars <- pillar_to$min_chars |
| 140 |
|
| 141 |
if (!is.null(pillar_x_min_chars) && !is.null(pillar_to_min_chars)) { |
| 142 |
pillar_to$min_chars <- max(pillar_x_min_chars, pillar_to_min_chars) |
| 143 |
} |
| 144 |
|
| 145 |
attr(x, "pillar") <- pillar_to |
| 146 |
x |
| 147 |
} |
| 148 |
#' @export |
| 149 |
vec_cast.character.pillar_char <- function(x, to, ...) { |
| 150 |
vec_data(x) |
| 151 |
} |
| 152 |
#' @export |
| 153 |
vec_cast.pillar_char.character <- function(x, to, ...) { |
| 154 |
out <- char(x) |
| 155 |
attr(out, "pillar") <- attr(to, "pillar") |
| 156 |
out |
| 157 |
} |