Skip to content

Commit e5f2f79

Browse files
committed
ls: Allow reading time-style from TIME_STYLE environment variable
According to GNU manual.
1 parent a1519e1 commit e5f2f79

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/uu/ls/src/ls.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String
266266
Ok((recent.to_string(), older.map(String::from)))
267267
}
268268

269-
if let Some(field) = options.get_one::<String>(options::TIME_STYLE) {
269+
if let Some(field) = options
270+
.get_one::<String>(options::TIME_STYLE)
271+
.map(|s| s.to_owned())
272+
.or_else(|| std::env::var("TIME_STYLE").ok())
273+
{
270274
//If both FULL_TIME and TIME_STYLE are present
271275
//The one added last is dominant
272276
if options.get_flag(options::FULL_TIME)
@@ -287,7 +291,7 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String
287291
}
288292
field
289293
} else {
290-
field
294+
&field
291295
};
292296

293297
match time_styles.get(field) {

tests/by-util/test_ls.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,23 @@ fn test_ls_styles() {
20682068
.arg("-x")
20692069
.succeeds()
20702070
.stdout_is("test test2\n");
2071+
2072+
// Time style can also be setup from environment
2073+
scene
2074+
.ucmd()
2075+
.env("TIME_STYLE", "full-iso")
2076+
.arg("-l")
2077+
.succeeds()
2078+
.stdout_matches(&re_full);
2079+
2080+
// ... but option takes precedence
2081+
scene
2082+
.ucmd()
2083+
.env("TIME_STYLE", "full-iso")
2084+
.arg("-l")
2085+
.arg("--time-style=long-iso")
2086+
.succeeds()
2087+
.stdout_matches(&re_long);
20712088
}
20722089

20732090
#[test]

0 commit comments

Comments
 (0)