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

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 #3856.
This commit is contained in:
MeGaurav4 committed 2026-08-11 11:48:10 +05:30
1 parent 78951393e2
commit 061fb216cb
10 files changed
+168 -6

No files matched your search

+8 -1
View File
@@ -738,10 +738,17 @@ impl Printer for InteractivePrinter<'_> {
// Line decorations.
if self.panel_width > 0 {
let display_line_number =
if self.config.number_nonblank && line.trim_end_matches(['\r', '\n']).is_empty() {
0
} else {
line_number
};
let decorations = self
.decorations
.iter()
.map(|d| d.generate(line_number, false, self));
.map(|d| d.generate(display_line_number, false, self));
for deco in decorations {
write!(handle, "{} ", deco.text)?;