File: styles.R

package info (click to toggle)
r-cran-pillar 1.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,476 kB
  • sloc: sh: 13; makefile: 2
file content (194 lines) | stat: -rw-r--r-- 4,359 bytes parent folder | download
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
184
185
186
187
188
189
190
191
192
193
194
keep_empty <- function(fun) {
  function(x) {
    ret <- rep_along(x, "")
    update <- which(is.na(x) | x != "")
    ret[update] <- fun(x[update])
    ret
  }
}

#' 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 `pillar.subtle` option.
#'
#' @param x The character vector to style.
#' @export
#' @seealso [pillar-package] for a list of options
#' @examples
#' style_subtle("text")
style_subtle <- keep_empty(function(x) {
  force(x)
  if (isTRUE(getOption("pillar.subtle", TRUE))) {
    crayon_grey_0.6(x)
  } else {
    x
  }
})

#' @rdname style_subtle
#' @details
#' `style_subtle_num()` is affected by the `pillar.subtle_num` option, 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(getOption("pillar.subtle_num", FALSE))) {
    style_subtle(x)
  } else {
    ifelse(negative, style_neg(x), x)
  }
}

style_hint <- keep_empty(function(x) {
  force(x)
  if (isTRUE(getOption("pillar.subtle", TRUE))) {
    crayon_grey_0.8(x)
  } else {
    x
  }
})

style_spark_na <- function(x) {
  crayon_yellow(x)
}

#' @details
#' `style_bold()` is affected by the `pillar.bold` option.
#'
#' @rdname style_subtle
#' @export
#' @examples
#' style_bold("Petal.Width")
style_bold <- function(x) {
  if (isTRUE(getOption("pillar.bold", FALSE))) {
    crayon_bold(x)
  } else {
    x
  }
}

#' @rdname style_subtle
#' @export
#' @examples
#' style_na("NA")
style_na <- function(x) {
  crayon_red(x)
}

#' @details
#' `style_neg()` is affected by the `pillar.neg` option.
#'
#' @rdname style_subtle
#' @export
#' @examples
#' style_neg("123")
style_neg <- keep_empty(function(x) {
  if (isTRUE(getOption("pillar.neg", TRUE))) {
    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
has_color <- local({
  has_color <- NULL
  function(forget = FALSE) {
    if (is.null(has_color) || forget) {
      has_color <<- crayon::has_color()
    }
    has_color
  }
})

# Important to use 16-color palette for consistent testing
is_testing <- local({
  is_testing <- FALSE
  function(set = NA) {
    if (!is.na(set)) {
      is_testing <<- set
    }
    is_testing
  }
})

# Crayon functions call crayon::has_color() every call
make_style_fast <- function(...) {
  # Force has_color to be true when making styles
  old <- options(crayon.enabled = TRUE)
  on.exit(options(old))

  style <- crayon::make_style(...)
  start <- stats::start(style)
  finish <- crayon::finish(style)

  function(...) {
    if (has_color()) {
      paste0(start, ..., finish)
    } else {
      paste0(...)
    }
  }
}

make_style_fast_16 <- function(...) {
  # Force has_color to be true when making styles
  old <- options(crayon.enabled = TRUE)
  on.exit(options(old))

  style <- crayon::make_style(...)
  start <- stats::start(style)
  finish <- crayon::finish(style)

  style_16 <- crayon::make_style(..., colors = 16)
  start_16 <- stats::start(style_16)
  finish_16 <- crayon::finish(style_16)

  function(...) {
    if (has_color()) {
      if (is_testing()) {
        paste0(start_16, gsub(finish_16, start_16, ..., fixed = TRUE), finish_16)
      } else {
        paste0(start, gsub(finish, start, ..., fixed = TRUE), finish)
      }
    } else {
      paste0(...)
    }
  }
}

# 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_16(grDevices::grey(0.6), grey = TRUE)
  crayon_grey_0.8 <<- make_style_fast_16(grDevices::grey(0.8), grey = TRUE)
}