Your code already works:
use std::process::Command;
fn main() {
let output = Command::new("ls")
.args(&["-l", "--color"])
.env("LS_COLORS", "rs=0:di=38;5;27:mh=44;38;5;15")
.output()
.expect("Failed to execute");
let sout = String::from_utf8(output.stdout).expect("Not UTF-8");
let serr = String::from_utf8(output.stderr).expect("Not UTF-8");
println!("{}", sout);
println!("{}", serr);
}
Prints the output:
total 68
-rw-r--r-- 4 root root 56158 Dec 23 00:00 [0m[44;38;5;15mCargo.lock[0m
-rw-rw-r-- 4 root root 2093 Dec 9 02:54 [44;38;5;15mCargo.toml[0m
drwxr-xr-x 1 root root 4096 Dec 30 15:24 [38;5;27msrc[0m
drwxr-xr-x 1 root root 4096 Dec 23 00:19 [38;5;27mtarget[0m
Note that there's a bunch of junk scattered inside the output ([44;
, [0m
, etc.). Those are ANSI escape codes, and the terminal emulator interprets those to change the color of the following text.
If you print the string with debugging, you will see:
\u{1b}[0m\u{1b}[44;38;5;15mCargo.lock\u{1b}[0m
Each escape code starts with an ESC
(\u{1b}
) followed by the actual command. You will have to parse those in order to ignore them for whatever processing you are doing.
Windows does not use escape codes (although maybe it can in Windows 10?), and instead a program directly modifies the console it is connected to. There is nothing in the output to indicate the color.