mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

Allow paging coloured terminal output

Open git-staus opened this issue 2 months ago • 0 comments

Problem

Using this repo as an example,

mdbook test -c 02-initialization/01-initialization.md

produces coloured output on terminals that support it (in my case, the macOS 14.0 terminal). However, if I try to pipe it into a colour-preserving screen reader like so

mdbook test -c 02-initialization/01-initialization.md |& less -R

I get monochrome output. I cannot find any way to change this behaviour.

Proposed Solution

Add some option to mdBook to emit ANSI escape codes even when stdout is not declared as a terminal.

Here are some of the usual ways I can think of other CLI tools do this:

mdbook --color=always test -c 02-initialization/01-initialization.md |& less -R
mdbook test --color=always -c 02-initialization/01-initialization.md |& less -R
mdbook test -- --color=always -c 02-initialization/01-initialization.md |& less -R
mdbook test -- --color=always |& less -R
mdbook test -c 02-initialization/01-initialization.md --color=always |& less -R
mdbook test -c 02-initialization/01-initialization.md -- --color=always |& less -R
CARGO_TERM_COLOR=always mdbook test -c 02-initialization/01-initialization.md |& less -R
TERM_COLOR=1 mdbook test -c 02-initialization/01-initialization.md |& less -R
TERM_COLOR=always mdbook test -c 02-initialization/01-initialization.md |& less -R
TERM=xterm-color mdbook test -c 02-initialization/01-initialization.md |& less -R
TERM=xterm-256color mdbook test -c 02-initialization/01-initialization.md |& less -R

I am not sure which option is best practice (Bash is a mess) but I confirmed that none of these options current do what you would expect.

Notes

I have something similar set up for Cargo (hence my surprise mdBook did not respect it):

# in ~/.cargo/config.toml

[term]
color = "always"

and then running

cargo clippy |& less -R

does not result in monochrome output. This can also be done without changing the configuration file:

cargo --color=always clippy |& less -R

or

CARGO_TERM_COLOR=always cargo clippy |& less -R

git-staus avatar Nov 17 '25 19:11 git-staus