-
Notifications
You must be signed in to change notification settings - Fork 38
/
char.R
157 lines (131 loc) · 3.33 KB
/
char.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#' Format a character vector in a tibble
#'
#' These functions are reexported as [tibble::char()] and [tibble::set_char_opts()].
#'
#' @keywords internal
#' @export
char <- function(x, ..., min_chars = NULL,
shorten = c("back", "front", "mid", "abbreviate")) {
stopifnot(is.character(x))
check_dots_empty()
if (missing(shorten)) {
shorten <- NULL
}
out <- set_char_opts(
x,
min_chars = min_chars,
shorten = shorten
)
new_class <- c("pillar_char", "pillar_vctr", "vctrs_vctr", "character")
class(out) <- new_class
out
}
#' @export
vec_ptype_full.pillar_char <- function(x, ...) {
# No longer inherited in vctrs > 0.4.1,
# https://github.com/r-lib/vctrs/pull/1571
format(attr(x, "pillar", exact = TRUE))
}
#' @export
vec_ptype_abbr.pillar_char <- function(x, ...) {
pillar_attr <- attr(x, "pillar", exact = TRUE)
out <- "char"
shorten <- pillar_attr$shorten
if (!is.null(shorten) && shorten == "abbr") {
out <- "abbr"
}
out
}
#' @export
format.pillar_char <- function(x, trim = FALSE, ...) {
"!!!!DEBUG format.pillar_char()"
shaft <- pillar_shaft(x)
out <- format(shaft, width = get_width(shaft))
if (trim) {
attributes(out) <- NULL
} else {
out <- format(out, align = "left")
}
out
}
#' @export
#' @rdname char
set_char_opts <- function(x, ..., min_chars = NULL,
shorten = c("back", "front", "mid", "abbreviate")) {
check_dots_empty()
if (missing(shorten)) {
shorten <- NULL
} else if (!is.null(shorten)) {
shorten <- arg_match(shorten)
}
pillar_attr <- structure(
list(
min_chars = min_chars,
shorten = shorten
),
class = c("pillar_char_attr", "pillar_vctr_attr", "tibble_vec_attr")
)
attr(x, "pillar") <- pillar_attr
x
}
#' @export
format.pillar_char_attr <- function(x, ...) {
out <- "pillar_char"
min_chars <- x$min_chars
if (!is.null(min_chars)) {
out <- paste0(out, "(", min_chars, ")")
}
shorten <- x$shorten
if (!is.null(shorten)) {
extra <- switch(shorten,
back = ">",
front = "<",
mid = "|",
never = "=",
abbr = "&"
)
out <- paste0(out, extra)
}
out
}
#' @export
vec_ptype2.pillar_char.pillar_char <- function(x, y, ...) {
x
}
#' @export
vec_ptype2.pillar_char.character <- function(x, y, ...) {
x
}
#' @export
vec_ptype2.character.pillar_char <- function(x, y, ...) {
y
}
#' @export
vec_cast.pillar_char.pillar_char <- function(x, to, ...) {
pillar_x <- attr(x, "pillar", exact = TRUE)
pillar_to <- attr(to, "pillar", exact = TRUE)
pillar_x_shorten <- pillar_x$shorten
pillar_to_shorten <- pillar_to$shorten
if (!is.null(pillar_x_shorten) && !is.null(pillar_to_shorten)) {
if (!identical(pillar_x$shorten, pillar_to$shorten)) {
abort("Only `pillar_char` objects with the same shortening setting can be combined.")
}
}
pillar_x_min_chars <- pillar_x$min_chars
pillar_to_min_chars <- pillar_to$min_chars
if (!is.null(pillar_x_min_chars) && !is.null(pillar_to_min_chars)) {
pillar_to$min_chars <- max(pillar_x_min_chars, pillar_to_min_chars)
}
attr(x, "pillar") <- pillar_to
x
}
#' @export
vec_cast.character.pillar_char <- function(x, to, ...) {
vec_data(x)
}
#' @export
vec_cast.pillar_char.character <- function(x, to, ...) {
out <- char(x)
attr(out, "pillar") <- attr(to, "pillar")
out
}