| 1 |
#' Formatting of tbl objects |
| 2 |
#' |
| 3 |
#' See [tibble::formatting] for details. |
| 4 |
#' |
| 5 |
#' @name format_tbl |
| 6 |
#' @export |
| 7 |
#' @keywords internal |
| 8 |
print.tbl <- function(x, width = NULL, ..., n = NULL, max_extra_cols = NULL, |
| 9 |
max_footer_lines = NULL) { |
| 10 |
print_tbl( |
| 11 |
x, width, ..., |
| 12 |
n = n, max_extra_cols = max_extra_cols, max_footer_lines = max_footer_lines |
| 13 |
) |
| 14 |
} |
| 15 |
|
| 16 |
print_tbl <- function(x, width = NULL, ..., |
| 17 |
n_extra = NULL, |
| 18 |
n = NULL, max_extra_cols = NULL, max_footer_lines = NULL) { |
| 19 |
if (!is.null(n_extra)) { |
| 20 |
deprecate_stop("1.6.2", "pillar::print(n_extra = )", "pillar::print(max_extra_cols = )") |
| 21 |
} |
| 22 |
|
| 23 |
# Printing happens as a side effect thanks to `transform = writeLines` . |
| 24 |
# For formatting, the default `transform = identity` returns the data instead. |
| 25 |
format_tbl( |
| 26 |
x, |
| 27 |
width = width, ..., |
| 28 |
n = n, max_extra_cols = max_extra_cols, max_footer_lines = max_footer_lines, |
| 29 |
transform = writeLines |
| 30 |
) |
| 31 |
|
| 32 |
invisible(x) |
| 33 |
} |
| 34 |
|
| 35 |
#' @export |
| 36 |
#' @rdname format_tbl |
| 37 |
format.tbl <- function(x, width = NULL, ..., |
| 38 |
n = NULL, max_extra_cols = NULL, max_footer_lines = NULL) { |
| 39 |
format_tbl( |
| 40 |
x, width, ..., |
| 41 |
n = n, max_extra_cols = max_extra_cols, max_footer_lines = max_footer_lines |
| 42 |
) |
| 43 |
} |
| 44 |
|
| 45 |
format_tbl <- function( |
| 46 |
x, |
| 47 |
width = NULL, |
| 48 |
..., |
| 49 |
n_extra = NULL, |
| 50 |
n = NULL, |
| 51 |
max_extra_cols = NULL, |
| 52 |
max_footer_lines = NULL, |
| 53 |
transform = identity |
| 54 |
) { |
| 55 |
# check_dots_used() gave revdepcheck errors |
| 56 |
|
| 57 |
if (!is.null(n_extra)) { |
| 58 |
deprecate_stop("1.6.2", "pillar::format(n_extra = )", "pillar::format(max_extra_cols = )") |
| 59 |
} |
| 60 |
|
| 61 |
# Reset local cache for each new output |
| 62 |
force(x) |
| 63 |
num_colors(forget = TRUE) |
| 64 |
|
| 65 |
# This is a bit of a hack to allow the setup function to be called twice |
| 66 |
# if the implementer is prepared to handle that. |
| 67 |
# We detect that by checking if the `setup` argument has been evaluated. |
| 68 |
setup_used <- FALSE |
| 69 |
|
| 70 |
# In either case, we expect a `setup` object that can be passed to `tbl_format_header()` |
| 71 |
# as a return from this call. |
| 72 |
setup <- tbl_format_setup( |
| 73 |
x, |
| 74 |
width = width, |
| 75 |
..., |
| 76 |
setup = { |
| 77 |
# This construct updates the `setup_used` variable in the parent scope |
| 78 |
# when the `setup` argument is evaluated. |
| 79 |
setup_used <- TRUE |
| 80 |
NULL |
| 81 |
}, |
| 82 |
n = n, |
| 83 |
max_extra_cols = max_extra_cols, |
| 84 |
max_footer_lines = max_footer_lines, |
| 85 |
focus = attr(x, "pillar_focus") |
| 86 |
) |
| 87 |
|
| 88 |
header <- transform(tbl_format_header(x, setup)) |
| 89 |
|
| 90 |
# If the implementation did not request the `setup` argument in the first call, |
| 91 |
# the default behavior before 1.9.1 is used: the first call already |
| 92 |
# has returned the full setup object. |
| 93 |
# Otherwise, we assume that a second call is required, and we pass it the |
| 94 |
# setup object returned from the first call. |
| 95 |
if (setup_used) { |
| 96 |
setup <- tbl_format_setup( |
| 97 |
x, |
| 98 |
width = width, |
| 99 |
..., |
| 100 |
setup = setup, |
| 101 |
n = n, |
| 102 |
max_extra_cols = max_extra_cols, |
| 103 |
max_footer_lines = max_footer_lines, |
| 104 |
focus = attr(x, "pillar_focus") |
| 105 |
) |
| 106 |
} |
| 107 |
|
| 108 |
# In either case, the `setup` object is now complete and can be used to format the body |
| 109 |
# and the footer. |
| 110 |
body <- transform(tbl_format_body(x, setup)) |
| 111 |
footer <- transform(tbl_format_footer(x, setup)) |
| 112 |
c(header, body, footer) |
| 113 |
} |
| 114 |
|
| 115 |
format_comment <- function(x, width) { |
| 116 |
if (length(x) == 0L) { |
| 117 |
return(character()) |
| 118 |
} |
| 119 |
map_chr(x, wrap, prefix = "# ", width = min(width, cli::console_width())) |
| 120 |
} |
| 121 |
|
| 122 |
# wrap -------------------------------------------------------------------- |
| 123 |
|
| 124 |
NBSP <- "\U00A0" |
| 125 |
|
| 126 |
wrap <- function(..., indent = 0, prefix = "", width) { |
| 127 |
x <- paste0(..., collapse = "") |
| 128 |
wrapped <- strwrap2(x, width - get_extent(prefix), indent) |
| 129 |
wrapped <- paste0(prefix, wrapped) |
| 130 |
wrapped <- gsub(NBSP, " ", wrapped) |
| 131 |
|
| 132 |
paste0(wrapped, collapse = "\n") |
| 133 |
} |
| 134 |
|
| 135 |
strwrap2 <- function(x, width, indent) { |
| 136 |
cli::ansi_strwrap(x, width = max(width, 0), indent = indent, exdent = indent + 2, simplify = FALSE) |
| 137 |
} |