rust-lang / rust-lang/mdBook

Allow paging coloured terminal output

Open
#2,939 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement Command-test
Dominant language
Rust
Stars
22.2k
Forks
1.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the documented mdbook test ... | less -R command and inspect the CLI and test-output paths responsible for deciding whether ANSI colors are emitted. Done means providing a documented color-forcing option or equivalent behavior that preserves colored output through a pipe, with coverage for the relevant command-line usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.