← Back to Pull requests

pr: stream simple layouts for non-file inputs #11244

Open
mattsu2020 opened this pull request with 704 lines diff across 4 files
View on GitHub
mattsu2020 mattsu2020 commented on Mar 09, 2026 05:22 UTC
Author

Summary

This PR adds a streaming path for simple pr layouts so inputs such as
character devices and FIFOs can be processed incrementally instead of buffering
the entire input in memory.

The main goal is to avoid OOM-like behavior for inputs such as /dev/zero,
where pr can produce output line by line without waiting for a full read.

What changed

  • add a streaming implementation for simple pr layouts

  • use the streaming path for supported non-regular-file inputs

  • align streaming output with the existing buffered behavior, including page trailer formatting

  • fall back to the buffered path when the selected page layout leaves no printable content lines

  • add regression tests for:

  • - character-device input
    - FIFO/file parity for layouts that actually exercise the streaming path
    - FIFO/file parity in zero-content-line fallback cases

    Related

  • #11139
  • Diff: 704 lines (+559 / -145) across 4 files Reviewer: sylvestre Review duration: 389174m
    All Vouch review confidence checks passed
    Residual risk 0.08 = Change Risk (0.54) × (1 − Review Confidence (0.86)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.54 · 704 lines diff across 4 files
    0.54
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.92 · 5 substantive comment(s)
    0.92
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    85%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.392
    Real PR Conversation Timeline
    26 items
    sylvestre sylvestre commented on Mar 09, 2026 05:24 UTC
    Contributor

    the list of commits isn't ideal, could you please improve it? thanks

    github-actions[bot] github-actions[bot] commented on Mar 09, 2026 05:40 UTC
    Bot

    GNU testsuite comparison:

    Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
    Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)
    Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
    Congrats! The gnu test tests/rm/many-dir-entries-vs-OOM is now passing!
    Congrats! The gnu test tests/tail/pipe-f is now passing!
    

    codspeed[bot] codspeed[bot] commented on Mar 09, 2026 05:41 UTC
    Bot

    <!-- __CODSPEED_PERFORMANCE_REPORT_COMMENT__ -->

    Merging this PR will regress 1 benchmark

    <details>
    <summary><b>2 warnings, 1 note</b></summary>

    :warning: 124 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

    :warning: 4 benchmarks with a significant change ran on different hardware

    > Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

    > Learn more about Macro Runners

    > See affected benchmarks

    :information_source: 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

    </details>

    ⚡ 3 improved benchmarks
    ❌ 1 regressed benchmark
    ✅ 411 untouched benchmarks
    ⏩ 74 skipped benchmarks[^skipped]

    [!WARNING]

    Please fix the performance issues or acknowledge them on CodSpeed.

    Performance Changes

    | | Mode | Benchmark | BASE | HEAD | Efficiency |
    | --- | ---- | --------- | ------ | ------ | ---------- |
    | ❌ | CPU Simulation | ` df_deep_directory | 898.5 µs | 941.1 µs | -4.52% |
    | ⚡ | Memory |
    timeout_enforced | 167.5 KB | 88.7 KB | +88.88% |
    | ⚡ | CPU Simulation | [
    socket_addrs_direct[100000] ](https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Apr_zero?uri=src%2Fuu%2Fhostname%2Fbenches%2Fhostname_bench.rs%3A%3Asocket_addrs_direct%255B100000%255D&runnerMode=Simulation&utm_source=github&utm_medium=comment-v2&utm_content=benchmark) | 132.1 µs | 105.1 µs | +25.7% |
    | ⚡ | CPU Simulation | [
    hostname_ip_lookup[100000] ](https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Apr_zero?uri=src%2Fuu%2Fhostname%2Fbenches%2Fhostname_bench.rs%3A%3Ahostname_ip_lookup%255B100000%255D&runnerMode=Simulation&utm_source=github&utm_medium=comment-v2&utm_content=benchmark) | 573.3 µs | 545.5 µs | +5.09% |

    [!TIP]

    Investigate this regression by commenting @codspeedbot fix this regression` on this PR, or directly use the CodSpeed MCP with your agent.

    ---

    <sub>Comparing <code>mattsu2020:pr_zero</code> (d79808e) with <code>main</code> (ce58468)[^unexpected-base]</sub>

    <a href="https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Apr_zero?utm_source=github&utm_medium=comment-v2&utm_content=button">
    <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://codspeed.io/pr-report/open-in-codspeed-dark.svg">
    <source media="(prefers-color-scheme: light)" srcset="https://codspeed.io/pr-report/open-in-codspeed-light.svg">
    <img alt="Open in CodSpeed" src="https://codspeed.io/pr-report/open-in-codspeed-light.svg" width="169" height="32">
    </picture>
    </a>

    [^skipped]: 74 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.
    [^unexpected-base]: No successful run was found on <code>main</code> (f02ef8e) during the generation of this report, so ce58468 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

    github-actions[bot] github-actions[bot] commented on Mar 24, 2026 10:41 UTC
    Bot

    GNU testsuite comparison:

    Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)
    Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
    Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
    Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
    Note: The gnu test tests/tail/pipe-f is now being skipped but was previously passing.
    

    github-actions[bot] github-actions[bot] commented on May 23, 2026 05:57 UTC
    Bot

    <!-- gnu-comment-bot -->
    GNU testsuite comparison:

    Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
    

    sylvestre sylvestre commented on code on Jun 07, 2026 09:34 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -884,6 +884,280 @@ fn read_to_end(path: &str) -> Result<Vec<u8>, std::io::Error> {
         }
     }
     
    +fn should_use_streaming_pr(path: &str, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && !options.join_lines
    +        && options.line_width.is_none()
    +        && options.offset_spaces.is_empty();
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    path != FILE_STDIN
    +        && lines_to_read_for_page(options) > 0
    +        && metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +fn page_is_in_range(page: usize, options: &OutputOptions) -> bool {
    +    options.start_page <= page && options.end_page.is_none_or(|end| page <= end)
    +}
    +
    +fn write_stream_page_header(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: usize,
    +) -> Result<(), std::io::Error> {
    +    let line_separator = options.line_separator.as_bytes();
    +    for line in header_content(options, page) {
    +        out.write_all(line.as_bytes())?;
    +        out.write_all(line_separator)?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_stream_page_trailer(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    lines_in_page: usize,
    +) -> Result<(), std::io::Error> {
    +    let content_line_separator = options.content_line_separator.as_bytes();
    +    if !options.form_feed_used {
    +        // `print_page`/`write_columns` emits blank-content separators until
    +        // the page reaches `lines_needed_per_page`.
    +        let lines_needed_per_page = lines_to_read_for_page(options);
    +        for _ in lines_in_page..lines_needed_per_page {
    +            out.write_all(content_line_separator)?;
    +        }
    +    }
    +
    +    let line_separator = options.line_separator.as_bytes();
    +    let trailer = trailer_content(options);
    +    for (index, line) in trailer.iter().enumerate() {
    +        out.write_all(line.as_bytes())?;
    +        if index + 1 != trailer.len() {
    +            out.write_all(line_separator)?;
    +        }
    +    }
    +    out.write_all(options.page_separator_char.as_bytes())?;
    +    Ok(())
    +}
    +
    +fn finish_stream_page(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: &mut usize,
    +    page_active: &mut bool,
    +    page_header_written: &mut bool,
    +    lines_in_page: &mut usize,
    +) -> Result<bool, std::io::Error> {
    +    if *page_active && page_is_in_range(*page, options) {
    +        if !*page_header_written {
    +            write_stream_page_header(out, options, *page)?;
    +            *page_header_written = true;
    +        }
    +        write_stream_page_trailer(out, options, *lines_in_page)?;
    +    }
    +
    +    *page += 1;
    +    *page_active = false;
    +    *page_header_written = false;
    +    *lines_in_page = 0;
    +
    +    Ok(options.end_page.is_some_and(|end| *page > end))
    +}
    +
    +fn validate_utf8_stream_chunk(utf8_tail: &mut Vec<u8>, chunk: &[u8]) -> Result<(), PrError> {
    +    let mut combined;
    +    let data = if utf8_tail.is_empty() {
    +        chunk
    +    } else {
    +        combined = Vec::with_capacity(utf8_tail.len() + chunk.len());
    +        combined.extend_from_slice(utf8_tail);
    +        combined.extend_from_slice(chunk);
    +        combined.as_slice()
    +    };
    +
    +    match std::str::from_utf8(data) {
    +        Ok(_) => {
    +            utf8_tail.clear();
    +            Ok(())
    +        }
    +        Err(err) => {
    +            if err.error_len().is_some() {

    Streaming rejects invalid UTF-8 with exit 1, but the buffered path uses from_utf8_lossy and GNU passes bytes through — so the same bytes succeed from a file yet fail from a FIFO, which is the opposite of the file/FIFO parity this PR is for. Drop the validation and stream the bytes.

    sylvestre sylvestre commented on code on Jun 07, 2026 09:34 UTC
    Code Review
    tests/by-util/test_pr.rs:1
    @@ -566,6 +598,61 @@ fn test_pr_char_device_dev_null() {
         new_ucmd!().arg("/dev/null").succeeds();
     }
     
    +#[cfg(unix)]
    +#[test]
    +fn test_streaming_char_device_from_infinite_source() {
    +    use std::process::Stdio;
    +    use std::time::Duration;
    +
    +    let mut cmd = new_ucmd!();
    +    cmd.timeout(Duration::from_secs(5));
    +
    +    let mut child = cmd
    +        .arg("/dev/zero")
    +        .set_stdout(Stdio::piped())
    +        .run_no_wait();
    +
    +    // `pr` should start writing promptly and terminate quietly on a closed pipe.
    +    child.close_stdout();
    +    child.wait().unwrap().fails_silently();
    +}
    +
    +#[cfg(unix)]
    +#[test]
    +fn test_streaming_fifo_with_invalid_utf8_fails() {
    +    use std::time::Duration;
    +
    +    let (at, mut ucmd) = at_and_ucmd!();
    +    let fifo_path = at.plus_as_string("fifo");
    +
    +    nix::unistd::mkfifo(fifo_path.as_str(), nix::sys::stat::Mode::S_IRWXU).unwrap();
    +
    +    let writer_path = at.plus("fifo");
    +    let writer = std::thread::spawn(move || {
    +        std::fs::write(writer_path, [0xFF_u8, b'\n']).unwrap();
    +    });
    +
    +    ucmd.timeout(Duration::from_secs(5));
    +    ucmd.arg("fifo")
    +        .fails_with_code(1)
    +        .stdout_is("")
    +        .stderr_contains("invalid utf-8 sequence");
    +
    +    writer.join().unwrap();
    +}
    +
    +#[cfg(unix)]
    +#[test]
    +fn test_streaming_fifo_falls_back_when_page_has_no_content_lines() {

    Both parity tests land in the lines_to_read_for_page == 0 fallback, so neither actually runs the streaming code. Add a case that streams a real multi-page layout (e.g. -h H -l 5 over ~20 lines) and compares file vs FIFO output.

    sylvestre sylvestre commented on code on Jun 07, 2026 09:34 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -884,6 +884,280 @@ fn read_to_end(path: &str) -> Result<Vec<u8>, std::io::Error> {
         }
     }
     
    +fn should_use_streaming_pr(path: &str, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && !options.join_lines
    +        && options.line_width.is_none()
    +        && options.offset_spaces.is_empty();
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    path != FILE_STDIN

    Excluding stdin means pr < /dev/zero and cat /dev/zero | pr still read to EOF and OOM; only named device/FIFO paths are fixed, not the pipe case from the issue.

    sylvestre sylvestre commented on code on Jun 07, 2026 09:34 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -884,6 +884,280 @@ fn read_to_end(path: &str) -> Result<Vec<u8>, std::io::Error> {
         }
     }
     
    +fn should_use_streaming_pr(path: &str, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && !options.join_lines
    +        && options.line_width.is_none()
    +        && options.offset_spaces.is_empty();
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    path != FILE_STDIN
    +        && lines_to_read_for_page(options) > 0
    +        && metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +fn page_is_in_range(page: usize, options: &OutputOptions) -> bool {
    +    options.start_page <= page && options.end_page.is_none_or(|end| page <= end)
    +}
    +
    +fn write_stream_page_header(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: usize,
    +) -> Result<(), std::io::Error> {
    +    let line_separator = options.line_separator.as_bytes();
    +    for line in header_content(options, page) {
    +        out.write_all(line.as_bytes())?;
    +        out.write_all(line_separator)?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_stream_page_trailer(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    lines_in_page: usize,
    +) -> Result<(), std::io::Error> {
    +    let content_line_separator = options.content_line_separator.as_bytes();
    +    if !options.form_feed_used {
    +        // `print_page`/`write_columns` emits blank-content separators until
    +        // the page reaches `lines_needed_per_page`.
    +        let lines_needed_per_page = lines_to_read_for_page(options);
    +        for _ in lines_in_page..lines_needed_per_page {
    +            out.write_all(content_line_separator)?;
    +        }
    +    }
    +
    +    let line_separator = options.line_separator.as_bytes();
    +    let trailer = trailer_content(options);
    +    for (index, line) in trailer.iter().enumerate() {
    +        out.write_all(line.as_bytes())?;
    +        if index + 1 != trailer.len() {
    +            out.write_all(line_separator)?;
    +        }
    +    }
    +    out.write_all(options.page_separator_char.as_bytes())?;
    +    Ok(())
    +}
    +
    +fn finish_stream_page(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: &mut usize,
    +    page_active: &mut bool,
    +    page_header_written: &mut bool,
    +    lines_in_page: &mut usize,
    +) -> Result<bool, std::io::Error> {
    +    if *page_active && page_is_in_range(*page, options) {
    +        if !*page_header_written {
    +            write_stream_page_header(out, options, *page)?;
    +            *page_header_written = true;
    +        }
    +        write_stream_page_trailer(out, options, *lines_in_page)?;
    +    }
    +
    +    *page += 1;
    +    *page_active = false;
    +    *page_header_written = false;
    +    *lines_in_page = 0;
    +
    +    Ok(options.end_page.is_some_and(|end| *page > end))
    +}
    +
    +fn validate_utf8_stream_chunk(utf8_tail: &mut Vec<u8>, chunk: &[u8]) -> Result<(), PrError> {
    +    let mut combined;
    +    let data = if utf8_tail.is_empty() {
    +        chunk
    +    } else {
    +        combined = Vec::with_capacity(utf8_tail.len() + chunk.len());
    +        combined.extend_from_slice(utf8_tail);
    +        combined.extend_from_slice(chunk);
    +        combined.as_slice()
    +    };
    +
    +    match std::str::from_utf8(data) {
    +        Ok(_) => {
    +            utf8_tail.clear();
    +            Ok(())
    +        }
    +        Err(err) => {
    +            if err.error_len().is_some() {
    +                return Err(err.into());
    +            }
    +
    +            utf8_tail.clear();
    +            utf8_tail.extend_from_slice(&data[err.valid_up_to()..]);
    +            Ok(())
    +        }
    +    }
    +}
    +
    +fn pr_stream_simple_with_reader<R: Read>(
    +    mut reader: R,
    +    options: &OutputOptions,
    +) -> Result<i32, PrError> {
    +    let content_line_separator = options.content_line_separator.as_bytes();
    +    let lines_needed_per_page = lines_to_read_for_page(options);
    +
    +    let out = stdout();
    +    let mut out = out.lock();
    +
    +    let mut page = 1;
    +    let mut page_active = false;
    +    let mut page_header_written = false;
    +    let mut lines_in_page = 0usize;
    +    let mut saw_non_delimiter_since_last = false;
    +    let mut last_delimiter: Option<u8> = None;
    +    let mut utf8_tail = Vec::new();
    +
    +    let mut buf = [0_u8; 8192];
    +    'read_loop: loop {
    +        let n = reader.read(&mut buf)?;
    +        if n == 0 {
    +            break;
    +        }
    +        validate_utf8_stream_chunk(&mut utf8_tail, &buf[..n])?;
    +
    +        for &byte in &buf[..n] {
    +            if byte == NL {
    +                let ignore_empty_line =
    +                    !saw_non_delimiter_since_last && matches!(last_delimiter, Some(FF));
    +
    +                if !ignore_empty_line {
    +                    if !page_active {
    +                        page_active = true;
    +                        page_header_written = false;
    +                    }
    +
    +                    if page_is_in_range(page, options) {
    +                        if !page_header_written {
    +                            write_stream_page_header(&mut out, options, page)?;
    +                            page_header_written = true;
    +                        }
    +                        out.write_all(content_line_separator)?;
    +                    }
    +                    lines_in_page += 1;
    +                }
    +
    +                saw_non_delimiter_since_last = false;
    +                last_delimiter = Some(NL);
    +
    +                if page_active && lines_in_page >= lines_needed_per_page {
    +                    if finish_stream_page(
    +                        &mut out,
    +                        options,
    +                        &mut page,
    +                        &mut page_active,
    +                        &mut page_header_written,
    +                        &mut lines_in_page,
    +                    )? {
    +                        break 'read_loop;
    +                    }
    +                }
    +                continue;
    +            }
    +
    +            if byte == FF {
    +                let ignore_empty_line =
    +                    !saw_non_delimiter_since_last && matches!(last_delimiter, Some(NL));
    +
    +                if !page_active {
    +                    page_active = true;
    +                    page_header_written = false;
    +                }
    +
    +                if page_is_in_range(page, options) && !page_header_written {
    +                    write_stream_page_header(&mut out, options, page)?;
    +                    page_header_written = true;
    +                }
    +
    +                if !ignore_empty_line {
    +                    if page_is_in_range(page, options) {
    +                        out.write_all(content_line_separator)?;
    +                    }
    +                    lines_in_page += 1;
    +                }
    +
    +                saw_non_delimiter_since_last = false;
    +                last_delimiter = Some(FF);
    +
    +                if finish_stream_page(
    +                    &mut out,
    +                    options,
    +                    &mut page,
    +                    &mut page_active,
    +                    &mut page_header_written,
    +                    &mut lines_in_page,
    +                )? {
    +                    break 'read_loop;
    +                }
    +                continue;
    +            }
    +
    +            if !page_active {
    +                page_active = true;
    +                page_header_written = false;
    +            }
    +
    +            if page_is_in_range(page, options) {
    +                if !page_header_written {
    +                    write_stream_page_header(&mut out, options, page)?;
    +                    page_header_written = true;
    +                }
    +                out.write_all(&[byte])?;

    Byte-at-a-time writes to an unbuffered StdoutLock cost a syscall per byte on input like /dev/zero; wrap the lock in a BufWriter.

    sylvestre sylvestre commented on code on Jun 07, 2026 09:34 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -884,6 +884,280 @@ fn read_to_end(path: &str) -> Result<Vec<u8>, std::io::Error> {
         }
     }
     
    +fn should_use_streaming_pr(path: &str, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && !options.join_lines
    +        && options.line_width.is_none()
    +        && options.offset_spaces.is_empty();
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    path != FILE_STDIN
    +        && lines_to_read_for_page(options) > 0
    +        && metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +fn page_is_in_range(page: usize, options: &OutputOptions) -> bool {
    +    options.start_page <= page && options.end_page.is_none_or(|end| page <= end)
    +}
    +
    +fn write_stream_page_header(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: usize,
    +) -> Result<(), std::io::Error> {
    +    let line_separator = options.line_separator.as_bytes();
    +    for line in header_content(options, page) {
    +        out.write_all(line.as_bytes())?;
    +        out.write_all(line_separator)?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_stream_page_trailer(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    lines_in_page: usize,
    +) -> Result<(), std::io::Error> {
    +    let content_line_separator = options.content_line_separator.as_bytes();
    +    if !options.form_feed_used {
    +        // `print_page`/`write_columns` emits blank-content separators until
    +        // the page reaches `lines_needed_per_page`.
    +        let lines_needed_per_page = lines_to_read_for_page(options);
    +        for _ in lines_in_page..lines_needed_per_page {
    +            out.write_all(content_line_separator)?;
    +        }
    +    }
    +
    +    let line_separator = options.line_separator.as_bytes();
    +    let trailer = trailer_content(options);
    +    for (index, line) in trailer.iter().enumerate() {
    +        out.write_all(line.as_bytes())?;
    +        if index + 1 != trailer.len() {
    +            out.write_all(line_separator)?;
    +        }
    +    }
    +    out.write_all(options.page_separator_char.as_bytes())?;
    +    Ok(())
    +}
    +
    +fn finish_stream_page(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: &mut usize,
    +    page_active: &mut bool,
    +    page_header_written: &mut bool,
    +    lines_in_page: &mut usize,
    +) -> Result<bool, std::io::Error> {
    +    if *page_active && page_is_in_range(*page, options) {
    +        if !*page_header_written {
    +            write_stream_page_header(out, options, *page)?;
    +            *page_header_written = true;
    +        }
    +        write_stream_page_trailer(out, options, *lines_in_page)?;
    +    }
    +
    +    *page += 1;
    +    *page_active = false;
    +    *page_header_written = false;
    +    *lines_in_page = 0;
    +
    +    Ok(options.end_page.is_some_and(|end| *page > end))
    +}
    +
    +fn validate_utf8_stream_chunk(utf8_tail: &mut Vec<u8>, chunk: &[u8]) -> Result<(), PrError> {
    +    let mut combined;
    +    let data = if utf8_tail.is_empty() {
    +        chunk
    +    } else {
    +        combined = Vec::with_capacity(utf8_tail.len() + chunk.len());
    +        combined.extend_from_slice(utf8_tail);
    +        combined.extend_from_slice(chunk);
    +        combined.as_slice()
    +    };
    +
    +    match std::str::from_utf8(data) {
    +        Ok(_) => {
    +            utf8_tail.clear();
    +            Ok(())
    +        }
    +        Err(err) => {
    +            if err.error_len().is_some() {
    +                return Err(err.into());
    +            }
    +
    +            utf8_tail.clear();
    +            utf8_tail.extend_from_slice(&data[err.valid_up_to()..]);
    +            Ok(())
    +        }
    +    }
    +}
    +
    +fn pr_stream_simple_with_reader<R: Read>(

    This re-implements the get_pages/print_page paging, trailer and \n\f logic as a parallel state machine, which will drift from the buffered path as it changes — better to factor the shared paging out than duplicate it.

    mattsu2020 mattsu2020 commented on code on Jun 07, 2026 13:55 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -884,6 +884,280 @@ fn read_to_end(path: &str) -> Result<Vec<u8>, std::io::Error> {
         }
     }
     
    +fn should_use_streaming_pr(path: &str, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && !options.join_lines
    +        && options.line_width.is_none()
    +        && options.offset_spaces.is_empty();
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    path != FILE_STDIN
    +        && lines_to_read_for_page(options) > 0
    +        && metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +fn page_is_in_range(page: usize, options: &OutputOptions) -> bool {
    +    options.start_page <= page && options.end_page.is_none_or(|end| page <= end)
    +}
    +
    +fn write_stream_page_header(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: usize,
    +) -> Result<(), std::io::Error> {
    +    let line_separator = options.line_separator.as_bytes();
    +    for line in header_content(options, page) {
    +        out.write_all(line.as_bytes())?;
    +        out.write_all(line_separator)?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_stream_page_trailer(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    lines_in_page: usize,
    +) -> Result<(), std::io::Error> {
    +    let content_line_separator = options.content_line_separator.as_bytes();
    +    if !options.form_feed_used {
    +        // `print_page`/`write_columns` emits blank-content separators until
    +        // the page reaches `lines_needed_per_page`.
    +        let lines_needed_per_page = lines_to_read_for_page(options);
    +        for _ in lines_in_page..lines_needed_per_page {
    +            out.write_all(content_line_separator)?;
    +        }
    +    }
    +
    +    let line_separator = options.line_separator.as_bytes();
    +    let trailer = trailer_content(options);
    +    for (index, line) in trailer.iter().enumerate() {
    +        out.write_all(line.as_bytes())?;
    +        if index + 1 != trailer.len() {
    +            out.write_all(line_separator)?;
    +        }
    +    }
    +    out.write_all(options.page_separator_char.as_bytes())?;
    +    Ok(())
    +}
    +
    +fn finish_stream_page(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: &mut usize,
    +    page_active: &mut bool,
    +    page_header_written: &mut bool,
    +    lines_in_page: &mut usize,
    +) -> Result<bool, std::io::Error> {
    +    if *page_active && page_is_in_range(*page, options) {
    +        if !*page_header_written {
    +            write_stream_page_header(out, options, *page)?;
    +            *page_header_written = true;
    +        }
    +        write_stream_page_trailer(out, options, *lines_in_page)?;
    +    }
    +
    +    *page += 1;
    +    *page_active = false;
    +    *page_header_written = false;
    +    *lines_in_page = 0;
    +
    +    Ok(options.end_page.is_some_and(|end| *page > end))
    +}
    +
    +fn validate_utf8_stream_chunk(utf8_tail: &mut Vec<u8>, chunk: &[u8]) -> Result<(), PrError> {
    +    let mut combined;
    +    let data = if utf8_tail.is_empty() {
    +        chunk
    +    } else {
    +        combined = Vec::with_capacity(utf8_tail.len() + chunk.len());
    +        combined.extend_from_slice(utf8_tail);
    +        combined.extend_from_slice(chunk);
    +        combined.as_slice()
    +    };
    +
    +    match std::str::from_utf8(data) {
    +        Ok(_) => {
    +            utf8_tail.clear();
    +            Ok(())
    +        }
    +        Err(err) => {
    +            if err.error_len().is_some() {

    I'll make the correction

    mattsu2020 mattsu2020 commented on code on Jun 07, 2026 13:58 UTC
    Code Review
    tests/by-util/test_pr.rs:1
    @@ -566,6 +598,61 @@ fn test_pr_char_device_dev_null() {
         new_ucmd!().arg("/dev/null").succeeds();
     }
     
    +#[cfg(unix)]
    +#[test]
    +fn test_streaming_char_device_from_infinite_source() {
    +    use std::process::Stdio;
    +    use std::time::Duration;
    +
    +    let mut cmd = new_ucmd!();
    +    cmd.timeout(Duration::from_secs(5));
    +
    +    let mut child = cmd
    +        .arg("/dev/zero")
    +        .set_stdout(Stdio::piped())
    +        .run_no_wait();
    +
    +    // `pr` should start writing promptly and terminate quietly on a closed pipe.
    +    child.close_stdout();
    +    child.wait().unwrap().fails_silently();
    +}
    +
    +#[cfg(unix)]
    +#[test]
    +fn test_streaming_fifo_with_invalid_utf8_fails() {
    +    use std::time::Duration;
    +
    +    let (at, mut ucmd) = at_and_ucmd!();
    +    let fifo_path = at.plus_as_string("fifo");
    +
    +    nix::unistd::mkfifo(fifo_path.as_str(), nix::sys::stat::Mode::S_IRWXU).unwrap();
    +
    +    let writer_path = at.plus("fifo");
    +    let writer = std::thread::spawn(move || {
    +        std::fs::write(writer_path, [0xFF_u8, b'\n']).unwrap();
    +    });
    +
    +    ucmd.timeout(Duration::from_secs(5));
    +    ucmd.arg("fifo")
    +        .fails_with_code(1)
    +        .stdout_is("")
    +        .stderr_contains("invalid utf-8 sequence");
    +
    +    writer.join().unwrap();
    +}
    +
    +#[cfg(unix)]
    +#[test]
    +fn test_streaming_fifo_falls_back_when_page_has_no_content_lines() {

    I'll make the correction

    mattsu2020 mattsu2020 commented on code on Jun 07, 2026 13:58 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -884,6 +884,280 @@ fn read_to_end(path: &str) -> Result<Vec<u8>, std::io::Error> {
         }
     }
     
    +fn should_use_streaming_pr(path: &str, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && !options.join_lines
    +        && options.line_width.is_none()
    +        && options.offset_spaces.is_empty();
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    path != FILE_STDIN

    I'll make the correction

    mattsu2020 mattsu2020 commented on code on Jun 07, 2026 13:58 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -884,6 +884,280 @@ fn read_to_end(path: &str) -> Result<Vec<u8>, std::io::Error> {
         }
     }
     
    +fn should_use_streaming_pr(path: &str, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && !options.join_lines
    +        && options.line_width.is_none()
    +        && options.offset_spaces.is_empty();
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    path != FILE_STDIN
    +        && lines_to_read_for_page(options) > 0
    +        && metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +fn page_is_in_range(page: usize, options: &OutputOptions) -> bool {
    +    options.start_page <= page && options.end_page.is_none_or(|end| page <= end)
    +}
    +
    +fn write_stream_page_header(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: usize,
    +) -> Result<(), std::io::Error> {
    +    let line_separator = options.line_separator.as_bytes();
    +    for line in header_content(options, page) {
    +        out.write_all(line.as_bytes())?;
    +        out.write_all(line_separator)?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_stream_page_trailer(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    lines_in_page: usize,
    +) -> Result<(), std::io::Error> {
    +    let content_line_separator = options.content_line_separator.as_bytes();
    +    if !options.form_feed_used {
    +        // `print_page`/`write_columns` emits blank-content separators until
    +        // the page reaches `lines_needed_per_page`.
    +        let lines_needed_per_page = lines_to_read_for_page(options);
    +        for _ in lines_in_page..lines_needed_per_page {
    +            out.write_all(content_line_separator)?;
    +        }
    +    }
    +
    +    let line_separator = options.line_separator.as_bytes();
    +    let trailer = trailer_content(options);
    +    for (index, line) in trailer.iter().enumerate() {
    +        out.write_all(line.as_bytes())?;
    +        if index + 1 != trailer.len() {
    +            out.write_all(line_separator)?;
    +        }
    +    }
    +    out.write_all(options.page_separator_char.as_bytes())?;
    +    Ok(())
    +}
    +
    +fn finish_stream_page(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: &mut usize,
    +    page_active: &mut bool,
    +    page_header_written: &mut bool,
    +    lines_in_page: &mut usize,
    +) -> Result<bool, std::io::Error> {
    +    if *page_active && page_is_in_range(*page, options) {
    +        if !*page_header_written {
    +            write_stream_page_header(out, options, *page)?;
    +            *page_header_written = true;
    +        }
    +        write_stream_page_trailer(out, options, *lines_in_page)?;
    +    }
    +
    +    *page += 1;
    +    *page_active = false;
    +    *page_header_written = false;
    +    *lines_in_page = 0;
    +
    +    Ok(options.end_page.is_some_and(|end| *page > end))
    +}
    +
    +fn validate_utf8_stream_chunk(utf8_tail: &mut Vec<u8>, chunk: &[u8]) -> Result<(), PrError> {
    +    let mut combined;
    +    let data = if utf8_tail.is_empty() {
    +        chunk
    +    } else {
    +        combined = Vec::with_capacity(utf8_tail.len() + chunk.len());
    +        combined.extend_from_slice(utf8_tail);
    +        combined.extend_from_slice(chunk);
    +        combined.as_slice()
    +    };
    +
    +    match std::str::from_utf8(data) {
    +        Ok(_) => {
    +            utf8_tail.clear();
    +            Ok(())
    +        }
    +        Err(err) => {
    +            if err.error_len().is_some() {
    +                return Err(err.into());
    +            }
    +
    +            utf8_tail.clear();
    +            utf8_tail.extend_from_slice(&data[err.valid_up_to()..]);
    +            Ok(())
    +        }
    +    }
    +}
    +
    +fn pr_stream_simple_with_reader<R: Read>(
    +    mut reader: R,
    +    options: &OutputOptions,
    +) -> Result<i32, PrError> {
    +    let content_line_separator = options.content_line_separator.as_bytes();
    +    let lines_needed_per_page = lines_to_read_for_page(options);
    +
    +    let out = stdout();
    +    let mut out = out.lock();
    +
    +    let mut page = 1;
    +    let mut page_active = false;
    +    let mut page_header_written = false;
    +    let mut lines_in_page = 0usize;
    +    let mut saw_non_delimiter_since_last = false;
    +    let mut last_delimiter: Option<u8> = None;
    +    let mut utf8_tail = Vec::new();
    +
    +    let mut buf = [0_u8; 8192];
    +    'read_loop: loop {
    +        let n = reader.read(&mut buf)?;
    +        if n == 0 {
    +            break;
    +        }
    +        validate_utf8_stream_chunk(&mut utf8_tail, &buf[..n])?;
    +
    +        for &byte in &buf[..n] {
    +            if byte == NL {
    +                let ignore_empty_line =
    +                    !saw_non_delimiter_since_last && matches!(last_delimiter, Some(FF));
    +
    +                if !ignore_empty_line {
    +                    if !page_active {
    +                        page_active = true;
    +                        page_header_written = false;
    +                    }
    +
    +                    if page_is_in_range(page, options) {
    +                        if !page_header_written {
    +                            write_stream_page_header(&mut out, options, page)?;
    +                            page_header_written = true;
    +                        }
    +                        out.write_all(content_line_separator)?;
    +                    }
    +                    lines_in_page += 1;
    +                }
    +
    +                saw_non_delimiter_since_last = false;
    +                last_delimiter = Some(NL);
    +
    +                if page_active && lines_in_page >= lines_needed_per_page {
    +                    if finish_stream_page(
    +                        &mut out,
    +                        options,
    +                        &mut page,
    +                        &mut page_active,
    +                        &mut page_header_written,
    +                        &mut lines_in_page,
    +                    )? {
    +                        break 'read_loop;
    +                    }
    +                }
    +                continue;
    +            }
    +
    +            if byte == FF {
    +                let ignore_empty_line =
    +                    !saw_non_delimiter_since_last && matches!(last_delimiter, Some(NL));
    +
    +                if !page_active {
    +                    page_active = true;
    +                    page_header_written = false;
    +                }
    +
    +                if page_is_in_range(page, options) && !page_header_written {
    +                    write_stream_page_header(&mut out, options, page)?;
    +                    page_header_written = true;
    +                }
    +
    +                if !ignore_empty_line {
    +                    if page_is_in_range(page, options) {
    +                        out.write_all(content_line_separator)?;
    +                    }
    +                    lines_in_page += 1;
    +                }
    +
    +                saw_non_delimiter_since_last = false;
    +                last_delimiter = Some(FF);
    +
    +                if finish_stream_page(
    +                    &mut out,
    +                    options,
    +                    &mut page,
    +                    &mut page_active,
    +                    &mut page_header_written,
    +                    &mut lines_in_page,
    +                )? {
    +                    break 'read_loop;
    +                }
    +                continue;
    +            }
    +
    +            if !page_active {
    +                page_active = true;
    +                page_header_written = false;
    +            }
    +
    +            if page_is_in_range(page, options) {
    +                if !page_header_written {
    +                    write_stream_page_header(&mut out, options, page)?;
    +                    page_header_written = true;
    +                }
    +                out.write_all(&[byte])?;

    I definitely agree

    mattsu2020 mattsu2020 commented on code on Jun 07, 2026 14:00 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -884,6 +884,280 @@ fn read_to_end(path: &str) -> Result<Vec<u8>, std::io::Error> {
         }
     }
     
    +fn should_use_streaming_pr(path: &str, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && !options.join_lines
    +        && options.line_width.is_none()
    +        && options.offset_spaces.is_empty();
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    path != FILE_STDIN
    +        && lines_to_read_for_page(options) > 0
    +        && metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +fn page_is_in_range(page: usize, options: &OutputOptions) -> bool {
    +    options.start_page <= page && options.end_page.is_none_or(|end| page <= end)
    +}
    +
    +fn write_stream_page_header(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: usize,
    +) -> Result<(), std::io::Error> {
    +    let line_separator = options.line_separator.as_bytes();
    +    for line in header_content(options, page) {
    +        out.write_all(line.as_bytes())?;
    +        out.write_all(line_separator)?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_stream_page_trailer(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    lines_in_page: usize,
    +) -> Result<(), std::io::Error> {
    +    let content_line_separator = options.content_line_separator.as_bytes();
    +    if !options.form_feed_used {
    +        // `print_page`/`write_columns` emits blank-content separators until
    +        // the page reaches `lines_needed_per_page`.
    +        let lines_needed_per_page = lines_to_read_for_page(options);
    +        for _ in lines_in_page..lines_needed_per_page {
    +            out.write_all(content_line_separator)?;
    +        }
    +    }
    +
    +    let line_separator = options.line_separator.as_bytes();
    +    let trailer = trailer_content(options);
    +    for (index, line) in trailer.iter().enumerate() {
    +        out.write_all(line.as_bytes())?;
    +        if index + 1 != trailer.len() {
    +            out.write_all(line_separator)?;
    +        }
    +    }
    +    out.write_all(options.page_separator_char.as_bytes())?;
    +    Ok(())
    +}
    +
    +fn finish_stream_page(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: &mut usize,
    +    page_active: &mut bool,
    +    page_header_written: &mut bool,
    +    lines_in_page: &mut usize,
    +) -> Result<bool, std::io::Error> {
    +    if *page_active && page_is_in_range(*page, options) {
    +        if !*page_header_written {
    +            write_stream_page_header(out, options, *page)?;
    +            *page_header_written = true;
    +        }
    +        write_stream_page_trailer(out, options, *lines_in_page)?;
    +    }
    +
    +    *page += 1;
    +    *page_active = false;
    +    *page_header_written = false;
    +    *lines_in_page = 0;
    +
    +    Ok(options.end_page.is_some_and(|end| *page > end))
    +}
    +
    +fn validate_utf8_stream_chunk(utf8_tail: &mut Vec<u8>, chunk: &[u8]) -> Result<(), PrError> {
    +    let mut combined;
    +    let data = if utf8_tail.is_empty() {
    +        chunk
    +    } else {
    +        combined = Vec::with_capacity(utf8_tail.len() + chunk.len());
    +        combined.extend_from_slice(utf8_tail);
    +        combined.extend_from_slice(chunk);
    +        combined.as_slice()
    +    };
    +
    +    match std::str::from_utf8(data) {
    +        Ok(_) => {
    +            utf8_tail.clear();
    +            Ok(())
    +        }
    +        Err(err) => {
    +            if err.error_len().is_some() {
    +                return Err(err.into());
    +            }
    +
    +            utf8_tail.clear();
    +            utf8_tail.extend_from_slice(&data[err.valid_up_to()..]);
    +            Ok(())
    +        }
    +    }
    +}
    +
    +fn pr_stream_simple_with_reader<R: Read>(

    I will keep the source code separate.

    oech3 oech3 commented on Jul 20, 2026 23:13 UTC
    Contributor

    Conflicting

    MuntasirSZN MuntasirSZN commented on Aug 06, 2026 05:21 UTC
    Contributor

    @mattsu2020 still conflicting

    MuntasirSZN MuntasirSZN commented on Aug 10, 2026 02:31 UTC
    Contributor

    @mattsu2020 Some clippy lints failing...

    MuntasirSZN MuntasirSZN commented on Nov 23, 2026 08:15 UTC
    Contributor

    @sylvestre @cakebaker Can you please take a look at this pr?

    sylvestre sylvestre commented on code on Nov 23, 2026 09:10 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -1145,6 +1158,162 @@ fn read_to_end(name: &OsStr) -> Result<Vec<u8>, PrError> {
         }
     }
     
    +fn should_use_streaming_pr(path: &OsStr, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && options.line_width.is_none()
    +        && options.offset_spaces == 0;
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    if path == FILE_STDIN {
    +        return should_stream_stdin();
    +    }
    +
    +    metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +#[cfg(unix)]
    +fn should_stream_stdin() -> bool {
    +    rustix::fs::fstat(stdin().as_fd())
    +        .is_ok_and(|stat| !rustix::fs::FileType::from_raw_mode(stat.st_mode).is_file())
    +}
    +
    +#[cfg(not(unix))]
    +fn should_stream_stdin() -> bool {
    +    false
    +}
    +
    +fn write_page_header(out: &mut impl Write, options: &OutputOptions, page: usize) -> io::Result<()> {
    +    let line_separator = options.line_separator.as_bytes();
    +    if let Some(header_line) = header_content(options, page) {
    +        // The left margin goes on the first blank line and the header text.
    +        write_offset_spaces(out, options.offset_spaces)?;
    +        out.write_all(line_separator)?;
    +        out.write_all(line_separator)?;
    +        write_offset_spaces(out, options.offset_spaces)?;
    +        out.write_all(&header_line)?;
    +        out.write_all(line_separator)?;
    +        out.write_all(line_separator)?;
    +        out.write_all(line_separator)?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_page_trailer(out: &mut impl Write, options: &OutputOptions) -> io::Result<()> {
    +    let line_separator = options.line_separator.as_bytes();
    +    let trailer = trailer_content(options);
    +    for (index, line) in trailer.iter().enumerate() {
    +        out.write_all(line.as_bytes())?;
    +        if index + 1 != trailer.len() {
    +            out.write_all(line_separator)?;
    +        }
    +    }
    +    if options.display_header_and_trailer || options.form_feed_used {
    +        out.write_all(options.page_separator_char.as_bytes())?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_stream_page(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: &InputPage,
    +    streamed_lines: Option<usize>,
    +) -> io::Result<()> {
    +    if streamed_lines.is_none() {
    +        write_page_header(out, options, page.page_number + 1)?;
    +    }
    +    write_stream_lines(out, options, &page.lines[streamed_lines.unwrap_or(0)..])?;
    +
    +    if options.display_header_and_trailer && !options.form_feed_used {
    +        // Pad the final rows just as write_columns does for buffered pages.
    +        for _ in page.lines.len()..text_lines_per_page(options) {
    +            out.write_all(options.content_line_separator.as_bytes())?;
    +        }
    +    }
    +    write_page_trailer(out, options)
    +}
    +
    +fn write_stream_lines(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    lines: &[PageLine],
    +) -> io::Result<()> {
    +    for line in lines {
    +        out.write_all(&line.content)?;
    +        out.write_all(content_line_ending(options, line.unterminated))?;
    +    }
    +    Ok(())
    +}
    +
    +fn pr_stream_simple_with_reader<R: Read>(
    +    mut reader: R,
    +    options: &OutputOptions,
    +) -> Result<i32, PrError> {
    +    let out = stdout();
    +    let mut out = BufWriter::new(out.lock());
    +    let mut page_builder = PageBuilder::new(options);
    +    // Some means the header and this many complete lines have been written.
    +    let mut streamed_lines = None;
    +
    +    let mut buf = [0_u8; 8192];
    +    loop {
    +        let n = reader.read(&mut buf).map_err(PrError::Read)?;

    read errors lose the file name here: pr /tmp now prints pr: Is a directory instead of pr: /tmp: Is a directory.
    could you please map it to ReadPath (or keep directories on the old path) and add a test?

    sylvestre sylvestre commented on code on Nov 23, 2026 09:10 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -1145,6 +1158,162 @@ fn read_to_end(name: &OsStr) -> Result<Vec<u8>, PrError> {
         }
     }
     
    +fn should_use_streaming_pr(path: &OsStr, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && options.line_width.is_none()
    +        && options.offset_spaces == 0;
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    if path == FILE_STDIN {
    +        return should_stream_stdin();
    +    }
    +
    +    metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +#[cfg(unix)]
    +fn should_stream_stdin() -> bool {
    +    rustix::fs::fstat(stdin().as_fd())
    +        .is_ok_and(|stat| !rustix::fs::FileType::from_raw_mode(stat.st_mode).is_file())
    +}
    +
    +#[cfg(not(unix))]
    +fn should_stream_stdin() -> bool {

    why only unix? type x | pr on windows still reads everything into memory, a comment saying it's a known limitation would be fine

    mattsu2020 mattsu2020 commented on code on Nov 23, 2026 11:41 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -1145,6 +1158,162 @@ fn read_to_end(name: &OsStr) -> Result<Vec<u8>, PrError> {
         }
     }
     
    +fn should_use_streaming_pr(path: &OsStr, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && options.line_width.is_none()
    +        && options.offset_spaces == 0;
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    if path == FILE_STDIN {
    +        return should_stream_stdin();
    +    }
    +
    +    metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +#[cfg(unix)]
    +fn should_stream_stdin() -> bool {
    +    rustix::fs::fstat(stdin().as_fd())
    +        .is_ok_and(|stat| !rustix::fs::FileType::from_raw_mode(stat.st_mode).is_file())
    +}
    +
    +#[cfg(not(unix))]
    +fn should_stream_stdin() -> bool {
    +    false
    +}
    +
    +fn write_page_header(out: &mut impl Write, options: &OutputOptions, page: usize) -> io::Result<()> {
    +    let line_separator = options.line_separator.as_bytes();
    +    if let Some(header_line) = header_content(options, page) {
    +        // The left margin goes on the first blank line and the header text.
    +        write_offset_spaces(out, options.offset_spaces)?;
    +        out.write_all(line_separator)?;
    +        out.write_all(line_separator)?;
    +        write_offset_spaces(out, options.offset_spaces)?;
    +        out.write_all(&header_line)?;
    +        out.write_all(line_separator)?;
    +        out.write_all(line_separator)?;
    +        out.write_all(line_separator)?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_page_trailer(out: &mut impl Write, options: &OutputOptions) -> io::Result<()> {
    +    let line_separator = options.line_separator.as_bytes();
    +    let trailer = trailer_content(options);
    +    for (index, line) in trailer.iter().enumerate() {
    +        out.write_all(line.as_bytes())?;
    +        if index + 1 != trailer.len() {
    +            out.write_all(line_separator)?;
    +        }
    +    }
    +    if options.display_header_and_trailer || options.form_feed_used {
    +        out.write_all(options.page_separator_char.as_bytes())?;
    +    }
    +    Ok(())
    +}
    +
    +fn write_stream_page(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    page: &InputPage,
    +    streamed_lines: Option<usize>,
    +) -> io::Result<()> {
    +    if streamed_lines.is_none() {
    +        write_page_header(out, options, page.page_number + 1)?;
    +    }
    +    write_stream_lines(out, options, &page.lines[streamed_lines.unwrap_or(0)..])?;
    +
    +    if options.display_header_and_trailer && !options.form_feed_used {
    +        // Pad the final rows just as write_columns does for buffered pages.
    +        for _ in page.lines.len()..text_lines_per_page(options) {
    +            out.write_all(options.content_line_separator.as_bytes())?;
    +        }
    +    }
    +    write_page_trailer(out, options)
    +}
    +
    +fn write_stream_lines(
    +    out: &mut impl Write,
    +    options: &OutputOptions,
    +    lines: &[PageLine],
    +) -> io::Result<()> {
    +    for line in lines {
    +        out.write_all(&line.content)?;
    +        out.write_all(content_line_ending(options, line.unterminated))?;
    +    }
    +    Ok(())
    +}
    +
    +fn pr_stream_simple_with_reader<R: Read>(
    +    mut reader: R,
    +    options: &OutputOptions,
    +) -> Result<i32, PrError> {
    +    let out = stdout();
    +    let mut out = BufWriter::new(out.lock());
    +    let mut page_builder = PageBuilder::new(options);
    +    // Some means the header and this many complete lines have been written.
    +    let mut streamed_lines = None;
    +
    +    let mut buf = [0_u8; 8192];
    +    loop {
    +        let n = reader.read(&mut buf).map_err(PrError::Read)?;

    fixed it.added test

    mattsu2020 mattsu2020 commented on code on Nov 23, 2026 11:42 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -1145,6 +1158,162 @@ fn read_to_end(name: &OsStr) -> Result<Vec<u8>, PrError> {
         }
     }
     
    +fn should_use_streaming_pr(path: &OsStr, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && options.line_width.is_none()
    +        && options.offset_spaces == 0;
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    if path == FILE_STDIN {
    +        return should_stream_stdin();
    +    }
    +
    +    metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +#[cfg(unix)]
    +fn should_stream_stdin() -> bool {
    +    rustix::fs::fstat(stdin().as_fd())
    +        .is_ok_and(|stat| !rustix::fs::FileType::from_raw_mode(stat.st_mode).is_file())
    +}
    +
    +#[cfg(not(unix))]
    +fn should_stream_stdin() -> bool {

    Added comments.

    oech3 oech3 commented on code on Nov 23, 2026 11:44 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -1145,6 +1158,162 @@ fn read_to_end(name: &OsStr) -> Result<Vec<u8>, PrError> {
         }
     }
     
    +fn should_use_streaming_pr(path: &OsStr, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && options.line_width.is_none()
    +        && options.offset_spaces == 0;
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    if path == FILE_STDIN {
    +        return should_stream_stdin();
    +    }
    +
    +    metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +#[cfg(unix)]
    +fn should_stream_stdin() -> bool {
    +    rustix::fs::fstat(stdin().as_fd())
    +        .is_ok_and(|stat| !rustix::fs::FileType::from_raw_mode(stat.st_mode).is_file())
    +}
    +
    +#[cfg(not(unix))]
    +fn should_stream_stdin() -> bool {

    How about wasi? rustix has stat for wasi. Also .as_fd() can be omitted.

    mattsu2020 mattsu2020 commented on code on Nov 23, 2026 12:19 UTC
    Code Review
    src/uu/pr/src/pr.rs:1
    @@ -1145,6 +1158,162 @@ fn read_to_end(name: &OsStr) -> Result<Vec<u8>, PrError> {
         }
     }
     
    +fn should_use_streaming_pr(path: &OsStr, options: &OutputOptions) -> bool {
    +    let is_simple_layout = options.number.is_none()
    +        && options.expand_tabs.is_none()
    +        && options.column_mode_options.is_none()
    +        && options.merge_files_print.is_none()
    +        && options.line_width.is_none()
    +        && options.offset_spaces == 0;
    +
    +    if !is_simple_layout {
    +        return false;
    +    }
    +
    +    if path == FILE_STDIN {
    +        return should_stream_stdin();
    +    }
    +
    +    metadata(path).is_ok_and(|meta| !meta.file_type().is_file())
    +}
    +
    +#[cfg(unix)]
    +fn should_stream_stdin() -> bool {
    +    rustix::fs::fstat(stdin().as_fd())
    +        .is_ok_and(|stat| !rustix::fs::FileType::from_raw_mode(stat.st_mode).is_file())
    +}
    +
    +#[cfg(not(unix))]
    +fn should_stream_stdin() -> bool {

    Thanks.
    I think I can write this cleanly.

    Reviewers
    Assignees
    mattsu2020 mattsu2020
    Labels
    risk: low confidence: 86.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 53.6% (MEDIUM) · Higher = larger blast radius
    53.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 86.0% · Lower = rushed or shallow review
    86.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.5% · Safe to merge
    7.5%