diff --git a/CHANGELOG.md b/CHANGELOG.md index c67fb827..336d8e50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # unreleased ## Features +- Implemented `-S` and `--chop-long-lines` flags as aliases for `--wrap=never`. See #2309 (@johnmatthiggins) ## Bugfixes diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 7bab26c3..75e5a063 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -161,17 +161,21 @@ impl App { }), show_nonprintable: self.matches.get_flag("show-all"), wrapping_mode: if self.interactive_output || maybe_term_width.is_some() { - match self.matches.get_one::("wrap").map(|s| s.as_str()) { - Some("character") => WrappingMode::Character, - Some("never") => WrappingMode::NoWrapping(true), - Some("auto") | None => { - if style_components.plain() { - WrappingMode::NoWrapping(false) - } else { - WrappingMode::Character + if !self.matches.contains_id("chop-long-lines") { + match self.matches.get_one::("wrap").map(|s| s.as_str()) { + Some("character") => WrappingMode::Character, + Some("never") => WrappingMode::NoWrapping(true), + Some("auto") | None => { + if style_components.plain() { + WrappingMode::NoWrapping(false) + } else { + WrappingMode::Character + } } + _ => unreachable!("other values for --wrap are not allowed"), } - _ => unreachable!("other values for --wrap are not allowed"), + } else { + WrappingMode::NoWrapping(true) } } else { // We don't have the tty width when piping to another program. diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index f96e9e98..426f6171 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -201,6 +201,13 @@ pub fn build_app(interactive_output: bool) -> Command<'static> { The '--terminal-width' option can be used in addition to \ control the output width."), ) + .arg( + Arg::new("chop-long-lines") + .long("chop-long-lines") + .short('S') + .takes_value(false) + .help("Truncate all lines longer than screen width. Alias for '--wrap=never'."), + ) .arg( Arg::new("terminal-width") .long("terminal-width") diff --git a/tests/examples/long-single-line.txt b/tests/examples/long-single-line.txt new file mode 100644 index 00000000..0717aeec --- /dev/null +++ b/tests/examples/long-single-line.txt @@ -0,0 +1 @@ +abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 9352c372..b7becf8b 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1499,6 +1499,47 @@ fn ignored_suffix_arg() { .stderr(""); } +fn wrapping_test(wrap_flag: &str, expect_wrap: bool) { + let expected = match expect_wrap { + true => + "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcde\nfghigklmnopqrstuvxyz\n", + false => + "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz\n", + }; + + bat() + .arg(wrap_flag) + .arg("--style=rule") + .arg("--color=never") + .arg("--decorations=always") + .arg("--terminal-width=80") + .arg("long-single-line.txt") + .assert() + .success() + .stdout(expected.to_owned()) + .stderr(""); +} + +#[test] +fn no_line_wrapping_when_set_to_never() { + wrapping_test("--wrap=never", false); +} + +#[test] +fn line_wrapping_when_auto() { + wrapping_test("--wrap=auto", true); +} + +#[test] +fn no_line_wrapping_with_s_flag() { + wrapping_test("-S", false); +} + +#[test] +fn no_wrapping_with_chop_long_lines() { + wrapping_test("--chop-long-lines", false); +} + #[test] fn highlighting_is_skipped_on_long_lines() { let expected = "\u{1b}[38;5;231m{\u{1b}[0m\u{1b}[38;5;208m\"\u{1b}[0m\u{1b}[38;5;208mapi\u{1b}[0m\u{1b}[38;5;208m\"\u{1b}[0m\u{1b}[38;5;231m:\u{1b}[0m\n".to_owned() +