Skip to content

Commit

Permalink
Merge pull request #335 from r-lib/f-263-max-extra-lines
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Jul 22, 2021
2 parents 8f4a486 + c5a87f9 commit a1623aa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 75 deletions.
2 changes: 1 addition & 1 deletion R/continuation-data.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
new_continuation_shaft <- function(parent) {
extra_width <- nchar(cli::symbol$ellipsis) + 1L
extra_width <- get_extent(cli::symbol$ellipsis) + 1L

new_pillar_shaft(
list(parent, extra_width),
Expand Down
6 changes: 1 addition & 5 deletions R/ctl_colonnade.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ colonnade_compute_tiered_col_widths_2 <- function(compound_pillar, tier_widths)
min_widths <- exec(pmax, !!!unname(map(compound_pillar, get_cell_min_widths)))
min_widths <- pmin(min_widths, max_tier_width)

id <- seq_along(max_widths)

col_df <- data.frame(id, max_widths, min_widths, row.names = NULL)

ret <- colonnade_compute_tiered_col_widths_df(col_df, tier_widths, data.frame(tier = integer()))
ret <- colonnade_compute_tiered_col_widths_df(max_widths, min_widths, tier_widths)

pillars <- map(ret$id, get_sub_pillar, x = compound_pillar)
ret$pillar <- pillars
Expand Down
15 changes: 7 additions & 8 deletions R/multi.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,24 +341,23 @@ get_tier_widths <- function(width, ncol, rowid_width, tier_width = getOption("wi
colonnade_compute_tiered_col_widths <- function(pillars, tier_widths) {
max_tier_width <- max(tier_widths)

col_df <- data.frame(
id = seq_along(pillars),
max_widths = pmin(map_int(map(pillars, get_widths), max), max_tier_width),
min_widths = map_int(map(pillars, get_min_widths), max),
row.names = NULL
)
max_widths <- pmin(map_int(map(pillars, get_widths), max), max_tier_width)
min_widths <- map_int(map(pillars, get_min_widths), max)

ret <- colonnade_compute_tiered_col_widths_df(col_df, tier_widths, data.frame(tier = integer()))
ret <- colonnade_compute_tiered_col_widths_df(max_widths, min_widths, tier_widths)
ret$pillar <- pillars
ret
}

#' @rdname colonnade
#' @usage NULL
#' @aliases NULL
colonnade_compute_tiered_col_widths_df <- function(col_df, tier_widths, fixed_tier_df) {
colonnade_compute_tiered_col_widths_df <- function(max_widths, min_widths, tier_widths) {
"!!!!!DEBUG colonnade_compute_tiered_col_widths_df(`v(tier_widths)`)"

id <- seq_along(max_widths)
col_df <- data.frame(id, max_widths, min_widths, row.names = NULL)

#' @details
#' For fitting pillars in one or more tiers, first a check is made
#' if all pillars fit with their maximum width (e.g.,
Expand Down
40 changes: 35 additions & 5 deletions R/tbl-format-footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ format_extra_vars <- function(extra_cols, extra_cols_total) {
out <- c(out, cli::symbol$ellipsis)
}

out <- gsub(NBSP, "\\\\U00a0", out)
out <- gsub(" ", NBSP, out)

out[-length(out)] <- paste0(out[-length(out)], ",")
out
}
Expand All @@ -116,8 +113,41 @@ wrap_footer <- function(footer, setup) {
return(character())
}

footer_string <- pre_dots(paste(footer, collapse = " "))
split_lines(format_comment(footer_string, width = setup$width))
# When asking for width = 80, use at most 79 characters
max_extent <- setup$width - 1L

# FIXME: Make n_tiers configurable
tier_widths <- get_footer_tier_widths(footer, max_extent, n_tiers = Inf)

# show optuput even if too wide
widths <- pmin(get_extent(footer), max_extent - 4L)
wrap <- colonnade_compute_tiered_col_widths_df(widths, widths, tier_widths)

# truncate output that doesn't fit
wrap <- wrap[wrap$tier != 0, ]
split <- split(footer[wrap$id], wrap$tier)
if (nrow(wrap) < length(footer) && length(split) > 0) {
split[[length(split)]] <- c(split[[length(split)]], cli::symbol$ellipsis)
}
split <- imap(split, function(x, y) c("#", if (y == 1) cli::symbol$ellipsis else " ", x))

map_chr(split, paste, collapse = " ")
}

get_footer_tier_widths <- function(footer, max_extent, n_tiers) {
extra_width <- get_extent(cli::symbol$ellipsis) + 1L # space, ellipsis

n_tiers <- min(length(footer), n_tiers)

if (n_tiers == 1) {
max_extent - 2 - 2 * extra_width
} else {
c(
max_extent - 2 - extra_width,
rep(max_extent - 4, n_tiers - 2),
max_extent - 4 - extra_width
)
}
}

pre_dots <- function(x) {
Expand Down
56 changes: 0 additions & 56 deletions tests/testthat/_snaps/tbl-format-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@
# 3
<tbl_format_body(setup)>
<tbl_format_footer(setup)>
# ...
# with
# 3
# more
# variables:
# column_zero_zero <dbl>,
# col_01 <tbl[,2]>,
# col_04 <ord>
Code
tbl_format_setup(x, width = 5)
Output
Expand All @@ -34,14 +26,6 @@
# 3
<tbl_format_body(setup)>
<tbl_format_footer(setup)>
# ...
# with
# 3
# more
# variables:
# column_zero_zero <dbl>,
# col_01 <tbl[,2]>,
# col_04 <ord>
Code
tbl_format_setup(x, width = 6)
Output
Expand All @@ -55,14 +39,6 @@
# 3
<tbl_format_body(setup)>
<tbl_format_footer(setup)>
# ...
# with
# 3
# more
# variables:
# column_zero_zero <dbl>,
# col_01 <tbl[,2]>,
# col_04 <ord>
Code
tbl_format_setup(x, width = 7)
Output
Expand All @@ -76,14 +52,6 @@
# 3
<tbl_format_body(setup)>
<tbl_format_footer(setup)>
# ...
# with
# 3
# more
# variables:
# column_zero_zero <dbl>,
# col_01 <tbl[,2]>,
# col_04 <ord>
Code
tbl_format_setup(x, width = 8)
Output
Expand All @@ -96,14 +64,6 @@
# 3
<tbl_format_body(setup)>
<tbl_format_footer(setup)>
# ...
# with
# 3
# more
# variables:
# column_zero_zero <dbl>,
# col_01 <tbl[,2]>,
# col_04 <ord>
Code
tbl_format_setup(x, width = 9)
Output
Expand All @@ -115,14 +75,6 @@
# 3
<tbl_format_body(setup)>
<tbl_format_footer(setup)>
# ...
# with
# 3
# more
# variables:
# column_zero_zero <dbl>,
# col_01 <tbl[,2]>,
# col_04 <ord>
Code
tbl_format_setup(x, width = 10)
Output
Expand All @@ -133,14 +85,6 @@
# 3 x 3
<tbl_format_body(setup)>
<tbl_format_footer(setup)>
# ...
# with
# 3
# more
# variables:
# column_zero_zero <dbl>,
# col_01 <tbl[,2]>,
# col_04 <ord>
Code
tbl_format_setup(x, width = 11)
Output
Expand Down

0 comments on commit a1623aa

Please sign in to comment.