Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/uu/ls/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,13 +1056,17 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String
"locale" => ok(LOCALE_FORMAT),
_ => match field.chars().next().unwrap() {
'+' => {
// recent/older formats are (optionally) separated by a newline
// Formats are (optionally) separated by a newline:
// FORMAT1 NEWLINE FORMAT2 -> FORMAT1 for older files, FORMAT2 for recent.
// A single format applies to all files (stored as recent).
let mut it = field[1..].split('\n');
let recent = it.next().unwrap_or_default();
let older = it.next();
let first = it.next().unwrap_or_default();
match it.next() {
None => ok((recent, older)),
Some(_) => Err(LsError::TimeStyleParseError(String::from(field))),
None => ok((first, None)),
Some(second) => match it.next() {
None => ok((second, Some(first))),
Some(_) => Err(LsError::TimeStyleParseError(String::from(field))),
},
}
}
_ => Err(LsError::TimeStyleParseError(String::from(field))),
Expand Down
18 changes: 10 additions & 8 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2335,15 +2335,17 @@ fn test_ls_time_styles() {
.stdout_matches(&re_custom_format_recent)
.stdout_matches(&re_custom_format_old);

//+FORMAT_RECENT\nFORMAT_OLD
//+FORMAT_OLD\nFORMAT_RECENT: FORMAT1 applies to old files, FORMAT2 to recent files.
let re_custom_format_recent_2 =
Regex::new(r"[a-z-]* \d* [\w.]* [\w.]* \d* \d{4}--\d{2} test\n").unwrap();
let re_custom_format_old =
Regex::new(r"[a-z-]* \d* [\w.]* [\w.]* \d* \d{4}--\d{2} test-old\n").unwrap();
Regex::new(r"[a-z-]* \d* [\w.]* [\w.]* \d* \d{4}__\d{2} test-old\n").unwrap();
scene
.ucmd()
.arg("-l")
.arg("--time-style=+%Y__%M\n%Y--%M")
.succeeds()
.stdout_matches(&re_custom_format_recent)
.stdout_matches(&re_custom_format_recent_2)
.stdout_matches(&re_custom_format_old);

// Also fails due to not having full clap support for time_styles
Expand Down Expand Up @@ -2471,13 +2473,13 @@ fn test_ls_time_recent_future() {
.succeeds()
.stdout_matches(&re_iso_old);

// Also test that we can set a format that varies for recent of older files.
//+FORMAT_RECENT\nFORMAT_OLD
// A two-line format assigns FORMAT1 to old files and FORMAT2 to recent files.
//+FORMAT_OLD\nFORMAT_RECENT
f.set_modified(SystemTime::now()).unwrap();
scene
.ucmd()
.arg("-l")
.arg("--time-style=+RECENT\nOLD")
.arg("--time-style=+OLD\nRECENT")
.succeeds()
.stdout_contains("RECENT");

Expand All @@ -2487,11 +2489,11 @@ fn test_ls_time_recent_future() {
scene
.ucmd()
.arg("-l")
.arg("--time-style=+RECENT\nOLD")
.arg("--time-style=+OLD\nRECENT")
.succeeds()
.stdout_contains("OLD");

// RECENT format is still used if no "OLD" one provided.
// The single format is used for all files when no second one is provided.
scene
.ucmd()
.arg("-l")
Expand Down
Loading