1
0
mirror of https://github.com/sharkdp/bat.git synced 2026-02-08 00:32:08 +00:00

Fix -n flag to show line numbers in loop-through mode

When the -n/--number flag is passed on the command line, bat now shows
line numbers even when piping output to another process (loop-through
mode), similar to how `cat -n` behaves.

This change detects if -n or --number was passed on the CLI (before
merging with config file and environment variables) and disables
loop-through mode in that case, allowing the InteractivePrinter to
add line numbers.

The existing behavior is preserved:
- Styles from config/env are still ignored when piping (unless --decorations=always is set)
- Only the -n flag from CLI enables line numbers in piped mode
- -p and --style options from CLI do not disable loop-through mode
This commit is contained in:
Keith Hall
2025-11-29 17:21:37 +02:00
parent f9c33971d9
commit abc7261488
3 changed files with 28 additions and 3 deletions

View File

@@ -166,6 +166,17 @@ fn line_numbers() {
.stdout(" 1 line 1\n 2 line 2\n 3 line 3\n 4 line 4\n 5 line 5\n 6 line 6\n 7 line 7\n 8 line 8\n 9 line 9\n 10 line 10\n");
}
// Test that -n on command line shows line numbers even when piping (similar to `cat -n`)
#[test]
fn line_numbers_from_cli_in_loop_through_mode() {
bat()
.arg("multiline.txt")
.arg("-n")
.assert()
.success()
.stdout(" 1 line 1\n 2 line 2\n 3 line 3\n 4 line 4\n 5 line 5\n 6 line 6\n 7 line 7\n 8 line 8\n 9 line 9\n 10 line 10\n");
}
#[test]
fn line_range_2_3() {
bat()
@@ -419,8 +430,8 @@ fn piped_output_with_line_numbers_style_flag() {
}
#[test]
#[cfg(not(target_os = "windows"))]
fn piped_output_with_line_numbers_with_header_grid_style_flag() {
// style ignored because non-interactive
bat()
.arg("--style=header,grid,numbers")
.write_stdin("hello\nworld\n")