mirror of
https://github.com/uutils/coreutils.git
synced 2025-12-23 08:47:37 +00:00
pr: fix header formatting for custom date formats starting with '+'
Should fix tests/misc/time-style.sh
This commit is contained in:
parent
d4321315f1
commit
fa82066cee
2 changed files with 97 additions and 16 deletions
|
|
@ -1150,23 +1150,61 @@ fn get_formatted_line_number(opts: &OutputOptions, line_number: usize, index: us
|
|||
/// Returns a five line header content if displaying header is not disabled by
|
||||
/// using `NO_HEADER_TRAILER_OPTION` option.
|
||||
fn header_content(options: &OutputOptions, page: usize) -> Vec<String> {
|
||||
if options.display_header_and_trailer {
|
||||
let first_line = format!(
|
||||
"{} {} {} {page}",
|
||||
options.last_modified_time,
|
||||
options.header,
|
||||
translate!("pr-page")
|
||||
);
|
||||
vec![
|
||||
String::new(),
|
||||
String::new(),
|
||||
first_line,
|
||||
String::new(),
|
||||
String::new(),
|
||||
]
|
||||
} else {
|
||||
Vec::new()
|
||||
if !options.display_header_and_trailer {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
// The header should be formatted with proper spacing:
|
||||
// - Date/time on the left
|
||||
// - Filename centered
|
||||
// - "Page X" on the right
|
||||
let date_part = &options.last_modified_time;
|
||||
let filename = &options.header;
|
||||
let page_part = format!("{} {page}", translate!("pr-page"));
|
||||
|
||||
// Use the line width if available, otherwise use default of 72
|
||||
let total_width = options.line_width.unwrap_or(DEFAULT_COLUMN_WIDTH);
|
||||
|
||||
// GNU pr uses a specific layout:
|
||||
// Date takes up the left part, filename is centered, page is right-aligned
|
||||
let date_len = date_part.chars().count();
|
||||
let filename_len = filename.chars().count();
|
||||
let page_len = page_part.chars().count();
|
||||
|
||||
let header_line = if date_len + filename_len + page_len + 2 < total_width {
|
||||
// Check if we're using a custom date format that needs centered alignment
|
||||
// This preserves backward compatibility while fixing the GNU time-style test
|
||||
if date_part.starts_with('+') {
|
||||
// GNU pr uses centered layout for headers with custom date formats
|
||||
// The filename should be centered between the date and page parts
|
||||
let space_for_filename = total_width - date_len - page_len;
|
||||
let padding_before_filename = (space_for_filename - filename_len) / 2;
|
||||
let padding_after_filename =
|
||||
space_for_filename - filename_len - padding_before_filename;
|
||||
|
||||
format!(
|
||||
"{date_part}{:width1$}{filename}{:width2$}{page_part}",
|
||||
"",
|
||||
"",
|
||||
width1 = padding_before_filename,
|
||||
width2 = padding_after_filename
|
||||
)
|
||||
} else {
|
||||
// For standard date formats, use simple spacing for backward compatibility
|
||||
format!("{date_part} {filename} {page_part}")
|
||||
}
|
||||
} else {
|
||||
// If content is too long, just use single spaces
|
||||
format!("{date_part} {filename} {page_part}")
|
||||
};
|
||||
|
||||
vec![
|
||||
String::new(),
|
||||
String::new(),
|
||||
header_line,
|
||||
String::new(),
|
||||
String::new(),
|
||||
]
|
||||
}
|
||||
|
||||
/// Returns five empty lines as trailer content if displaying trailer
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue