-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathshaft-simple.R
88 lines (80 loc) · 2.82 KB
/
shaft-simple.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#' @description
#' `new_pillar_shaft_simple()` provides an implementation of the `pillar_shaft`
#' class suitable for output that has a fixed formatting, which will be
#' truncated with a continuation character (ellipsis or `~`) if it doesn't fit
#' the available width.
#' By default, the required width is computed from the natural width of the
#' `formatted` argument.
#'
#' @details
#' The `formatted` argument may also contain ANSI escapes to change color
#' or other attributes of the text, provided e.g. by the \pkg{cli} package.
#'
#' @inheritParams tibble::char
#' @param ... Passed on to [new_pillar_shaft()].
#' @param formatted The data to show, an object coercible to [character].
#' @param align Alignment of the column.
#' @param na String to use as `NA` value, defaults to `"NA"` styled with
#' [style_na()] with fallback if color is not available.
#' @param na_indent Indentation of `NA` values.
#' @param short_formatted If provided, a character vector of the same length as
#' `formatted`, to be used when the available width is insufficient to show
#' the full output.
#' @export
#' @rdname new_pillar_shaft
new_pillar_shaft_simple <- function(formatted, ..., width = NULL, align = "left",
min_width = NULL, na = NULL, na_indent = 0L,
shorten = c("back", "front", "mid", "abbreviate"),
short_formatted = NULL) {
formatted <- as.character(formatted)
if (is.null(width)) {
width <- get_max_extent(formatted)
}
if (is.null(na)) {
na <- pillar_na()
}
if (missing(shorten)) {
shorten <- NULL
} else if (!is.null(shorten)) {
shorten <- arg_match(shorten)
}
if (!is.null(short_formatted)) {
# stopifnot(get_extent(short_formatted) <= get_extent(formatted))
if (is.null(min_width)) {
min_width <- get_max_extent(short_formatted)
}
}
new_pillar_shaft(
list(formatted),
...,
width = width,
min_width = min_width,
align = align,
na = na,
na_indent = na_indent,
shorten = shorten,
short_formatted = short_formatted,
class = "pillar_shaft_simple"
)
}
#' @export
format.pillar_shaft_simple <- function(x, width, ...) {
align <- attr(x, "align", exact = TRUE)
shorten <- attr(x, "shorten", exact = TRUE)
short_formatted <- attr(x, "short_formatted", exact = TRUE)
desired_width <- get_width(x)
shaft <- as.character(x[[1]])
if (width < desired_width) {
if (is.null(short_formatted)) {
shaft <- str_trunc(shaft, width, shorten)
} else {
short_idx <- !is.na(shaft) & (get_extent(shaft) > width)
shaft[short_idx] <- short_formatted[short_idx]
}
}
shaft[is.na(shaft)] <- paste0(
strrep(" ", attr(x, "na_indent", exact = TRUE)),
attr(x, "na", exact = TRUE)
)
new_ornament(shaft, width = width, align = align)
}