-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathstyles.R
183 lines (165 loc) · 4 KB
/
styles.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# nocov start
keep_empty <- function(fun) {
function(x) {
ret <- rep_along(x, "")
update <- which(is.na(x) | x != "")
ret[update] <- fun(x[update])
ret
}
}
# nocov end
#' Styling helpers
#'
#' Functions that allow implementers of formatters for custom data types to
#' maintain a consistent style with the default data types.
#'
#' `style_subtle()` is affected by the `subtle` [option][pillar_options].
#'
#' @param x The character vector to style.
#' @export
#' @seealso [pillar_options] for a list of options
#' @examples
#' style_subtle("text")
style_subtle <- keep_empty(function(x) {
force(x)
if (isTRUE(get_pillar_option_subtle())) {
crayon_grey_0.6(x)
} else {
x
}
})
#' @rdname style_subtle
#' @details
#' `style_subtle_num()` is affected by the
#' `subtle_num` [option][pillar_options],
#' which is `FALSE` by default.
#'
#' @export
#' @examples
#' style_subtle_num(0.01 * 1:3, c(TRUE, FALSE, TRUE))
style_subtle_num <- function(x, negative) {
if (isTRUE(get_pillar_option_subtle_num())) {
style_subtle(x)
} else {
ifelse(negative, style_neg(x), x)
}
}
style_hint <- keep_empty(function(x) {
force(x)
if (isTRUE(get_pillar_option_subtle())) {
crayon_grey_0.8(x)
} else {
x
}
})
style_spark_na <- function(x) {
crayon_yellow(x)
}
#' @details
#' `style_bold()` is affected by the `bold` [option][pillar_options],
#' which is `FALSE` by default.
#'
#' @rdname style_subtle
#' @export
#' @examples
#' style_bold("Petal.Width")
style_bold <- keep_empty(function(x) {
if (isTRUE(get_pillar_option_bold())) {
crayon_bold(x)
} else {
x
}
})
#' @rdname style_subtle
#' @export
#' @examples
#' style_na("NA")
style_na <- function(x) {
crayon_red(x)
}
style_na_if <- function(x, p) {
idx <- which(p)
if (length(idx) == 0) {
return(x)
}
x[p] <- style_na(x[idx[[1]]])
x
}
#' @details
#' `style_neg()` is affected by the `pillar.neg` [option][pillar_options].
#'
#' @rdname style_subtle
#' @export
#' @examples
#' style_neg("123")
style_neg <- keep_empty(function(x) {
if (isTRUE(get_pillar_option_neg())) {
crayon_red(x)
} else {
x
}
})
pillar_na <- function(use_brackets_if_no_color = FALSE) {
if (use_brackets_if_no_color && !has_color()) {
"<NA>"
} else {
style_na("NA")
}
}
style_list <- function(x) {
style_subtle(x)
}
# Only check if we have color support once per session
num_colors <- local({
num_colors <- NULL
function(forget = FALSE) {
if (is.null(num_colors) || forget) {
num_colors <<- cli::num_ansi_colors()
}
num_colors
}
})
has_color <- function() {
num_colors() > 1
}
# nocov start
# Crayon functions call cli::num_ansi_colors() every call
make_style_fast <- function(...) {
# Force has_color to be true when making styles
# Use prefix to avoid problems with empty NAMESPACE
rlang::local_options(cli.num_colors = 16L)
style_16 <- attr(cli::make_ansi_style(..., colors = 16), "_styles")[[1]]
open_16 <- style_16$open
close_16 <- style_16$close
style_256 <- attr(cli::make_ansi_style(..., colors = 256), "_styles")[[1]]
open_256 <- style_256$open
close_256 <- style_256$close
function(...) {
colors <- num_colors()
if (colors <= 1) {
paste0(...)
} else if (colors >= 256) {
paste0(open_256, ..., close_256)
} else {
paste0(open_16, ..., close_16)
}
}
}
# Placeholders, assigned in .onLoad()
crayon_underline <- function(...) {}
crayon_italic <- function(...) {}
crayon_red <- function(...) {}
crayon_yellow <- function(...) {}
crayon_bold <- function(...) {}
crayon_grey_0.6 <- function(...) {}
crayon_grey_0.8 <- function(...) {}
assign_crayon_styles <- function() {
crayon_underline <<- make_style_fast("underline")
crayon_italic <<- make_style_fast("italic")
crayon_red <<- make_style_fast("red")
crayon_yellow <<- make_style_fast("yellow")
crayon_bold <<- make_style_fast("bold")
crayon_grey_0.6 <<- make_style_fast(grDevices::grey(0.6), grey = TRUE)
crayon_grey_0.8 <<- make_style_fast(grDevices::grey(0.8), grey = TRUE)
}
# nocov end