2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-10 19:10:41 +00:00

Merge pull request #2309 from johnmatthiggins/master

Added -S flag for truncating long lines
This commit is contained in:
David Peter 2022-10-30 17:36:09 +01:00 committed by GitHub
commit 3c9c960612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 9 deletions

View File

@ -1,6 +1,7 @@
# unreleased
## Features
- Implemented `-S` and `--chop-long-lines` flags as aliases for `--wrap=never`. See #2309 (@johnmatthiggins)
## Bugfixes

View File

@ -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.

View File

@ -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")

1
tests/examples/long-single-line.txt vendored Normal file
View File

@ -0,0 +1 @@
abcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyzabcdefghigklmnopqrstuvxyz

View File

@ -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() +