Debian Codegraph @

R/styles.R

raw
1 # nocov start
2 keep_empty <- function(fun) {
3 function(x) {
4 ret <- rep_along(x, "")
5 update <- which(is.na(x) | x != "")
6 ret[update] <- fun(x[update])
7 ret
8 }
9 }
10 # nocov end
11
12 #' Styling helpers
13 #'
14 #' Functions that allow implementers of formatters for custom data types to
15 #' maintain a consistent style with the default data types.
16 #'
17 #' `style_subtle()` is affected by the `subtle` [option][pillar_options].
18 #'
19 #' @param x The character vector to style.
20 #' @export
21 #' @seealso [pillar_options] for a list of options
22 #' @examples
23 #' style_subtle("text")
24 style_subtle <- keep_empty(function(x) {
25 force(x)
26 if (isTRUE(get_pillar_option_subtle())) {
27 crayon_grey_0.6(x)
28 } else {
29 x
30 }
31 })
32
33 #' @rdname style_subtle
34 #' @details
35 #' `style_subtle_num()` is affected by the
36 #' `subtle_num` [option][pillar_options],
37 #' which is `FALSE` by default.
38 #'
39 #' @export
40 #' @examples
41 #' style_subtle_num(0.01 * 1:3, c(TRUE, FALSE, TRUE))
42 style_subtle_num <- function(x, negative) {
43 if (isTRUE(get_pillar_option_subtle_num())) {
44 style_subtle(x)
45 } else {
46 ifelse(negative, style_neg(x), x)
47 }
48 }
49
50 style_hint <- keep_empty(function(x) {
51 force(x)
52 if (isTRUE(get_pillar_option_subtle())) {
53 crayon_grey_0.8(x)
54 } else {
55 x
56 }
57 })
58
59 style_spark_na <- function(x) {
60 crayon_yellow(x)
61 }
62
63 #' @details
64 #' `style_bold()` is affected by the `bold` [option][pillar_options],
65 #' which is `FALSE` by default.
66 #'
67 #' @rdname style_subtle
68 #' @export
69 #' @examples
70 #' style_bold("Petal.Width")
71 style_bold <- keep_empty(function(x) {
72 if (isTRUE(get_pillar_option_bold())) {
73 crayon_bold(x)
74 } else {
75 x
76 }
77 })
78
79 #' @rdname style_subtle
80 #' @export
81 #' @examples
82 #' style_na("NA")
83 style_na <- function(x) {
84 crayon_red(x)
85 }
86
87 style_na_if <- function(x, p) {
88 idx <- which(p)
89 if (length(idx) == 0) {
90 return(x)
91 }
92 x[p] <- style_na(x[idx[[1]]])
93 x
94 }
95
96 #' @details
97 #' `style_neg()` is affected by the `pillar.neg` [option][pillar_options].
98 #'
99 #' @rdname style_subtle
100 #' @export
101 #' @examples
102 #' style_neg("123")
103 style_neg <- keep_empty(function(x) {
104 if (isTRUE(get_pillar_option_neg())) {
105 crayon_red(x)
106 } else {
107 x
108 }
109 })
110
111 pillar_na <- function(use_brackets_if_no_color = FALSE) {
112 if (use_brackets_if_no_color && !has_color()) {
113 "<NA>"
114 } else {
115 style_na("NA")
116 }
117 }
118
119 style_list <- function(x) {
120 style_subtle(x)
121 }
122
123 # Only check if we have color support once per session
124 num_colors <- local({
125 num_colors <- NULL
126 function(forget = FALSE) {
127 if (is.null(num_colors) || forget) {
128 num_colors <<- cli::num_ansi_colors()
129 }
130 num_colors
131 }
132 })
133
134 has_color <- function() {
135 num_colors() > 1
136 }
137
138 # nocov start
139 # Crayon functions call cli::num_ansi_colors() every call
140 make_style_fast <- function(...) {
141 # Force has_color to be true when making styles
142 # Use prefix to avoid problems with empty NAMESPACE
143 rlang::local_options(cli.num_colors = 16L)
144
145 style_16 <- attr(cli::make_ansi_style(..., colors = 16), "_styles")[[1]]
146 open_16 <- style_16$open
147 close_16 <- style_16$close
148
149 style_256 <- attr(cli::make_ansi_style(..., colors = 256), "_styles")[[1]]
150 open_256 <- style_256$open
151 close_256 <- style_256$close
152
153 function(...) {
154 colors <- num_colors()
155 if (colors <= 1) {
156 paste0(...)
157 } else if (colors >= 256) {
158 paste0(open_256, ..., close_256)
159 } else {
160 paste0(open_16, ..., close_16)
161 }
162 }
163 }
164
165 # Placeholders, assigned in .onLoad()
166 crayon_underline <- function(...) {}
167 crayon_italic <- function(...) {}
168 crayon_red <- function(...) {}
169 crayon_yellow <- function(...) {}
170 crayon_bold <- function(...) {}
171 crayon_grey_0.6 <- function(...) {}
172 crayon_grey_0.8 <- function(...) {}
173
174 assign_crayon_styles <- function() {
175 crayon_underline <<- make_style_fast("underline")
176 crayon_italic <<- make_style_fast("italic")
177 crayon_red <<- make_style_fast("red")
178 crayon_yellow <<- make_style_fast("yellow")
179 crayon_bold <<- make_style_fast("bold")
180 crayon_grey_0.6 <<- make_style_fast(grDevices::grey(0.6), grey = TRUE)
181 crayon_grey_0.8 <<- make_style_fast(grDevices::grey(0.8), grey = TRUE)
182 }
183 # nocov end