colored-rs/colored

Colors don't work when saving to variable in bash

gbaranski opened this issue · 2 comments

Hello, I've got problem with coloring after saving program output to variable, thats my code

use colored::*;

fn main() {
    println!("{}", "hello world".red());
}

and then it's wrapped using bash script

output=$(program)
echo -e "${output}"

and it prints out hello world without colors. ansi_term crate works.

I guess this is by intention since it does not run on a terminal. The check is here (atty.is(...)):
https://github.com/mackwic/colored/blob/015f2a58bfac8da6f0db8518c6813c79ec063f7f/src/control.rs#L100-L115

Yes, colored try to be smart and not emit colors when the output will be processed by a program rather than a human.

You can set CLICOLOR_FORCE=1 to force the colors anyway.