Skip to content

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

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

cindykrafft wants to merge 17 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 <[email protected]>
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.

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 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
Resolve the CHANGELOG conflict with sharkdp#4102 and sharkdp#4103: keep their new
entries, keep this PR's sharkdp#4084 entry, and point the UTF-16BE entry back
at sharkdp#4076 (the merge of sharkdp#4102 had filled its placeholder with sharkdp#4102).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
Brings in sharkdp#4038, sharkdp#4048 and sharkdp#4070. The merge of sharkdp#4048 re-added the old
UTF-16BE CHANGELOG entry with its "#XXXX" placeholder at the top of
Bugfixes, so drop that duplicate; the entry this PR corrects to sharkdp#4076
stays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Keep physical line numbers for Git change decorations

The new line_number is the file line, but display_line_number is still passed to every Decoration::generate call. LineChangesDecoration::generate uses that argument as a key into printer.line_changes, which is indexed by physical file line. After a blank line, bat -b --style=changes,numbers therefore looks up the nonblank ordinal and can omit or misplace +/~ markers even though ranges and highlights now use the right line. For example, a change on physical line 3 after a blank line at line 2 is looked up as line 2. Please pass the physical line to the Git decoration and the nonblank count only to LineNumberDecoration, with a regression test combining -b and --style=changes.

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I found one edge case in the new nonblank line counter.

Comment thread src/printer.rs Outdated
// In number-nonblank mode, the displayed number counts non-blank lines only
// (content-free lines that contain only the line terminator are skipped).
// `line_number` stays the line number in the file, which is what line ranges,
// highlighted lines and Git changes refer to.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Detect blank lines after decoding UTF-16. line_buffer still contains the original UTF-16 bytes here, so even an empty UTF-16LE line is 0A 00 (or 0D 00 0A 00) and fails this byte-only check. With bat -b on a UTF-16 file, those blank lines now receive visible line numbers and advance nonblank_line_count; the following nonblank lines are numbered too high. The old printer checked the decoded line before suppressing its number. Please classify blankness from the decoded text (or handle the input encoding here) and cover a UTF-16 file with an interior blank line.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, you are right, and it was a regression from this PR: blank lines were detected on the raw bytes, so the NUL bytes in a UTF-16 terminator made a blank line look non-blank and every later number came out one too high. Fixed in d2d97d4: the check now runs on the decoded line (line.trim_end_matches(['\r', '\n']).is_empty(), as before this PR), so -A also behaves as on master. Added number_nonblank_skips_blank_utf16_lines, which writes UTF-16LE and UTF-16BE files with blank \n and \r\n lines and checks the numbering; it fails without the change. cargo test, cargo fmt --check and cargo clippy --all-targets are clean.

Disclosure: this fix and reply were produced by an AI agent (Claude Code), which ran the tests mentioned.

…changes

Two follow-ups from review:

- With -b, blank lines were detected on the raw line bytes, so a blank
  UTF-16 line (whose terminator contains NUL bytes) got a number and
  shifted every later number. Detect them on the decoded line instead,
  as before this change.
- Only the line number decoration uses the displayed (non-blank) number.
  Other decorations, such as the Git change markers, get the line number
  in the file, which is what their lookups use. The command line enables
  only the numbers column with -b, so this matters when the library
  Config combines number_nonblank with other style components.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
