/
print-str.R
165 lines (136 loc) · 3.36 KB
/
print-str.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
158
159
160
161
162
163
164
165
# print -------------------------------------------------------------------
#' `print()` and `str()` generics.
#'
#' These are constructed to be more easily extensible since you can override
#' the `_header()`, `_data()` or `_footer()` components individually. The
#' default methods are built on top of `format()`.
#'
#' @param x A vector
#' @param ... Additional arguments passed on to methods. See [print()] and
#' [str()] for commonly used options
#' @keywords internal
#' @export
obj_print <- function(x, ...) {
obj_print_header(x, ...)
obj_print_data(x, ...)
obj_print_footer(x, ...)
invisible(x)
}
#' @export
#' @rdname obj_print
obj_print_header <- function(x, ...) {
UseMethod("obj_print_header")
}
#' @export
obj_print_header.default <- function(x, ...) {
cat_line("<", vec_ptype_full(x), "[", vec_size(x), "]>")
invisible(x)
}
#' @export
#' @rdname obj_print
obj_print_data <- function(x, ...) {
UseMethod("obj_print_data")
}
#' @export
obj_print_data.default <- function(x, ...) {
if (length(x) == 0)
return(invisible(x))
out <- stats::setNames(format(x), names(x))
print(out, quote = FALSE)
invisible(x)
}
#' @export
#' @rdname obj_print
obj_print_footer <- function(x, ...) {
UseMethod("obj_print_footer")
}
#' @export
obj_print_footer.default <- function(x, ...) {
invisible(x)
}
# str ---------------------------------------------------------------------
#' @export
#' @rdname obj_print
obj_str <- function(x, ...) {
obj_str_header(x, ...)
obj_str_data(x, ...)
obj_str_footer(x, ...)
}
#' @export
#' @rdname obj_print
obj_str_header <- function(x, ...) {
UseMethod("obj_str_header")
}
#' @export
obj_str_header.default <- function(x, ...) {
invisible(x)
}
#' @export
#' @rdname obj_print
obj_str_data <- function(x, ...) {
UseMethod("obj_str_data")
}
#' @export
obj_str_data.default <- function(x, ...) {
if (is.list(x)) {
obj_str_recursive(x, ...)
} else {
obj_str_leaf(x, ...)
}
}
obj_str_recursive <- function(x, ...,
indent.str = "",
nest.lev = 0) {
if (nest.lev != 0L)
cat(" ")
cat_line(glue::glue("{vec_ptype_abbr(x)} [1:{vec_size(x)}] "))
utils::str(
vec_data(x),
no.list = TRUE,
...,
nest.lev = nest.lev + 1L,
indent.str = indent.str
)
}
obj_str_leaf <- function(x, ...,
indent.str = "",
width = getOption("width")) {
width <- width - nchar(indent.str) - 2
# Avoid spending too much time formatting elements that won't see
length <- ceiling(width / 2)
if (length(x) > length) {
out <- x[seq2(1, length)]
} else {
out <- x
}
title <- glue::glue(" {vec_ptype_abbr(x)} [1:{length(x)}] ")
cat_line(inline_list(title, format(out), width = width))
invisible(x)
}
#' @export
#' @rdname obj_print
obj_str_footer <- function(x, ...) {
UseMethod("obj_str_footer")
}
#' @export
obj_str_footer.default <- function(x, ...,
indent.str = "",
nest.lev = 0) {
attr <- attributes(x)
attr[["class"]] <- NULL
attr[["names"]] <- NULL
if (length(attr) == 0)
return(invisible(x))
if (!is.list(x)) {
indent.str <- paste0(" ", indent.str)
}
utils::str(
attr,
no.list = TRUE,
...,
comp.str = "@ ",
nest.lev = nest.lev + 1L,
indent.str = indent.str
)
invisible(x)
}