1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #1402 from GabrielGanne/master

ls: fix --color flag
This commit is contained in:
Alex Lyon 2020-01-27 15:20:43 -08:00 committed by GitHub
commit 2dad625dfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View file

@ -10,6 +10,7 @@ path = "ls.rs"
[dependencies] [dependencies]
getopts = "0.2.18" getopts = "0.2.18"
isatty = "0.1"
number_prefix = "0.2.8" number_prefix = "0.2.8"
term_grid = "0.1.5" term_grid = "0.1.5"
termsize = "0.1.6" termsize = "0.1.6"

View file

@ -14,6 +14,8 @@ extern crate termsize;
extern crate time; extern crate time;
extern crate unicode_width; extern crate unicode_width;
extern crate number_prefix; extern crate number_prefix;
extern crate isatty;
use isatty::stdout_isatty;
use number_prefix::{Standalone, Prefixed, decimal_prefix}; use number_prefix::{Standalone, Prefixed, decimal_prefix};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use time::{strftime, Timespec}; use time::{strftime, Timespec};
@ -158,7 +160,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 +613,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" => stdout_isatty(),
"never" | "no" | "none" | _ => false,
},
};
let classify = options.opt_present("classify"); let classify = options.opt_present("classify");
let ext; let ext;

View file

@ -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();
}