dircolors: manage the --print-ls-colors pipe option

This commit is contained in:
Sylvestre Ledru 2023-12-03 10:34:33 +01:00
parent f5776bc511
commit dabbcff9fb
2 changed files with 23 additions and 4 deletions

View file

@ -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() {
// 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,6 +384,7 @@ where
let (key, val) = line.split_two();
if val.is_empty() {
return Err(format!(
// The double space is what GNU is doing
"{}:{}: invalid line; missing second token",
fp.maybe_quote(),
num

View file

@ -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!()