Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added -S flag for truncating long lines #2309

Merged
merged 9 commits into from
Oct 30, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# unreleased

## Features
- Implemented `-S` and `--chop-long-lines` flags as aliases for `--wrap=never`. See #2309 (@johnmatthiggins)

## Bugfixes

Expand Down
22 changes: 13 additions & 9 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>("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::<String>("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.
Expand Down
7 changes: 7 additions & 0 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions tests/examples/long-single-line.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz
41 changes: 41 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
Expand Down