File: fansi.R

package info (click to toggle)
r-cran-tibble 3.1.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,008 kB
  • sloc: ansic: 317; sh: 10; makefile: 5
file content (49 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download | duplicates (2)
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
# nocov start - https://github.com/tidyverse/tibble/blob/main/R/fansi.R
set_fansi_hooks <- function() {
  knitr::opts_chunk$set(collapse = TRUE)

  if (Sys.getenv("IN_GALLEY") != "") {
    knitr::opts_chunk$set(comment = "#>")
    return()
  }

  knitr::opts_chunk$set(comment = pillar::style_subtle("#>"))

  options(cli.num_colors = 256)
  options(pillar.bold = TRUE)

  knitr::knit_hooks$set(
    output = colourise_chunk("output"),
    message = colourise_chunk("message"),
    warning = colourise_chunk("warning"),
    error = colourise_chunk("error")
  )

  invisible()
}

colourise_chunk <- function(type) {
  # Fallback if fansi is missing
  if (is_installed("fansi")) {
    sgr_to_html <- fansi::sgr_to_html
  } else {
    sgr_to_html <- identity
  }

  function(x, options) {
    # lines <- strsplit(x, "\\n")[[1]]
    lines <- x
    if (type != "output") {
      lines <- crayon::red(lines)
    }
    paste0(
      '<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode">',
      paste0(
        sgr_to_html(htmltools::htmlEscape(lines)),
        collapse = "\n"
      ),
      "</code></pre></div>"
    )
  }
}
# nocov end