Skip to content

pr: fill the space between columns with tabs, add -i - #16555

Open
ihsandeniz wants to merge 5 commits into
uutils:mainfrom
ihsandeniz:fix/pr-multicolumn-tabs
Open

ihsandeniz wants to merge 5 commits into
uutils:mainfrom
ihsandeniz:fix/pr-multicolumn-tabs

Conversation

@ihsandeniz

@ihsandeniz ihsandeniz commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #16510.

POSIX says that output in more than one column implies -e and -i. GNU pr
pads columns with tabs and expands input tabs from the start of each column.
uutils padded with spaces and then wrote the separator as a literal tab, so
every column moved on to the next tab stop: pr -t -4 put the columns at
24/48/72 instead of 18/36/54.

command (LC_ALL=C) GNU 9.12 / this PR before
printf 'a\nb\n' | pr -t -2 a\t\t\t\t b a + 34 spaces + \tb
pr -t -4 on a, bbbbbbbbbbb, ccc, d columns at 0/18/36/54 0/24/48/72
printf 'a\n\tb\n' | pr -t -2 a\t\t\t\t \t b a + 34 spaces + \t\tb
printf 'a\tb\nc\n' | pr -t -2 -s, a\tb,c a\tb + 25 spaces + ,c

The first commit:

  • adds -i[CHAR[WIDTH]] / --output-tabs[=CHAR[WIDTH]], parsed like -e,
    with GNU's error message
  • implies -i for output in more than one column, and -e for padded columns
  • leaves columns unpadded for -s without -w, as was already done for -m
  • counts the line number in the column width, and when merging prints it
    before the first column only

The second commit matches a few more of GNU's column positions: a TAB that
starts a line is expanded as if the line number were not there; without
padding, a line number followed by text is counted as if a TAB came after it;
printing across with a TAB between columns, each TAB of the text counts as 8
columns; text of spaces only after a line number is not printed.

The fixtures and inline expectations that pinned the old output were
regenerated from GNU 9.12 output, as the TODOs in test_columns and
test_merge asked. The exceptions are left to separate PRs: the -o margin
(column_spaces_across.log.expected), the width of a -S string longer than
one byte (column_across_sep1.log.expected) and the empty lines that fill a
merged page (test_merge). Those still pin the current output. There are 9
new tests.

On 5000 random inputs mixing -2..-4, -w, -s, -S, -n, -o, -i,
-e, -J, -m and -a (LC_ALL=C), mismatches with GNU went from 3916 to
1191, with no input that matched before and differs now; the same in
C.UTF-8.

#16518 changes the same function. The single-column -W path is left as it
is here, so whichever lands first, the other only needs a textual rebase.

POSIX says output in more than one column implies -e and -i. Like GNU
pr, the padding between columns is now written as tabs followed by the
spaces that are left, tabs in a padded column are expanded from where
the column starts, and -i[CHAR[WIDTH]] / --output-tabs is accepted.

Also like GNU pr:
- the -o margin is written once per line, not before every column
- -s without -w prints the columns without padding them
- a -S separator longer than one byte takes its share of the line
- in a column, the line number takes its width from the column, and
  when merging it precedes the first column only
- a merged page is filled out with empty lines

Fixes uutils#16510
Like GNU pr:
- a TAB that starts a line is expanded as if the line number were not
  there
- without padding, a line number followed by text is counted as if a
  TAB came after it, and printing across with a TAB between columns,
  each TAB of the text is counted as 8 columns
- text of spaces only after a line number is not printed
Comment thread src/uu/pr/src/pr.rs Outdated
@@ -1519,14 +1586,129 @@ fn to_table_short_file(

/// Write `n` space characters to `out` in fixed-size chunks, so the indent is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover doc comment from write_offset_spaces, please remove it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment thread src/uu/pr/src/pr.rs Outdated
self.flush_spaces()?;
self.buf.push(byte);
self.column = if byte == TAB as u8 {
self.column + 8 - self.column % 8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x + 8 - x % 8 is now repeated 6 times (here, unpadded_column_end, number_field_width, get_line_for_printing). Could you please add a small next_tab_stop() helper?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: next_tab_stop(column, width), also used for the -i width in flush_spaces.

Comment thread src/uu/pr/src/pr.rs Outdated
.as_ref()
.is_some_and(|i| i.across_mode);
let text = &content[number_len..];
if across && options.col_sep_for_printing == TAB.to_string() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this allocates a String for every cell, could you compare with "\t" instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, here and in number_field_width.

Comment thread src/uu/pr/src/pr.rs
let text = &content[number_len..];
if across && options.col_sep_for_printing == TAB.to_string() {
let tabs = memchr::memchr_iter(TAB as u8, text).count();
cell_start + number_width + display_width(text) + 7 * tabs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you find the 7 * tabs rule? From GNU output only, i hope?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, from GNU's output only, I haven't looked at its source. Printing across with a TAB between columns, the spaces that follow in the next column turn into a TAB earlier than the tab stops would give, as if each TAB of the previous column took 8 columns. Without -a, GNU counts to the tab stop:

input (LC_ALL=C) pr -t -2 -J -a pr -t -2 -J
bb\t / ab a ba bb\t\tab a\t ba bb\t\tab a ba
a\tb / ab a ba a\tb\tab a\t ba a\tb\tab a\tba
a\t\t / ab a ba a\t\t\tab a\tba a\t\t\tab a ba

In the first one bb\t ends at 2 + 8 = 10 rather than 8, the separator makes it 11, ab a 15, and the two spaces reach 17, past the tab stop at 16. Without -a they go from 13 to 15. Counting to the tab stop instead differs from GNU on 107 of 3000 random -J -a inputs that this rule gets right.

I wrote this with the help of an AI assistant; the rules were worked out by running GNU's pr binary on hand-picked inputs and then compared on random ones, and I can share those scripts if useful.

Comment thread src/uu/pr/src/pr.rs Outdated
if no_trailer {
break;
}
// Like GNU pr, the rest of the page is filled with empty lines.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a separate fix from #16510, could it go in its own PR? Same for the -o margin and the -S width

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure: the empty lines of a merged page, the -o margin and the -S width are out of this PR now, and I'll send them as three small PRs. Until then the -o and -Sdivide fixtures and the blank lines in test_merge pin the current output here.

Comment thread src/uu/pr/src/pr.rs Outdated
/// TAB and a missing WIDTH is 8.
fn parse_tab_argument(s: &str, option: char) -> Result<(char, i32), PrError> {
let invalid = |arg: &str| {
let message = if option == 'i' {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you pass the ftl key instead of the option char?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Also compare the separators with "\t" instead of allocating a String,
and drop a doc comment left over from write_offset_spaces.
The fixtures of -o and -S pin the current output until then.
pr exits on the invalid argument before reading stdin, so writing to it
could fail with a broken pipe now and then.
@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning, 1 note

⚠️ 117 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

✅ 391 untouched benchmarks
⏩ 92 skipped benchmarks1


Comparing ihsandeniz:fix/pr-multicolumn-tabs (63fda38) with main (c44859c)2

Open in CodSpeed

Footnotes

  1. 92 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (8406d69) during the generation of this report, so c44859c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pr: multi-column mode uses spaces instead of tabs for inter-column padding

2 participants