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:
@@ -9,7 +9,7 @@
|
|||||||
- Fix hang when using `--list-themes` with an explicit pager, see #3457 (@abhinavcool42)
|
- Fix hang when using `--list-themes` with an explicit pager, see #3457 (@abhinavcool42)
|
||||||
- Fix negative values of N not being parsed in <N:M> line ranges without `=` flag value separator, see #3442 (@lmmx)
|
- Fix negative values of N not being parsed in <N:M> line ranges without `=` flag value separator, see #3442 (@lmmx)
|
||||||
- Fix broken Docker syntax preventing use of custom assets, see #3476 (@keith-hall)
|
- Fix broken Docker syntax preventing use of custom assets, see #3476 (@keith-hall)
|
||||||
- Fix decorations being applied unexpectedly when piping, breaking cat compatibility. See #3496 (@keith-hall)
|
- Fix decorations being applied unexpectedly when piping. Now only line numbers explicitly required on the command line should be applied in auto decorations mode for `cat` compatibility. See #3496 (@keith-hall)
|
||||||
|
|
||||||
## Other
|
## Other
|
||||||
- Improve README documentation on pager options passed to less, see #3443 (@injust)
|
- Improve README documentation on pager options passed to less, see #3443 (@injust)
|
||||||
|
|||||||
@@ -46,6 +46,10 @@ enum HelpType {
|
|||||||
pub struct App {
|
pub struct App {
|
||||||
pub matches: ArgMatches,
|
pub matches: ArgMatches,
|
||||||
interactive_output: bool,
|
interactive_output: bool,
|
||||||
|
/// True if -n / --number was passed on the command line
|
||||||
|
/// (not from config file or environment variables).
|
||||||
|
/// This is used to honor the flag when piping output, similar to `cat -n`.
|
||||||
|
number_from_cli: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
@@ -54,6 +58,15 @@ impl App {
|
|||||||
let _ = nu_ansi_term::enable_ansi_support();
|
let _ = nu_ansi_term::enable_ansi_support();
|
||||||
|
|
||||||
let interactive_output = std::io::stdout().is_terminal();
|
let interactive_output = std::io::stdout().is_terminal();
|
||||||
|
|
||||||
|
// Check if the -n / --number option was passed on the command line
|
||||||
|
// (before merging with config file and environment variables).
|
||||||
|
// This is needed to honor the -n flag when piping output, similar to `cat -n`.
|
||||||
|
let number_from_cli = wild::args_os().any(|arg| {
|
||||||
|
let arg_str = arg.to_string_lossy();
|
||||||
|
arg_str == "-n" || arg_str == "--number"
|
||||||
|
});
|
||||||
|
|
||||||
let matches = Self::matches(interactive_output)?;
|
let matches = Self::matches(interactive_output)?;
|
||||||
|
|
||||||
if matches.get_flag("help") {
|
if matches.get_flag("help") {
|
||||||
@@ -91,6 +104,7 @@ impl App {
|
|||||||
Ok(App {
|
Ok(App {
|
||||||
matches,
|
matches,
|
||||||
interactive_output,
|
interactive_output,
|
||||||
|
number_from_cli,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -384,7 +398,7 @@ impl App {
|
|||||||
.map(|s| s.as_str())
|
.map(|s| s.as_str())
|
||||||
== Some("always")
|
== Some("always")
|
||||||
|| self.matches.get_flag("force-colorization")
|
|| self.matches.get_flag("force-colorization")
|
||||||
|| self.matches.get_flag("number")),
|
|| self.number_from_cli),
|
||||||
tab_width: self
|
tab_width: self
|
||||||
.matches
|
.matches
|
||||||
.get_one::<String>("tabs")
|
.get_one::<String>("tabs")
|
||||||
|
|||||||
@@ -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");
|
.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]
|
#[test]
|
||||||
fn line_range_2_3() {
|
fn line_range_2_3() {
|
||||||
bat()
|
bat()
|
||||||
@@ -419,8 +430,8 @@ fn piped_output_with_line_numbers_style_flag() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
fn piped_output_with_line_numbers_with_header_grid_style_flag() {
|
fn piped_output_with_line_numbers_with_header_grid_style_flag() {
|
||||||
|
// style ignored because non-interactive
|
||||||
bat()
|
bat()
|
||||||
.arg("--style=header,grid,numbers")
|
.arg("--style=header,grid,numbers")
|
||||||
.write_stdin("hello\nworld\n")
|
.write_stdin("hello\nworld\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user