1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-10-20 18:53:53 +01:00

feat(pipe-style): make output pipeable (any style)

This commit is contained in:
Louis Maddox
2025-10-16 13:27:11 +01:00
parent f1a0d8b3f7
commit 7eedc0f854
3 changed files with 81 additions and 1 deletions

View File

@@ -24,6 +24,7 @@
- `--style=changes` no longer prints a two-space indent when the file is unmodified, see issue #2710 and PR #3406 (@jyn514)
- Add missing shell completions, see #3411 (@keith-hall)
- Execute help/version/diagnostic commands even with invalid config/arguments present, see #3414 (@keith-hall)
- Fixed line numbers (`-n`) and style components not printing when piping output, see issue #2935 and PR #3438 (@lmmx)
## Other

View File

@@ -276,7 +276,9 @@ impl App {
.get_one::<String>("decorations")
.map(|s| s.as_str())
== Some("always")
|| self.matches.get_flag("force-colorization")),
|| self.matches.get_flag("force-colorization")
|| self.matches.get_flag("number")
|| self.matches.contains_id("style") && !style_components.plain()),
tab_width: self
.matches
.get_one::<String>("tabs")

View File

@@ -378,6 +378,83 @@ fn line_range_context_very_large() {
);
}
#[test]
fn piped_output_with_implicit_auto_style() {
bat()
.write_stdin("hello\nworld\n")
.assert()
.success()
.stdout("hello\nworld\n");
}
#[test]
fn piped_output_with_line_number_flag() {
bat()
.arg("--number")
.write_stdin("hello\nworld\n")
.assert()
.success()
.stdout(" 1 hello\n 2 world\n");
}
#[test]
fn piped_output_with_line_numbers_style_flag() {
bat()
.arg("--style=numbers")
.write_stdin("hello\nworld\n")
.assert()
.success()
.stdout(" 1 hello\n 2 world\n");
}
#[test]
#[cfg(not(target_os = "windows"))]
fn piped_output_with_line_numbers_with_header_grid_style_flag() {
bat()
.arg("--style=header,grid,numbers")
.write_stdin("hello\nworld\n")
.assert()
.success()
.stdout(
"─────┬──────────────────────────────────────────────────────────────────────────
│ STDIN
─────┼──────────────────────────────────────────────────────────────────────────
1 │ hello
2 │ world
─────┴──────────────────────────────────────────────────────────────────────────
",
);
}
#[test]
fn piped_output_with_auto_style() {
bat()
.arg("--style=auto")
.write_stdin("hello\nworld\n")
.assert()
.success()
.stdout("hello\nworld\n"); // Should be plain when piped
}
#[test]
#[cfg(not(target_os = "windows"))]
fn piped_output_with_default_style_flag() {
bat()
.arg("--style=default")
.write_stdin("hello\nworld\n")
.assert()
.success()
.stdout(
"─────┬──────────────────────────────────────────────────────────────────────────
│ STDIN
─────┼──────────────────────────────────────────────────────────────────────────
1 │ hello
2 │ world
─────┴──────────────────────────────────────────────────────────────────────────
",
);
}
#[test]
fn squeeze_blank() {
bat()