diff --git a/R/type-sum.R b/R/type-sum.R index d1fa09609..301113de2 100644 --- a/R/type-sum.R +++ b/R/type-sum.R @@ -69,10 +69,13 @@ vec_ptype_abbr.pillar_empty_col <- function(x, ...) { #' As of pillar v1.6.1, the default method forwards to [vctrs::vec_ptype_abbr()] #' for vectors and to [type_sum()] for other objects. #' Previous versions always forwarded to [type_sum()]. +#' An attribute named `"short"` in the return value will be picked up by +#' the [pillar_shaft()] method for lists, and used if space is limited. #' #' @examples #' obj_sum(1:10) #' obj_sum(matrix(1:10)) +#' obj_sum(data.frame(a = 1)) #' obj_sum(Sys.Date()) #' obj_sum(Sys.time()) #' obj_sum(mean) @@ -87,7 +90,16 @@ obj_sum.default <- function(x) { if (!vec_is(x)) { type_sum(x) } else { - paste(vec_ptype_abbr(x, suffix_shape = FALSE), size_sum(x)) + abbr <- vec_ptype_abbr(x, suffix_shape = FALSE) + + out <- paste(abbr, size_sum(x)) + if (is.array(x)) { + short <- paste0(abbr, "[", cli::symbol$ellipsis, "]") + } else { + short <- abbr + } + + structure(out, short = short) } } diff --git a/man/type_sum.Rd b/man/type_sum.Rd index 02ea368fd..78d2eac5d 100644 --- a/man/type_sum.Rd +++ b/man/type_sum.Rd @@ -27,6 +27,8 @@ It should always return a string (a character vector of length one). As of pillar v1.6.1, the default method forwards to \code{\link[vctrs:vec_ptype_full]{vctrs::vec_ptype_abbr()}} for vectors and to \code{\link[=type_sum]{type_sum()}} for other objects. Previous versions always forwarded to \code{\link[=type_sum]{type_sum()}}. +An attribute named \code{"short"} in the return value will be picked up by +the \code{\link[=pillar_shaft]{pillar_shaft()}} method for lists, and used if space is limited. \code{size_sum()} is called by \code{obj_sum()} to format the size of the object. It should always return a string (a character vector of length one), @@ -42,6 +44,7 @@ to avoid confusion. \examples{ obj_sum(1:10) obj_sum(matrix(1:10)) +obj_sum(data.frame(a = 1)) obj_sum(Sys.Date()) obj_sum(Sys.time()) obj_sum(mean) diff --git a/tests/testthat/test-obj-sum.R b/tests/testthat/test-obj-sum.R index f34223de1..278639a54 100644 --- a/tests/testthat/test-obj-sum.R +++ b/tests/testthat/test-obj-sum.R @@ -19,6 +19,8 @@ test_that("data frames and common data vectors have size summary", { expect_obj_sum_is_ptype <- function(x) { obj_sum <- obj_sum(x) + attr(obj_sum, "short") <- NULL + ptype <- vec_ptype_abbr(x, suffix_shape = FALSE) expect_equal(obj_sum, !!paste_with_space_if_needed(ptype, size_sum(x))) @@ -32,9 +34,35 @@ test_that("data frames and common data vectors have size summary", { expect_obj_sum_is_ptype(Sys.time() - Sys.time() + 1:3) expect_obj_sum_is_ptype(as.POSIXlt(Sys.time() + 1:3)) expect_obj_sum_is_ptype(list(1, 2:3)) + expect_obj_sum_is_ptype(matrix(1:10, ncol = 2)) + expect_obj_sum_is_ptype(Titanic) expect_obj_sum_is_ptype(as_override_size_sum(1:3)) }) +test_that("object summary has short version", { + local_override_size_sum() + + expect_obj_sum_short_is_ptype <- function(x, suffix = "") { + obj_sum <- attr(obj_sum(x), "short") + + ptype <- vec_ptype_abbr(x, suffix_shape = FALSE) + + expect_equal(obj_sum, !!paste0(ptype, suffix)) + } + + expect_obj_sum_short_is_ptype(mtcars) + expect_obj_sum_short_is_ptype(factor(1:3)) + expect_obj_sum_short_is_ptype(ordered(1:3)) + expect_obj_sum_short_is_ptype(Sys.Date() + 1:3) + expect_obj_sum_short_is_ptype(Sys.time() + 1:3) + expect_obj_sum_short_is_ptype(Sys.time() - Sys.time() + 1:3) + expect_obj_sum_short_is_ptype(as.POSIXlt(Sys.time() + 1:3)) + expect_obj_sum_short_is_ptype(list(1, 2:3)) + expect_obj_sum_short_is_ptype(matrix(1:10, ncol = 2), paste0("[", cli::symbol$ellipsis, "]")) + expect_obj_sum_short_is_ptype(Titanic, paste0("[", cli::symbol$ellipsis, "]")) + expect_obj_sum_short_is_ptype(as_override_size_sum(1:3)) +}) + # type_sum ----------------------------------------------------------------