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
|