mirror of
https://github.com/sharkdp/bat.git
synced 2025-10-21 11:13:54 +01:00
Merge pull request #3438 from lmmx/pipe-style
feat: make output pipeable with `-n`, non-auto styles
This commit is contained in:
@@ -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)
|
- `--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)
|
- Add missing shell completions, see #3411 (@keith-hall)
|
||||||
- Execute help/version/diagnostic commands even with invalid config/arguments present, see #3414 (@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
|
## Other
|
||||||
|
|
||||||
|
6
assets/manual/bat.1.in
vendored
6
assets/manual/bat.1.in
vendored
@@ -14,7 +14,8 @@ It also communicates with git(1) to show modifications with respect to the git i
|
|||||||
|
|
||||||
Whenever the output of {{PROJECT_EXECUTABLE}} goes to a non-interactive terminal, i.e. when the
|
Whenever the output of {{PROJECT_EXECUTABLE}} goes to a non-interactive terminal, i.e. when the
|
||||||
output is piped into another process or into a file, {{PROJECT_EXECUTABLE}} will act as a drop-in
|
output is piped into another process or into a file, {{PROJECT_EXECUTABLE}} will act as a drop-in
|
||||||
replacement for cat(1) and fall back to printing the plain file contents.
|
replacement for cat(1) and fall back to printing the plain file contents,
|
||||||
|
unless an explicit style is requested.
|
||||||
|
|
||||||
.SH "OPTIONS"
|
.SH "OPTIONS"
|
||||||
General remarks: Command-line options like '-l'/'--language' that take values can be specified as
|
General remarks: Command-line options like '-l'/'--language' that take values can be specified as
|
||||||
@@ -131,7 +132,8 @@ always, *never*.
|
|||||||
\fB\-\-decorations\fR <when>
|
\fB\-\-decorations\fR <when>
|
||||||
.IP
|
.IP
|
||||||
Specify when to use the decorations that have been specified via '\-\-style'. The
|
Specify when to use the decorations that have been specified via '\-\-style'. The
|
||||||
automatic mode only enables decorations if an interactive terminal is detected. Possible
|
automatic mode only enables decorations if an interactive terminal is detected. The
|
||||||
|
always mode will show decorations even when piping output. Possible
|
||||||
values: *auto*, never, always.
|
values: *auto*, never, always.
|
||||||
.HP
|
.HP
|
||||||
\fB\-f\fR, \fB\-\-force\-colorization\fR
|
\fB\-f\fR, \fB\-\-force\-colorization\fR
|
||||||
|
@@ -87,8 +87,8 @@ Options:
|
|||||||
|
|
||||||
--decorations <when>
|
--decorations <when>
|
||||||
Specify when to use the decorations that have been specified via '--style'. The automatic
|
Specify when to use the decorations that have been specified via '--style'. The automatic
|
||||||
mode only enables decorations if an interactive terminal is detected. Possible values:
|
mode only enables decorations if an interactive terminal is detected. The always mode will
|
||||||
*auto*, never, always.
|
show decorations even when piping output. Possible values: *auto*, never, always.
|
||||||
|
|
||||||
-f, --force-colorization
|
-f, --force-colorization
|
||||||
Alias for '--decorations=always --color=always'. This is useful if the output of bat is
|
Alias for '--decorations=always --color=always'. This is useful if the output of bat is
|
||||||
|
@@ -276,7 +276,9 @@ impl App {
|
|||||||
.get_one::<String>("decorations")
|
.get_one::<String>("decorations")
|
||||||
.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.matches.contains_id("style") && !style_components.plain()),
|
||||||
tab_width: self
|
tab_width: self
|
||||||
.matches
|
.matches
|
||||||
.get_one::<String>("tabs")
|
.get_one::<String>("tabs")
|
||||||
|
@@ -300,8 +300,9 @@ pub fn build_app(interactive_output: bool) -> Command {
|
|||||||
.long_help(
|
.long_help(
|
||||||
"Specify when to use the decorations that have been specified \
|
"Specify when to use the decorations that have been specified \
|
||||||
via '--style'. The automatic mode only enables decorations if \
|
via '--style'. The automatic mode only enables decorations if \
|
||||||
an interactive terminal is detected. Possible values: *auto*, never, always.",
|
an interactive terminal is detected. The always mode will show \
|
||||||
),
|
decorations even when piping output. Possible values: *auto*, never, always.",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("force-colorization")
|
Arg::new("force-colorization")
|
||||||
|
@@ -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]
|
#[test]
|
||||||
fn squeeze_blank() {
|
fn squeeze_blank() {
|
||||||
bat()
|
bat()
|
||||||
|
Reference in New Issue
Block a user