1
2#' @include style-var.r
3NULL
4
5#' Show the ANSI color table on the screen
6#'
7#' @param colors Number of colors to show, meaningful values
8#'   are 8 and 256. It is automatically set to the number of
9#'   supported colors, if not specified.
10#' @return The printed string, invisibly.
11#'
12#' @export
13
14show_ansi_colors <- function(colors = num_colors()) {
15  if (colors < 8) {
16    cat("Colors are not supported")
17  } else if (colors < 256) {
18    cat(ansi_colors_8(), sep = "")
19    invisible(ansi_colors_8)
20  } else {
21    cat(ansi_colors_256(), sep = "")
22    invisible(ansi_colors_256)
23  }
24}
25
26#' @importFrom grDevices rgb
27
28ansi_colors_256_col <- function() {
29  sapply(0:5, function(r) {
30    sapply(0:5, function(g) {
31      c(sapply(0:5, function(b) {
32        s <- paste0("r:", r, " g:", g, " b:", b, " ")
33        style(s, as = "grey", bg = rgb(r, g, b, maxColorValue = 5))
34      }), "\n")
35    })
36  })
37}
38
39#' @importFrom grDevices grey
40
41ansi_colors_256_grey <- function() {
42  sapply(0:23, function(g) {
43    s <- paste0(" grey ", format(g, width = 2), "    ")
44    style(s, as = "grey",
45          bg = make_style(grey(g / 23), grey = TRUE, bg = TRUE)) %+%
46      (if ((g + 1) %% 6) "" else "\n")
47  })
48}
49
50ansi_colors_256 <- function() {
51  c(ansi_colors_256_col(), "\n", ansi_colors_256_grey())
52}
53
54ansi_colors_8 <- function () {
55  multicol(sapply(seq_along(builtin_styles), function(s) {
56    st <- names(builtin_styles)[s]
57    styled <- st %+% ": " %+% style("foobar", as = st) %+% " "
58  }))
59}
60