From edaf2d85cb05c4dc9dad720e2a11cdbde8c025c0 Mon Sep 17 00:00:00 2001 From: Gabriel Ganne Date: Thu, 20 Jun 2019 09:55:01 +0200 Subject: [PATCH] ls: implement --color flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GNU coreutils ls command implements the --color option as follow: --color[=WHEN] colorize the output; WHEN can be 'always' (default if omitted), 'auto', or 'never' With --color=auto, ls emits color codes only when standard output is connected to a terminal. Also, add support for the following aliases: - ‘always’, ‘yes’, ‘force’ - ‘never’, ‘no’, ‘none’ - ‘auto’, ‘tty’, ‘if-tty’ Signed-off-by: Gabriel Ganne --- src/ls/ls.rs | 11 +++++++++-- tests/test_ls.rs | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ls/ls.rs b/src/ls/ls.rs index 658be8d2c..86d9385bb 100644 --- a/src/ls/ls.rs +++ b/src/ls/ls.rs @@ -158,7 +158,7 @@ pub fn uumain(args: Vec) -> i32 { directory. This is especially useful when listing very large directories, \ since not doing any sorting can be noticeably faster.", ) - .optflag("", "color", "Color output based on file type.") + .optflagopt("", "color", "Color output based on file type.", "always|auto|never") .parse(args); list(matches); @@ -611,7 +611,14 @@ fn display_file_name( } let mut width = UnicodeWidthStr::width(&*name); - let color = options.opt_present("color"); + let color = match options.opt_str("color") { + None => true, + Some(val) => match val.as_ref() { + "always" | "yes" | "force" => true, + "auto" | "tty" | "if-tty" => true, /* TODO */ + "never" | "no" | "none" | _ => false, + }, + }; let classify = options.opt_present("classify"); let ext; diff --git a/tests/test_ls.rs b/tests/test_ls.rs index 374bfbb08..4cb5904af 100644 --- a/tests/test_ls.rs +++ b/tests/test_ls.rs @@ -11,3 +11,10 @@ fn test_ls_ls_i() { new_ucmd!().arg("-i").succeeds(); new_ucmd!().arg("-il").succeeds(); } + +#[test] +fn test_ls_ls_color() { + new_ucmd!().arg("--color").succeeds(); + new_ucmd!().arg("--color=always").succeeds(); + new_ucmd!().arg("--color=never").succeeds(); +}