quarto-dev / quarto-dev/quarto-cli
Support ANSI colour output with `downlit`
@cderv is already working on this.
Since Jan 13, 2023.
- Dominant language
- JavaScript
- Stars
- 6k
- Forks
- 458
- Avg merge
- 1d 9h
- Merged PRs (30d)
- 41
Description
This issue is based on the discussion in https://github.com/quarto-dev/quarto-cli/discussions/3717. Just reporting here so it's easier to follow.
It would be nice if Quarto supported ANSI colour output with downlit. Currently Quarto uses downlit for code linking when code-link: true, but this doesn't make colours work.
I thought it would work based on my experiences with pkgdown, but after some discussion it sounds like pkgdown does some additional processing with fansi to support colour output (See https://github.com/quarto-dev/quarto-cli/discussions/3717#discussioncomment-4459352). At this time, if you do need colour output in Quarto, you can get it use the fansi knitr hooks for converting ANSI highlighting to HTML highlighting. See https://github.com/quarto-dev/quarto-cli/discussions/3717#discussioncomment-4459352.
One thing that is nice about this approach with fansi is that you can turn the hook on and off throughout a document, in case you only want colour output for certain parts. For example, in this blog post I have fansi hooks on in the body of the post, but turned them off for the appendix so the output from sessioninfo::session_info() wouldn't be coloured.
If Quarto does support colour output in the future, it would be nice to make it a chunk option (e.g., colour-output: true/false) rather than a document-wide option like code-link so it can be used selectively when needed.
Here's a reprex copied from the discussion that can be used for testing.
---
title: "Reprex"
format:
html:
code-link: true
---
```{r}
#| collapse = TRUE
cat(cli::col_red("This is red"), "\n")
cat(cli::col_blue("This is blue"), "\n")
message(cli::col_green("This is green"))
warning(cli::style_bold("This is bold"))
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.