Debian Codegraph @

R/title.R

raw
1 style_title <- style_bold
2
3 #' Prepare a column title for formatting
4 #'
5 #' Call [format()] on the result to render column titles.
6 #'
7 #' @param x A character vector of column titles.
8 #' @inheritParams rlang::args_dots_empty
9 #' @export
10 #' @examples
11 #' format(new_pillar_title(names(trees)))
12 new_pillar_title <- function(x, ...) {
13 "!!!!DEBUG new_pillar_title(`v(x)`)"
14 if (!missing(...)) {
15 check_dots_empty(action = warn)
16 }
17
18 if (is.null(x)) {
19 width <- 0L
20 } else if (all(is.na(x) | x == "")) {
21 width <- 0L
22 x <- NULL
23 } else {
24 width <- get_max_extent(format_title(x, width = Inf))
25 stopifnot(!is.na(width))
26 }
27
28 ret <- structure(list(x), class = "pillar_title")
29
30 ret <- set_width(ret, width)
31 ret <- set_min_width(ret, min(width, get_min_title_width(width)))
32 ret
33 }
34
35 get_min_title_width <- function(width) {
36 title_chars <- get_pillar_option_min_title_chars()
37 if (!is.numeric(title_chars) || length(title_chars) != 1 || title_chars < 0) {
38 stop("Option pillar.min_title_chars must be a nonnegative number", call. = FALSE)
39 }
40
41 if (is.infinite(title_chars)) {
42 return(width)
43 }
44
45 # We don't use the ellipsis if we don't truncate, a solution with min()
46 # is difficult to make work in all corner cases (and slower too)
47 if (width <= title_chars) {
48 return(width)
49 }
50 title_chars + 2L
51 }
52
53 #' @export
54 format.pillar_title <- function(x, width = NULL, ..., footnote_idx = 0L) {
55 title <- x[[1]]
56 if (is.null(title)) {
57 return(character())
58 }
59
60 if (is.null(width)) {
61 width <- get_width(x)
62 }
63
64 footnote <- (footnote_idx > 0L)
65 title <- format_title(title, width, footnote)
66 if (footnote) {
67 title <- gsub("\u02df$", superdigit(footnote_idx), title)
68 }
69 style_title(title)
70 }