From a272d3df16df67db0cd41df517b7d0379116ccee Mon Sep 17 00:00:00 2001 From: John Higgins Date: Fri, 9 Sep 2022 17:04:09 -0700 Subject: [PATCH 1/7] Added -S flag for truncating long lines --- src/bin/bat/app.rs | 23 ++++++++++++++--------- src/bin/bat/clap_app.rs | 7 +++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 7bab26c3..b52aa265 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -161,17 +161,22 @@ 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") From 2af54dbacda66a34155edfd3cdf6552de8573c4b Mon Sep 17 00:00:00 2001 From: John Higgins Date: Fri, 9 Sep 2022 22:26:53 -0700 Subject: [PATCH 2/7] Fixed else statement with `cargo fmt` --- src/bin/bat/app.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index b52aa265..75e5a063 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -174,8 +174,7 @@ impl App { } _ => unreachable!("other values for --wrap are not allowed"), } - } - else { + } else { WrappingMode::NoWrapping(true) } } else { From 375b392478586690f53b2667d356aba928ce8c57 Mon Sep 17 00:00:00 2001 From: John Higgins Date: Sun, 9 Oct 2022 22:41:27 -0700 Subject: [PATCH 3/7] Fixed reversed logic error for --chop-long-lines --- src/bin/bat/app.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index b52aa265..eeaa7b39 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -174,9 +174,8 @@ impl App { } _ => unreachable!("other values for --wrap are not allowed"), } - } - else { - WrappingMode::NoWrapping(true) + } else { + WrappingMode::Character } } else { // We don't have the tty width when piping to another program. From 236a2c57944d710332d97881188bafa720600269 Mon Sep 17 00:00:00 2001 From: John Higgins Date: Thu, 13 Oct 2022 23:52:15 -0700 Subject: [PATCH 4/7] Fixed inverted logic on -S and --chop-long-lines --- CHANGELOG.md | 1 + src/bin/bat/app.rs | 2 +- src/bin/bat/clap_app.rs | 2 +- tests/integration_tests.rs | 51 +++++++++++--------------------------- 4 files changed, 18 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 986e8a09..67332437 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # unreleased ## Features +- Implemented `-S` and `--chop-long-lines` flags as aliases for `--wrap=character`. See #2309 (@johnmatthiggins) ## Bugfixes diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index eeaa7b39..75e5a063 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -175,7 +175,7 @@ impl App { _ => unreachable!("other values for --wrap are not allowed"), } } else { - WrappingMode::Character + 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 426f6171..c956bf5d 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -206,7 +206,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> { .long("chop-long-lines") .short('S') .takes_value(false) - .help("Truncate all lines longer than screen width. Alias for '--wrap=never'."), + .help("Truncate all lines longer than screen width. Alias for '--wrap=character'."), ) .arg( Arg::new("terminal-width") diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 3130f434..6417327f 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1501,17 +1501,12 @@ fn ignored_suffix_arg() { #[test] fn no_line_wrapping_when_set_to_never() { - let expected = -"───────┬──────────────────────────────────────────────────────────────────────── - │ File: 80-columns.txt - │ Size: 101 B -───────┼──────────────────────────────────────────────────────────────────────── - 1 │ abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz -───────┴──────────────────────────────────────────────────────────────────────── + let expected = "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz "; bat() - .arg("--style=full") + .arg("--style=rule") + .arg("--color=never") .arg("--decorations=always") .arg("--wrap=never") .arg("--terminal-width=80") @@ -1525,17 +1520,13 @@ fn no_line_wrapping_when_set_to_never() { #[test] fn line_wrapping_when_auto() { let expected = - "───────┬──────────────────────────────────────────────────────────────────────── - │ File: 80-columns.txt - │ Size: 101 B -───────┼──────────────────────────────────────────────────────────────────────── - 1 │ abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstu - │ vxyzabcdefghigklmnopqrstuvxyz -───────┴──────────────────────────────────────────────────────────────────────── + "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcde +fghigklmnopqrstuvxyz "; bat() - .arg("--style=full") + .arg("--color=never") + .arg("--style=rule") .arg("--decorations=always") .arg("--wrap=auto") .arg("--terminal-width=80") @@ -1547,19 +1538,13 @@ fn line_wrapping_when_auto() { } #[test] -fn line_wrapping_with_s_flag() { +fn no_line_wrapping_with_s_flag() { let expected = - "───────┬──────────────────────────────────────────────────────────────────────── - │ File: 80-columns.txt - │ Size: 101 B -───────┼──────────────────────────────────────────────────────────────────────── - 1 │ abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstu - │ vxyzabcdefghigklmnopqrstuvxyz -───────┴──────────────────────────────────────────────────────────────────────── -"; + "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz\n"; bat() - .arg("--style=full") + .arg("--color=never") + .arg("--style=rule") .arg("--decorations=always") .arg("-S") .arg("--terminal-width=80") @@ -1571,19 +1556,13 @@ fn line_wrapping_with_s_flag() { } #[test] -fn chop_long_lines_when_specified() { +fn no_wrapping_with_chop_long_lines() { let expected = - "───────┬──────────────────────────────────────────────────────────────────────── - │ File: 80-columns.txt - │ Size: 101 B -───────┼──────────────────────────────────────────────────────────────────────── - 1 │ abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstu - │ vxyzabcdefghigklmnopqrstuvxyz -───────┴──────────────────────────────────────────────────────────────────────── -"; + "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz\n"; bat() - .arg("--style=full") + .arg("--color=never") + .arg("--style=rule") .arg("--decorations=always") .arg("--chop-long-lines") .arg("--terminal-width=80") From 20f272168ac155c2a4b78d6935d675f7d33603f3 Mon Sep 17 00:00:00 2001 From: John Higgins Date: Fri, 14 Oct 2022 14:23:51 -0700 Subject: [PATCH 5/7] Fixed typo in CHANGELOG.md for PR #2309 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67332437..e7869cc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # unreleased ## Features -- Implemented `-S` and `--chop-long-lines` flags as aliases for `--wrap=character`. See #2309 (@johnmatthiggins) +- Implemented `-S` and `--chop-long-lines` flags as aliases for `--wrap=never`. See #2309 (@johnmatthiggins) ## Bugfixes From bf114f5844c0a1e70ea479857708eb76fe44c632 Mon Sep 17 00:00:00 2001 From: John Higgins Date: Mon, 17 Oct 2022 14:43:02 -0700 Subject: [PATCH 6/7] Added helper function for -S flag tests --- .../{80-columns.txt => long-single-line.txt} | 0 tests/integration_tests.rs | 69 +++++-------------- 2 files changed, 18 insertions(+), 51 deletions(-) rename tests/examples/{80-columns.txt => long-single-line.txt} (100%) diff --git a/tests/examples/80-columns.txt b/tests/examples/long-single-line.txt similarity index 100% rename from tests/examples/80-columns.txt rename to tests/examples/long-single-line.txt diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 6417327f..b7becf8b 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1499,78 +1499,45 @@ fn ignored_suffix_arg() { .stderr(""); } -#[test] -fn no_line_wrapping_when_set_to_never() { - let expected = "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz -"; +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("--wrap=never") .arg("--terminal-width=80") - .arg("80-columns.txt") + .arg("long-single-line.txt") .assert() .success() - .stdout(expected) + .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() { - let expected = - "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcde -fghigklmnopqrstuvxyz -"; - - bat() - .arg("--color=never") - .arg("--style=rule") - .arg("--decorations=always") - .arg("--wrap=auto") - .arg("--terminal-width=80") - .arg("80-columns.txt") - .assert() - .success() - .stdout(expected) - .stderr(""); + wrapping_test("--wrap=auto", true); } #[test] fn no_line_wrapping_with_s_flag() { - let expected = - "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz\n"; - - bat() - .arg("--color=never") - .arg("--style=rule") - .arg("--decorations=always") - .arg("-S") - .arg("--terminal-width=80") - .arg("80-columns.txt") - .assert() - .success() - .stdout(expected) - .stderr(""); + wrapping_test("-S", false); } #[test] fn no_wrapping_with_chop_long_lines() { - let expected = - "abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz\n"; - - bat() - .arg("--color=never") - .arg("--style=rule") - .arg("--decorations=always") - .arg("--chop-long-lines") - .arg("--terminal-width=80") - .arg("80-columns.txt") - .assert() - .success() - .stdout(expected) - .stderr(""); + wrapping_test("--chop-long-lines", false); } #[test] From ff7130334951311d2516bb202ca09d59f74539e0 Mon Sep 17 00:00:00 2001 From: John Higgins <76265228+johnmatthiggins@users.noreply.github.com> Date: Mon, 17 Oct 2022 23:35:52 -0700 Subject: [PATCH 7/7] Update src/bin/bat/clap_app.rs for PR 2309 Co-authored-by: Martin Nordholts --- src/bin/bat/clap_app.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index c956bf5d..426f6171 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -206,7 +206,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> { .long("chop-long-lines") .short('S') .takes_value(false) - .help("Truncate all lines longer than screen width. Alias for '--wrap=character'."), + .help("Truncate all lines longer than screen width. Alias for '--wrap=never'."), ) .arg( Arg::new("terminal-width")