mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
ls: implement --color flag
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 <gabriel.ganne@gmail.com>
This commit is contained in:
parent
33a112d64b
commit
edaf2d85cb
2 changed files with 16 additions and 2 deletions
11
src/ls/ls.rs
11
src/ls/ls.rs
|
@ -158,7 +158,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
directory. This is especially useful when listing very large directories, \
|
directory. This is especially useful when listing very large directories, \
|
||||||
since not doing any sorting can be noticeably faster.",
|
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);
|
.parse(args);
|
||||||
|
|
||||||
list(matches);
|
list(matches);
|
||||||
|
@ -611,7 +611,14 @@ fn display_file_name(
|
||||||
}
|
}
|
||||||
let mut width = UnicodeWidthStr::width(&*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 classify = options.opt_present("classify");
|
||||||
let ext;
|
let ext;
|
||||||
|
|
||||||
|
|
|
@ -11,3 +11,10 @@ fn test_ls_ls_i() {
|
||||||
new_ucmd!().arg("-i").succeeds();
|
new_ucmd!().arg("-i").succeeds();
|
||||||
new_ucmd!().arg("-il").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();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue