Skip to content

Commit f344af5

Browse files
authored
Merge pull request #944 from tidyverse/docs-941-pretty
2 parents e82d5fa + f2050b7 commit f344af5

File tree

5 files changed

+2226
-429
lines changed

5 files changed

+2226
-429
lines changed

R/fansi.R

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# nocov start - https://github.com/tidyverse/tibble/blob/master/R/fansi.R
2+
set_fansi_hooks <- function() {
3+
knitr::opts_chunk$set(collapse = TRUE)
4+
5+
if (Sys.getenv("IN_GALLEY") != "") {
6+
knitr::opts_chunk$set(comment = "#>")
7+
return()
8+
}
9+
10+
knitr::opts_chunk$set(comment = pillar::style_subtle("#>"))
11+
12+
options(cli.num_colors = 256)
13+
options(pillar.bold = TRUE)
14+
15+
knitr::knit_hooks$set(
16+
output = colourise_chunk("output"),
17+
message = colourise_chunk("message"),
18+
warning = colourise_chunk("warning"),
19+
error = colourise_chunk("error")
20+
)
21+
22+
invisible()
23+
}
24+
25+
colourise_chunk <- function(type) {
26+
# Fallback if fansi is missing
27+
if (is_installed("fansi")) {
28+
sgr_to_html <- fansi::sgr_to_html
29+
} else {
30+
sgr_to_html <- identity
31+
}
32+
33+
function(x, options) {
34+
# lines <- strsplit(x, "\\n")[[1]]
35+
lines <- x
36+
if (type != "output") {
37+
lines <- crayon::red(lines)
38+
}
39+
paste0(
40+
'<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode">',
41+
paste0(
42+
sgr_to_html(htmltools::htmlEscape(lines)),
43+
collapse = "\n"
44+
),
45+
"</code></pre></div>"
46+
)
47+
}
48+
}
49+
# nocov end

0 commit comments

Comments
 (0)