From 3be1309dbfb3ba8cf2ff714d8a27410af704fd54 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 7 Jun 2026 11:18:39 +0200 Subject: [PATCH] ls: apply two-line --time-style format to old/recent files correctly FORMAT1 NEWLINE FORMAT2 should use FORMAT1 for non-recent (old) files and FORMAT2 for recent files; the assignment was reversed. --- src/uu/ls/src/config.rs | 14 +++++++++----- tests/by-util/test_ls.rs | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/uu/ls/src/config.rs b/src/uu/ls/src/config.rs index 3a879b83735..d1650a3a7d9 100644 --- a/src/uu/ls/src/config.rs +++ b/src/uu/ls/src/config.rs @@ -1056,13 +1056,17 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option 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))), diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 55e9b22e112..8111901e906 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -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 @@ -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"); @@ -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")