diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 28d74775d..cf8ed6292 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -9,6 +9,7 @@ use std::borrow::Borrow; use std::env; use std::fmt::Write; use std::fs::File; +use std::io::IsTerminal; use std::io::{BufRead, BufReader}; use std::path::Path; @@ -192,9 +193,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let result; if files.is_empty() { - println!("{}", generate_ls_colors(&out_format, ":")); - - return Ok(()); + // Check if data is being piped into the program + if std::io::stdin().is_terminal() { + // No data piped, use default behavior + println!("{}", generate_ls_colors(&out_format, ":")); + return Ok(()); + } else { + // Data is piped, process the input from stdin + let fin = BufReader::new(std::io::stdin()); + result = parse(fin.lines().map_while(Result::ok), &out_format, "-"); + } } else if files.len() > 1 { return Err(UUsageError::new( 1, @@ -376,7 +384,8 @@ where let (key, val) = line.split_two(); if val.is_empty() { return Err(format!( - "{}:{}: invalid line; missing second token", + // The double space is what GNU is doing + "{}:{}: invalid line; missing second token", fp.maybe_quote(), num )); diff --git a/tests/by-util/test_dircolors.rs b/tests/by-util/test_dircolors.rs index d4fa0a3b0..e3752fcde 100644 --- a/tests/by-util/test_dircolors.rs +++ b/tests/by-util/test_dircolors.rs @@ -159,6 +159,16 @@ fn test_quoting() { .no_stderr(); } +#[test] +fn test_print_ls_colors() { + new_ucmd!() + .pipe_in("OWT 40;33\n") + .args(&["--print-ls-colors"]) + .succeeds() + .stdout_is("\x1B[40;33mtw\t40;33\x1B[0m\n") + .no_stderr(); +} + #[test] fn test_extra_operand() { new_ucmd!()