Skip to content

feat: implement -b / --number-nonblank for cat -b compatibility - #3857

Merged
keith-hall merged 2 commits into
sharkdp:masterfrom
MeGaurav4:master
Aug 11, 2026
Merged

keith-hall merged 2 commits into
sharkdp:masterfrom
MeGaurav4:master

Conversation

@MeGaurav4

Copy link
Copy Markdown
Contributor

Implements cat -b compatibility via -b / --number-nonblank.

Behaviour

$ printf 'alpha\n\nbeta\n' | bat -b
   1 alpha
     
   2 world
  • Non-blank lines are numbered sequentially
  • Blank lines (empty / newline-only) are printed without a number and
    do not advance the line counter
  • Lines containing spaces or tabs are not blank and are numbered
  • -b takes precedence over -n; both -bn and -nb produce nonblank numbering
  • -bp disables numbering (plain mode wins)

Implementation

Core change is in src/controller.rs: when number_nonblank is set,
the line counter is not incremented for lines whose buffer contains
only \r / \n bytes. In src/printer.rs the decoration engine
receives line_number = 0 as sentinel for those lines, and
LineNumberDecoration::generate returns empty gutter spaces in that
case. The raw-arg scanner in App::new() was extended to detect -b
the same way it detects -n (so it works with combined short flags
like -pb / -bp).

Testing

  • 5 new integration tests: plain -b, -b in loop-through mode,
    -bn/-nb precedence, -bp override, piped stdin
  • All 436 existing tests continue to pass
  • Help-text snapshots regenerated

Closes #3856.

@TyceHerrman

Copy link
Copy Markdown
Contributor

@MeGaurav4 are you still working on this? if not, I can start from your work and credit you on a new pr and fix the failing ci checks

Adds a `-b` / `--number-nonblank` flag that numbers only non-blank
lines.  Blank lines (those containing only the line terminator) are
printed without a number and don't advance the counter, matching
`cat -b` behaviour.

- `-b` implies `--style=numbers` (same as `-n`), but only counts
  non-blank lines
- `-b` takes precedence over `-n`: both `-bn` and `-nb` behave like `-b`
- `-bp` disables numbering (plain mode wins)
- Works with files, stdin, and piped output (like `-n`)

Closes sharkdp#3856.
@MeGaurav4

Copy link
Copy Markdown
Contributor Author

Hey Tyce, thanks for checking in, and thanks for the offer either way. Still working on this: I've merged current master into the branch and fixed the failing cargo fmt check. The cargo audit failure was a stale advisory that's already cleared on master. CI is re-running now, so I'll follow up once it's green.

@keith-hall
keith-hall merged commit b671e53 into sharkdp:master Aug 11, 2026
24 checks passed
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.

Support -b / --number-nonblank for cat compatibility

3 participants