diff --git a/NAMESPACE b/NAMESPACE index 3fc053b06..5373150e1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,7 +9,6 @@ S3method(format,pillar_1e) S3method(format,pillar_capital) S3method(format,pillar_colonnade) S3method(format,pillar_continuation_shaft) -S3method(format,pillar_empty_shaft) S3method(format,pillar_ornament) S3method(format,pillar_rif_shaft) S3method(format,pillar_rif_title) diff --git a/R/ctl_colonnade.R b/R/ctl_colonnade.R index a8c50d35a..48af2b40a 100644 --- a/R/ctl_colonnade.R +++ b/R/ctl_colonnade.R @@ -53,20 +53,30 @@ ctl_colonnade <- function(x, has_row_id = TRUE, width = NULL, controller = new_t }) } - out <- map(col_widths_tiers, function(tier) { + flat_tiers <- map(col_widths_tiers, function(tier) { map2(tier$pillar, tier$width, pillar_format_parts_2) }) + out <- map(flat_tiers, format_colonnade_tier_2) + extra_cols <- x[seq2(length(pillars) + 1L, nc)] new_colonnade_body(out, extra_cols = extra_cols) } +format_colonnade_tier_2 <- function(x) { + "!!!!!DEBUG format_colonnade_tier_2(`v(x)`)" + + if (length(x) == 0) { + return(character()) + } + + exec(paste, !!!x) +} + new_colonnade_body <- function(x, extra_cols) { "!!!!!DEBUG new_colonnade_body()" - formatted_tiers <- map(x, format_colonnade_tier) - formatted <- new_vertical(as.character(unlist(formatted_tiers))) - + formatted <- new_vertical(as.character(unlist(x))) new_vertical(formatted, extra_cols = extra_cols) } diff --git a/R/ctl_compound.R b/R/ctl_compound.R index 9fe805fcf..b5b2ca0ee 100644 --- a/R/ctl_compound.R +++ b/R/ctl_compound.R @@ -6,11 +6,11 @@ new_data_frame_pillar <- function(x, controller, width, title) { new_packed_pillars <- function(x, controller, width, title) { "!!!!!DEBUG new_packed_pillars(`v(width)`, `v(title)`)" - if (length(x) == 0) { + if (ncol(x) == 0) { return(compact(list(pillar_from_shaft( new_pillar_title(prepare_title(title)), new_pillar_type(x), - new_empty_shaft(), + new_empty_shaft(nrow(x)), width )))) } @@ -63,7 +63,7 @@ new_matrix_pillar <- function(x, controller, width, title) { return(pillar_from_shaft( new_pillar_title(prepare_title(title)), new_pillar_type(x), - new_empty_shaft(), + new_empty_shaft(nrow(x)), width )) } diff --git a/R/ctl_new_pillar.R b/R/ctl_new_pillar.R index 2c54f5058..ca08abe33 100644 --- a/R/ctl_new_pillar.R +++ b/R/ctl_new_pillar.R @@ -19,6 +19,9 @@ #' for the titles and types of the sub-pillar. #' Users will only rarely need to override this method if ever. #' +#' All components must be of the same height. +#' This restriction may be levied in the future. +#' #' Implementations should return `NULL` if none of the data #' fits the available width. #' diff --git a/R/ctl_pillar.R b/R/ctl_pillar.R index ede22da1f..db2bf03fc 100644 --- a/R/ctl_pillar.R +++ b/R/ctl_pillar.R @@ -172,9 +172,7 @@ format.pillar <- function(x, width = NULL, ...) { width <- sum(widths) - length(widths) + 1L } - out <- pillar_format_parts_2(x, width) - - new_vertical(unlist(unname(out))) + new_vertical(pillar_format_parts_2(x, width)) } #' @export diff --git a/R/ctl_pillar_component.R b/R/ctl_pillar_component.R index 3303684d6..103a3e305 100644 --- a/R/ctl_pillar_component.R +++ b/R/ctl_pillar_component.R @@ -106,5 +106,5 @@ pillar_format_parts_2 <- function(x, width) { alignment <- attr(formatted[["data"]], "align") %||% "left" - map(formatted, align, width = width, align = alignment) + align(unlist(formatted), width = width, align = alignment) } diff --git a/R/empty-data.R b/R/empty-data.R index 9fd8722b4..18e090e98 100644 --- a/R/empty-data.R +++ b/R/empty-data.R @@ -2,14 +2,6 @@ style_empty <- function(n) { "" } -new_empty_shaft <- function() { - new_pillar_shaft(list(), class = "pillar_empty_shaft", width = 0) -} - -#' @export -format.pillar_empty_shaft <- function(x, width, ...) { - new_ornament( - style_empty(), - width = width - ) +new_empty_shaft <- function(height = 1) { + new_pillar_shaft_simple(rep("", height), width = 0) } diff --git a/TODO.md b/TODO.md index 8fdfce5af..e8036afbd 100644 --- a/TODO.md +++ b/TODO.md @@ -2,11 +2,6 @@ ## Next steps -- Reduce calls to `align()`: - - align all components vertically - - squash - - only then `align()` - - paste - Benchmark and profile again - `bench.R` - https://github.com/tidyverse/tibble/issues/598 diff --git a/man/ctl_new_pillar.Rd b/man/ctl_new_pillar.Rd index 8ba25bdb0..40795e5e7 100644 --- a/man/ctl_new_pillar.Rd +++ b/man/ctl_new_pillar.Rd @@ -42,6 +42,9 @@ The default implementation returns a compound pillar with suitable formatting for the titles and types of the sub-pillar. Users will only rarely need to override this method if ever. +All components must be of the same height. +This restriction may be levied in the future. + Implementations should return \code{NULL} if none of the data fits the available width. } diff --git a/tests/testthat/_snaps/ctl_colonnade.md b/tests/testthat/_snaps/ctl_colonnade.md index f7f367e69..70b9b4f73 100644 --- a/tests/testthat/_snaps/ctl_colonnade.md +++ b/tests/testthat/_snaps/ctl_colonnade.md @@ -176,7 +176,7 @@ 3 NA Code ctl_colonnade(list(` - ` = c("\n", "\""), `` = factor("\n")), width = 30) + ` = c("\n", "\""), `` = factor(c("\n", "\n"))), width = 30) Output `\n` `\r` @@ -224,6 +224,114 @@ Code # dummy +# color, options: UTF-8 is TRUE + + Code + style_na("NA") + Output + [1] "\033[31mNA\033[39m" + Code + style_neg("-1") + Output + [1] "\033[31m-1\033[39m" + +--- + + Code + xf <- (function() ctl_colonnade(list(x = c((10^(-3:4)) * c(-1, 1), NA)))) + print(xf()) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.subtle_num = TRUE, print(xf())) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.subtle = FALSE, print(xf())) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.neg = FALSE, print(xf())) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.subtle = FALSE, pillar.neg = FALSE, print(xf())) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.bold = TRUE, print(xf())) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + +--- + + Code + ctl_colonnade(list(a_very_long_column_name = 0), width = 20) + Output + a_very_long_colum… +  + 1 0 + # color, options: UTF-8 is FALSE Code @@ -359,11 +467,11 @@ Code ctl_colonnade(x, width = 40) Output - a b$c $d $e - - 1 1 4 7 - 2 2 5 8 - 3 3 6 9 + a b$c $d $e c + + 1 1 4 7 10 + 2 2 5 8 11 + 3 3 6 9 12 # matrix columns (unnamed) @@ -390,11 +498,11 @@ # matrix columns (empty) Code - ctl_colonnade(x, width = 30) + ctl_colonnade(list(a = 1:3, b = matrix(4:6, ncol = 1)[, 0], c = 4:6), width = 30) Output - a b - - 1 1 - 2 2 - 3 3 + a b c + + 1 1 4 + 2 2 5 + 3 3 6 diff --git a/tests/testthat/_snaps/format_multi.md b/tests/testthat/_snaps/format_multi.md index 5c1e960c7..88395bdf0 100644 --- a/tests/testthat/_snaps/format_multi.md +++ b/tests/testthat/_snaps/format_multi.md @@ -544,6 +544,136 @@ Code # dummy +# color, options: UTF-8 is TRUE + + Code + crayon::has_color() + Output + [1] TRUE + Code + crayon::num_colors() + Output + [1] 16 + Code + has_color() + Output + [1] TRUE + Code + num_colors() + Output + [1] 16 + Code + style_na("NA") + Output + [1] "\033[31mNA\033[39m" + Code + style_neg("-1") + Output + [1] "\033[31m-1\033[39m" + +--- + + Code + style_na("NA") + Output + [1] "\033[31mNA\033[39m" + +--- + + Code + print(xf) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.subtle_num = TRUE, print(xf)) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.subtle = FALSE, print(xf)) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.neg = FALSE, print(xf)) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.subtle = FALSE, pillar.neg = FALSE, print(xf)) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + Code + with_options(pillar.bold = TRUE, print(xf)) + Output + x +  + 1 -0.001 + 2 0.01 + 3 -0.1 + 4 1 + 5 -10 + 6 100 + 7 -1000 + 8 10000 + 9 NA + +--- + + Code + colonnade(list(a_very_long_column_name = 0), width = 15) + Output + a_very_long_… +  + 1 0 + # color, options: UTF-8 is FALSE Code @@ -724,11 +854,11 @@ Code colonnade(x, width = 40) Output - a b$c $d $e - - 1 1 4 7 - 2 2 5 8 - 3 3 6 9 + a b$c $d $e $f + + 1 1 4 7 10 + 2 2 5 8 11 + 3 3 6 9 12 # matrix columns (unnamed) @@ -757,9 +887,9 @@ Code colonnade(x, width = 30) Output - a b - - 1 1 - 2 2 - 3 3 + a b c + + 1 1 4 + 2 2 5 + 3 3 6 diff --git a/tests/testthat/test-ctl_colonnade.R b/tests/testthat/test-ctl_colonnade.R index 0a598fc7b..7e1b554d8 100644 --- a/tests/testthat/test-ctl_colonnade.R +++ b/tests/testthat/test-ctl_colonnade.R @@ -25,7 +25,7 @@ test_that("tests from tibble", { ctl_colonnade(df_all, width = 300) options(width = 20) ctl_colonnade(df_all, width = 300) - ctl_colonnade(list(`\n` = c("\n", '"'), `\r` = factor("\n")), width = 30) + ctl_colonnade(list(`\n` = c("\n", '"'), `\r` = factor(c("\n", "\n"))), width = 30) ctl_colonnade(list(a = c("", " ", "a ", " a")), width = 30) ctl_colonnade(list("mean(x)" = 5, "var(x)" = 3), width = 30) }) @@ -131,7 +131,8 @@ test_that("tibble columns (empty)", { b = vctrs::data_frame( c = 4:6, d = 7:9, e = vctrs::data_frame(f = 10:12)[, 0] - ) + ), + c = 10:12 ) expect_snapshot({ ctl_colonnade(x, width = 40) @@ -153,8 +154,10 @@ test_that("matrix columns (named)", { }) test_that("matrix columns (empty)", { - x <- list(a = 1:3, b = matrix(4:6, ncol = 1)[, 0]) expect_snapshot({ - ctl_colonnade(x, width = 30) + ctl_colonnade( + list(a = 1:3, b = matrix(4:6, ncol = 1)[, 0], c = 4:6), + width = 30 + ) }) }) diff --git a/tests/testthat/test-format_multi.R b/tests/testthat/test-format_multi.R index 5f8c186af..22430c0bf 100644 --- a/tests/testthat/test-format_multi.R +++ b/tests/testthat/test-format_multi.R @@ -242,7 +242,8 @@ test_that("tibble columns (empty)", { b = structure( list( c = 4:6, d = 7:9, - e = data.frame(f = 10:12)[, 0] + e = data.frame(f = 10:12)[, 0], + f = 10:12 ), class = "data.frame" ) @@ -267,7 +268,7 @@ test_that("matrix columns (named)", { }) test_that("matrix columns (empty)", { - x <- list(a = 1:3, b = matrix(4:6, ncol = 1)[, 0]) + x <- list(a = 1:3, b = matrix(4:6, ncol = 1)[, 0], c = 4:6) expect_snapshot({ colonnade(x, width = 30) })