mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
pr: Add support for -D/--date-format parameter
This commit is contained in:
@@ -13,6 +13,8 @@ pr-help-pages = Begin and stop printing with page FIRST_PAGE[:LAST_PAGE]
|
|||||||
pr-help-header =
|
pr-help-header =
|
||||||
Use the string header to replace the file name
|
Use the string header to replace the file name
|
||||||
in the header line.
|
in the header line.
|
||||||
|
pr-help-date-format =
|
||||||
|
Use 'date'-style FORMAT for the header date.
|
||||||
pr-help-double-space =
|
pr-help-double-space =
|
||||||
Produce output that is double spaced. An extra <newline>
|
Produce output that is double spaced. An extra <newline>
|
||||||
character is output following every <newline> found in the input.
|
character is output following every <newline> found in the input.
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ pr-help-pages = Commencer et arrêter l'impression à la page PREMIÈRE_PAGE[:DE
|
|||||||
pr-help-header =
|
pr-help-header =
|
||||||
Utiliser la chaîne d'en-tête pour remplacer le nom de fichier
|
Utiliser la chaîne d'en-tête pour remplacer le nom de fichier
|
||||||
dans la ligne d'en-tête.
|
dans la ligne d'en-tête.
|
||||||
|
pr-help-date-format =
|
||||||
|
Utiliser le FORMAT de style 'date' pour la date dans la ligne d'en-tête.
|
||||||
pr-help-double-space =
|
pr-help-double-space =
|
||||||
Produire une sortie avec double espacement. Un caractère <saut de ligne>
|
Produire une sortie avec double espacement. Un caractère <saut de ligne>
|
||||||
supplémentaire est affiché après chaque <saut de ligne> trouvé dans l'entrée.
|
supplémentaire est affiché après chaque <saut de ligne> trouvé dans l'entrée.
|
||||||
|
|||||||
+25
-12
@@ -19,8 +19,8 @@ use thiserror::Error;
|
|||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::UResult;
|
use uucore::error::UResult;
|
||||||
use uucore::format_usage;
|
use uucore::format_usage;
|
||||||
use uucore::translate;
|
|
||||||
use uucore::time::{FormatSystemTimeFallback, format, format_system_time};
|
use uucore::time::{FormatSystemTimeFallback, format, format_system_time};
|
||||||
|
use uucore::translate;
|
||||||
|
|
||||||
const TAB: char = '\t';
|
const TAB: char = '\t';
|
||||||
const LINES_PER_PAGE: usize = 66;
|
const LINES_PER_PAGE: usize = 66;
|
||||||
@@ -36,6 +36,7 @@ const FF: u8 = 0x0C_u8;
|
|||||||
|
|
||||||
mod options {
|
mod options {
|
||||||
pub const HEADER: &str = "header";
|
pub const HEADER: &str = "header";
|
||||||
|
pub const DATE_FORMAT: &str = "date-format";
|
||||||
pub const DOUBLE_SPACE: &str = "double-space";
|
pub const DOUBLE_SPACE: &str = "double-space";
|
||||||
pub const NUMBER_LINES: &str = "number-lines";
|
pub const NUMBER_LINES: &str = "number-lines";
|
||||||
pub const FIRST_LINE_NUMBER: &str = "first-line-number";
|
pub const FIRST_LINE_NUMBER: &str = "first-line-number";
|
||||||
@@ -176,6 +177,13 @@ pub fn uu_app() -> Command {
|
|||||||
.help(translate!("pr-help-header"))
|
.help(translate!("pr-help-header"))
|
||||||
.value_name("STRING"),
|
.value_name("STRING"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new(options::DATE_FORMAT)
|
||||||
|
.short('D')
|
||||||
|
.long(options::DATE_FORMAT)
|
||||||
|
.value_name("FORMAT")
|
||||||
|
.help(translate!("pr-help-date-format")),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(options::DOUBLE_SPACE)
|
Arg::new(options::DOUBLE_SPACE)
|
||||||
.short('d')
|
.short('d')
|
||||||
@@ -401,16 +409,21 @@ fn parse_usize(matches: &ArgMatches, opt: &str) -> Option<Result<usize, PrError>
|
|||||||
.map(from_parse_error_to_pr_error)
|
.map(from_parse_error_to_pr_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_date_format() -> String {
|
fn get_date_format(matches: &ArgMatches) -> String {
|
||||||
// Replicate behavior from GNU manual.
|
match matches.get_one::<String>(options::DATE_FORMAT) {
|
||||||
if std::env::var("POSIXLY_CORRECT").is_ok()
|
Some(format) => format,
|
||||||
// TODO: This needs to be moved to uucore and handled by icu?
|
None => {
|
||||||
&& (std::env::var("LC_TIME").unwrap_or_default() == "POSIX"
|
// Replicate behavior from GNU manual.
|
||||||
|| std::env::var("LC_ALL").unwrap_or_default() == "POSIX")
|
if std::env::var("POSIXLY_CORRECT").is_ok()
|
||||||
{
|
// TODO: This needs to be moved to uucore and handled by icu?
|
||||||
"%b %e %H:%M %Y"
|
&& (std::env::var("LC_TIME").unwrap_or_default() == "POSIX"
|
||||||
} else {
|
|| std::env::var("LC_ALL").unwrap_or_default() == "POSIX")
|
||||||
format::LONG_ISO
|
{
|
||||||
|
"%b %e %H:%M %Y"
|
||||||
|
} else {
|
||||||
|
format::LONG_ISO
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
@@ -514,7 +527,7 @@ fn build_options(
|
|||||||
format_system_time(
|
format_system_time(
|
||||||
&mut v,
|
&mut v,
|
||||||
time,
|
time,
|
||||||
&get_date_format(),
|
&get_date_format(matches),
|
||||||
FormatSystemTimeFallback::Integer,
|
FormatSystemTimeFallback::Integer,
|
||||||
)
|
)
|
||||||
.ok()
|
.ok()
|
||||||
|
|||||||
@@ -404,6 +404,50 @@ fn test_with_offset_space_option() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_with_date_format() {
|
fn test_with_date_format() {
|
||||||
|
let test_file_path = "test_one_page.log";
|
||||||
|
let expected_test_file_path = "test_one_page.log.expected";
|
||||||
|
let mut scenario = new_ucmd!();
|
||||||
|
let value = file_last_modified_time_format(&scenario, test_file_path, "%Y__%s");
|
||||||
|
scenario
|
||||||
|
.args(&[test_file_path, "-D", "%Y__%s"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
|
||||||
|
|
||||||
|
// "Format" doesn't need to contain any replaceable token.
|
||||||
|
let mut scenario = new_ucmd!();
|
||||||
|
scenario
|
||||||
|
.args(&[test_file_path, "-D", "Hello!"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is_templated_fixture(
|
||||||
|
expected_test_file_path,
|
||||||
|
&[("{last_modified_time}", "Hello!")],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Long option also works
|
||||||
|
let mut scenario = new_ucmd!();
|
||||||
|
scenario
|
||||||
|
.args(&[test_file_path, "--date-format=Hello!"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is_templated_fixture(
|
||||||
|
expected_test_file_path,
|
||||||
|
&[("{last_modified_time}", "Hello!")],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Option takes precedence over environment variables
|
||||||
|
let mut scenario = new_ucmd!();
|
||||||
|
scenario
|
||||||
|
.env("POSIXLY_CORRECT", "1")
|
||||||
|
.env("LC_TIME", "POSIX")
|
||||||
|
.args(&[test_file_path, "-D", "Hello!"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is_templated_fixture(
|
||||||
|
expected_test_file_path,
|
||||||
|
&[("{last_modified_time}", "Hello!")],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_date_format_env() {
|
||||||
const POSIXLY_FORMAT: &str = "%b %e %H:%M %Y";
|
const POSIXLY_FORMAT: &str = "%b %e %H:%M %Y";
|
||||||
|
|
||||||
// POSIXLY_CORRECT + LC_ALL/TIME=POSIX uses "%b %e %H:%M %Y" date format
|
// POSIXLY_CORRECT + LC_ALL/TIME=POSIX uses "%b %e %H:%M %Y" date format
|
||||||
|
|||||||
Reference in New Issue
Block a user