Resolve the CHANGELOG conflict: keep the sharkdp#4111 and sharkdp#4110 entries and
drop the stale UTF-16BE '#XXXX' duplicate (the entry is listed as sharkdp#4076).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
Resolve the src/controller.rs conflict with sharkdp#4118: keep this branch's
plain line counter (blank-line handling for -b now lives in the printer,
which decodes UTF-16 before checking), and drop the ContentType and
UTF_16LE/BE imports that only sharkdp#4118's controller check used.
sharkdp#4118's CHANGELOG entry and number_nonblank_utf16 test are kept and pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo

Copy link
Copy Markdown

I rechecked head cfc0ab5: the updated decoration dispatch uses physical line numbers for Git decorations, and blankness is now checked on decoded text with UTF-16LE and UTF-16BE regression coverage. Those code changes address the two behaviors I raised. I don't see a regression test combining -b with Git change decorations in the current patch; that part of my earlier request remains useful. I reviewed the patch but did not run tests locally. AI-assisted review.

With number_nonblank, the line number column shows the count of non-blank
lines, but the Git change marker must stay on the line that changed in the
file. The command line enables only the numbers column with -b, so the
test builds a library Config with LineNumbers and Changes, commits a file
to a temporary repository and modifies the line after a blank line.
Without the fix in d2d97d4 the marker moves to the following line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
@cindykrafft

Copy link
Copy Markdown
Author

Thanks for rechecking. Added the regression test in 4803929: tests/number_nonblank_changes.rs (behind the git feature) commits a file to a temporary repository, modifies the line after a blank line, and prints it through a library Config with number_nonblank plus the LineNumbers and Changes components. On the command line -b drops the changes column, so the test drives Controller instead of the binary. It checks that the ~ marker stays on the modified line; with the decoration change from d2d97d4 reverted, the marker moves to the next line and the test fails. cargo test, cargo fmt --check and cargo clippy --all-targets are clean.

The per-target build jobs are red on this push for an unrelated reason: their test steps pass (including the new test), then the packaging step fails on cp target/<target>/release/build/bat-*/out/assets/manual/bat.1. Rust 1.100 (stable since 2026-11-09) puts build script output in build/bat/<hash>/out, so that glob in CICD.yml no longer matches; master will hit the same on its next run.

Disclosure: this test and reply were produced by an AI agent (Claude Code), which ran the tests mentioned.

Copy link
Copy Markdown

Thanks for adding the Git decoration regression. I rechecked 4803929: the new test combines number_nonblank with the line-number and Git-change decorations and asserts that the change marker stays on the modified physical line after a blank line. This covers the specific test gap I raised. I reviewed the patch with AI assistance; I did not run the tests locally.

Picks up the CI packaging fix (sharkdp#4128) so the per-target build jobs can
pass again. No conflicts.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo

Copy link
Copy Markdown

I found another numbering case on head 2da6633 involving -A -b. With a file containing alpha\n\nbeta\n:

printf 'alpha\n\nbeta\n' > fixture.txt
bat --no-config --paging=never --color=never --decorations=always -b -A fixture.txt

The PR displays the nonblank beta line as 3; the base displays it as 2. Both display a number beside the blank line's visible newline marker, so the change is that the blank line advances the following line's number. As a control, -b without -A displays beta as 2 on the PR.

The counter appears to check blankness after -A renders the newline marker. Could blankness be determined before that rendering, with an -A -b regression test?

I reproduced this with Windows CI binaries for the PR head and a commit with the same tree as base 599c383, and with a locally built Linux PR binary. I did not run the full suite locally. AI-assisted investigation.

With --show-all, the line terminator is rendered as a visible character,
so the blank-line check on the rendered line never matched and blank
lines were counted: `-b -A` numbered the line after a blank line one too
high. Check the input instead (decoded for UTF-16, raw bytes otherwise),
which is the same condition the controller used before this branch.

Blank lines now also stay unnumbered under `-b -A`, as with `cat -bA`.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo

Copy link
Copy Markdown

I rechecked 6b1f50f on Linux. With the -A -b fixture from my report, the following nonblank line now displays as 2 for both LF and CRLF inputs. This is a focused CLI check; I did not run the full test suite or recheck other platforms on this head. AI-assisted testing.

Brings in sharkdp#4141, which moves line decoding in print_line into
decode_line(). No conflicts; the blank-line check for -b still runs on
the input before the line is rendered.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
v0.27.0 was released, so the changelog entry for this fix moves from
the v0.27.0 section to "unreleased".

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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.

4 participants