Skip to content

Commit 5940695

Browse files
authored
Merge pull request #3592 from IMaloney/fix/wrap-never-clean
respect --wrap=never flag when piping to pager
2 parents fbc05da + 62b0388 commit 5940695

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Improve native man pages and command help syntax highlighting by stripping overstriking, see #3517 (@akirk)
1111

1212
## Bugfixes
13+
- Fix `--wrap=never` and `-S` flags being ignored when piping to pager, see #3592 (@IMaloney)
1314
- Fix crash with BusyBox `less` on Windows, see #3527 (@Anchal-T)
1415
- Fix `bat cache --help` failing with 'unexpected argument' error, see #3580 and #3560 (@NORMAL-EX)
1516
- `--help` now correctly honors `--pager=builtin`. See #3516 (@keith-hall)

src/bin/bat/app.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -399,27 +399,29 @@ impl App {
399399
Some("no-printing") => BinaryBehavior::NoPrinting,
400400
_ => unreachable!("other values for --binary are not allowed"),
401401
},
402-
wrapping_mode: if self.interactive_output || maybe_term_width.is_some() {
403-
if !self.matches.get_flag("chop-long-lines") {
402+
wrapping_mode: {
403+
if self.matches.get_flag("chop-long-lines") {
404+
WrappingMode::NoWrapping(true)
405+
} else {
404406
match self.matches.get_one::<String>("wrap").map(|s| s.as_str()) {
405407
Some("character") => WrappingMode::Character,
406408
Some("never") => WrappingMode::NoWrapping(true),
407409
Some("auto") | None => {
408-
if style_components.plain() && maybe_term_width.is_none() {
409-
WrappingMode::NoWrapping(false)
410+
if self.interactive_output || maybe_term_width.is_some() {
411+
if style_components.plain() && maybe_term_width.is_none() {
412+
WrappingMode::NoWrapping(false)
413+
} else {
414+
WrappingMode::Character
415+
}
410416
} else {
411-
WrappingMode::Character
417+
// We don't have the tty width when piping to another program.
418+
// There's no point in wrapping when this is the case.
419+
WrappingMode::NoWrapping(false)
412420
}
413421
}
414422
_ => unreachable!("other values for --wrap are not allowed"),
415423
}
416-
} else {
417-
WrappingMode::NoWrapping(true)
418424
}
419-
} else {
420-
// We don't have the tty width when piping to another program.
421-
// There's no point in wrapping when this is the case.
422-
WrappingMode::NoWrapping(false)
423425
},
424426
colored_output: self.matches.get_flag("force-colorization")
425427
|| match self.matches.get_one::<String>("color").map(|s| s.as_str()) {

tests/integration_tests.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,6 +2884,44 @@ fn no_wrapping_with_chop_long_lines() {
28842884
wrapping_test("--chop-long-lines", false);
28852885
}
28862886

2887+
#[test]
2888+
#[serial]
2889+
fn wrap_never_flag_respected_with_paging_always() {
2890+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
2891+
bat()
2892+
.arg("--pager=cat")
2893+
.arg("--paging=always")
2894+
.arg("--wrap=never")
2895+
.arg("--color=never")
2896+
.arg("--decorations=never")
2897+
.arg("--style=plain")
2898+
.write_stdin("abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz\n")
2899+
.assert()
2900+
.success()
2901+
.stdout(predicate::str::contains("abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz").normalize())
2902+
.stderr("");
2903+
});
2904+
}
2905+
2906+
#[test]
2907+
#[serial]
2908+
fn s_flag_respected_with_paging_always() {
2909+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
2910+
bat()
2911+
.arg("--pager=cat")
2912+
.arg("--paging=always")
2913+
.arg("-S")
2914+
.arg("--color=never")
2915+
.arg("--decorations=never")
2916+
.arg("--style=plain")
2917+
.write_stdin("abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz\n")
2918+
.assert()
2919+
.success()
2920+
.stdout(predicate::str::contains("abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz").normalize())
2921+
.stderr("");
2922+
});
2923+
}
2924+
28872925
#[test]
28882926
fn theme_arg_overrides_env() {
28892927
bat()

0 commit comments

Comments
 (0)