thiagowfx's avatar

¬ just serendipity 🍀 (not just serendipity)

watch with --color

• 156 words • 1 min • updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

When using watch(1) to monitor program output, colors will not be emitted by default.

Thankfully watch(1) has a --color (or -c flag).

Still, when using it with e.g. node tests, colors are not emitted:

% watch --color -- node mytest.js
Every 2.0s: node ex5.js                       MacBookAir: 14:43:06
                                                     in 0.107s (0)
✔ toWasm (0.865792ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 3.8805

The command detects that it’s not attached to the terminal. :(

How can we force color output in this case?

There’s no universal answer:

Another possibility, which is program agnostic, is to use an utility such as unbuffer(1) from the TCL Expect package:

shell
% watch --color -- unbuffer node mytest.js

The unbuffer solution is my favorite (previously).

Reference: Stack Overflow.