Skip to content

Make -r and -H use file line numbers with -b - #4084

Open
cindykrafft wants to merge 3 commits into
sharkdp:masterfrom
cindykrafft:fix/number-nonblank-line-ranges
Open

cindykrafft wants to merge 3 commits into
sharkdp:masterfrom
cindykrafft:fix/number-nonblank-line-ranges

Conversation

@cindykrafft

Copy link
Copy Markdown

With the new -b / --number-nonblank (#3857, unreleased), --line-range and --highlight-line address the non-blank line count instead of file lines, so the same range shows different lines than with -n:

$ printf 'one\n\nthree\n\nfive\nsix\n' > f.txt
$ bat -n -r 3:5 --style=numbers f.txt     $ bat -b -r 3:5 --style=numbers f.txt
   3 three                                   
   4                                         3 five
   5 five                                    4 six

and bat -b -H 5 f.txt highlights nothing (there is no 5th non-blank line) instead of five. cat -b only changes the displayed numbers, and neither #3857 nor #3856 mentions ranges or highlights, so I assumed this wasn't intended; happy to be told otherwise.

Cause: in src/controller.rs the line counter skipped blank lines in -b mode, but the same counter drives the range and highlight checks (and the line-1 BOM strip). This counts file lines again and keeps a separate non-blank counter in the printer for the displayed number, so -b output is unchanged without -r/-H.

Tests: number_nonblank_line_range_uses_file_line_numbers and number_nonblank_highlight_line_uses_file_line_numbers in tests/integration_tests.rs fail on master and pass with the change; the existing -b tests are unchanged. Full cargo test (458 passed), cargo fmt --check and cargo clippy --all-targets are clean. The CHANGELOG also fills in #4076 for my earlier entry, which was merged with a placeholder.

Disclosure: this was found, reproduced and patched by an AI agent (Claude Code), which ran the reproduction and tests quoted above. It has not been separately reviewed by a human.

With --number-nonblank the controller's line counter skipped blank lines,
and that counter also drives --line-range, --highlight-line and the line-1
BOM strip, so ranges and highlights addressed non-blank line numbers. Count
file lines again and keep a separate non-blank count in the printer for the
displayed number. Also fill in sharkdp#4076 in its CHANGELOG entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
@hafizahmedzia

hafizahmedzia commented Oct 19, 2026 •

Copy link
Copy Markdown

Additional CLI checks of @cindykrafft's fix compared f748c1a467ae161682849949a7d036ab8362f9ba with 59a29e312bcf555b0fd289e8664dda467ec41bc6 on Linux, using Rust/Cargo 1.99.1 and the committed lockfile.

A 14-line fixture has nonblank content on physical lines 1, 3, 6, 7, 9, 11 and 14. With -b -r 1:3 -r 9:11 -H 11, the parent selects using nonblank positions and misses the second physical range/highlight. The PR head matches the -n physical-content/highlight oracle while displaying nonblank ordinals 1, 2, 5 and 6, including counting across discarded lines.

For -r ':-2' -H 11 and -r '-5:' -H 14, selected content already matches the physical oracle on the parent in this fixture, but the highlight is absent. The head preserves that content and highlights the requested physical line. LF-file and CRLF-stdin variants agree; no-range controls preserve numbering 1–7 on both.

All 16 comparisons (32 CLI processes) produced these expected results. Paging/config loading were disabled and an ANSI theme exposed highlighting. Clean package builds avoided cached same-version base-library reuse. This is focused CLI evidence; the full suite, fmt/clippy, CI and other platforms were not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
Resolve the CHANGELOG conflict by keeping the new sharkdp#4091 entry along
with the filled-in sharkdp#4076 and sharkdp#4084 entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo

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.

3 participants