Changed default to be Plain in case of loop_through

This commit is contained in:
Domenico Mastrangelo 2024-06-06 13:04:33 +02:00
parent 2ba681315c
commit 1f4d6f0a89
5 changed files with 84 additions and 65 deletions

View File

@ -93,7 +93,16 @@ impl App {
} }
pub fn config(&self, inputs: &[Input]) -> Result<Config> { pub fn config(&self, inputs: &[Input]) -> Result<Config> {
let style_components = self.style_components()?; let loop_through = !(self.interactive_output
|| self.matches.get_one::<String>("color").map(|s| s.as_str()) == Some("always")
|| self
.matches
.get_one::<String>("decorations")
.map(|s| s.as_str())
== Some("always")
|| self.matches.get_flag("force-colorization"));
let style_components = self.style_components(loop_through)?;
let paging_mode = match self.matches.get_one::<String>("paging").map(|s| s.as_str()) { let paging_mode = match self.matches.get_one::<String>("paging").map(|s| s.as_str()) {
Some("always") => PagingMode::Always, Some("always") => PagingMode::Always,
@ -222,14 +231,7 @@ impl App {
}, },
paging_mode, paging_mode,
term_width: maybe_term_width.unwrap_or(Term::stdout().size().1 as usize), term_width: maybe_term_width.unwrap_or(Term::stdout().size().1 as usize),
loop_through: !(self.interactive_output loop_through: loop_through,
|| self.matches.get_one::<String>("color").map(|s| s.as_str()) == Some("always")
|| self
.matches
.get_one::<String>("decorations")
.map(|s| s.as_str())
== Some("always")
|| self.matches.get_flag("force-colorization")),
tab_width: self tab_width: self
.matches .matches
.get_one::<String>("tabs") .get_one::<String>("tabs")
@ -353,7 +355,7 @@ impl App {
Ok(file_input) Ok(file_input)
} }
fn style_components(&self) -> Result<StyleComponents> { fn style_components(&self, loop_through: bool) -> Result<StyleComponents> {
let matches = &self.matches; let matches = &self.matches;
let mut styled_components = StyleComponents( let mut styled_components = StyleComponents(
if matches.get_one::<String>("decorations").map(|s| s.as_str()) == Some("never") { if matches.get_one::<String>("decorations").map(|s| s.as_str()) == Some("never") {
@ -374,7 +376,7 @@ impl App {
}) })
.unwrap_or_else(|| vec![StyleComponent::Default]) .unwrap_or_else(|| vec![StyleComponent::Default])
.into_iter() .into_iter()
.map(|style| style.components(self.interactive_output)) .map(|style| style.components(self.interactive_output, loop_through))
.fold(HashSet::new(), |mut acc, components| { .fold(HashSet::new(), |mut acc, components| {
acc.extend(components.iter().cloned()); acc.extend(components.iter().cloned());
acc acc

View File

@ -365,7 +365,9 @@ fn run() -> Result<bool> {
let languages: String = get_languages(&config, cache_dir)?; let languages: String = get_languages(&config, cache_dir)?;
let inputs: Vec<Input> = vec![Input::from_reader(Box::new(languages.as_bytes()))]; let inputs: Vec<Input> = vec![Input::from_reader(Box::new(languages.as_bytes()))];
let plain_config = Config { let plain_config = Config {
style_components: StyleComponents::new(StyleComponent::Plain.components(false)), style_components: StyleComponents::new(
StyleComponent::Plain.components(false, config.loop_through),
),
paging_mode: PagingMode::QuitIfOneScreen, paging_mode: PagingMode::QuitIfOneScreen,
..Default::default() ..Default::default()
}; };

View File

@ -15,7 +15,6 @@ use crate::output::OutputType;
#[cfg(feature = "paging")] #[cfg(feature = "paging")]
use crate::paging::PagingMode; use crate::paging::PagingMode;
use crate::printer::{InteractivePrinter, OutputHandle, Printer, SimplePrinter}; use crate::printer::{InteractivePrinter, OutputHandle, Printer, SimplePrinter};
use crate::style::{StyleComponent, StyleComponents};
use clircle::{Clircle, Identifier}; use clircle::{Clircle, Identifier};
@ -175,19 +174,8 @@ impl<'b> Controller<'b> {
None None
}; };
let mut config = self.config.clone();
if self.config.loop_through {
config = Config {
style_components: StyleComponents(
[StyleComponent::Plain].iter().cloned().collect(),
),
..self.config.clone()
};
}
let mut printer: Box<dyn Printer> = if self.config.loop_through { let mut printer: Box<dyn Printer> = if self.config.loop_through {
Box::new(SimplePrinter::new(&config)) Box::new(SimplePrinter::new(self.config))
} else { } else {
Box::new(InteractivePrinter::new( Box::new(InteractivePrinter::new(
self.config, self.config,

View File

@ -22,13 +22,17 @@ pub enum StyleComponent {
} }
impl StyleComponent { impl StyleComponent {
pub fn components(self, interactive_terminal: bool) -> &'static [StyleComponent] { pub fn components(
self,
interactive_terminal: bool,
loop_through: bool,
) -> &'static [StyleComponent] {
match self { match self {
StyleComponent::Auto => { StyleComponent::Auto => {
if interactive_terminal { if interactive_terminal {
StyleComponent::Default.components(interactive_terminal) StyleComponent::Default.components(interactive_terminal, loop_through)
} else { } else {
StyleComponent::Plain.components(interactive_terminal) StyleComponent::Plain.components(interactive_terminal, loop_through)
} }
} }
#[cfg(feature = "git")] #[cfg(feature = "git")]
@ -49,14 +53,24 @@ impl StyleComponent {
StyleComponent::LineNumbers, StyleComponent::LineNumbers,
StyleComponent::Snip, StyleComponent::Snip,
], ],
StyleComponent::Default => &[ StyleComponent::Default => {
if loop_through {
return &[
#[cfg(feature = "git")]
StyleComponent::Changes,
StyleComponent::Plain,
];
} else {
return &[
#[cfg(feature = "git")] #[cfg(feature = "git")]
StyleComponent::Changes, StyleComponent::Changes,
StyleComponent::Grid, StyleComponent::Grid,
StyleComponent::HeaderFilename, StyleComponent::HeaderFilename,
StyleComponent::LineNumbers, StyleComponent::LineNumbers,
StyleComponent::Snip, StyleComponent::Snip,
], ];
}
}
StyleComponent::Plain => &[], StyleComponent::Plain => &[],
} }
} }

View File

@ -48,7 +48,7 @@ fn basic() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 hello world\n") .stdout("hello world\n")
.stderr(""); .stderr("");
} }
@ -58,7 +58,7 @@ fn stdin() {
.write_stdin("foo\nbar\n") .write_stdin("foo\nbar\n")
.assert() .assert()
.success() .success()
.stdout(" 1 foo\n 2 bar\n"); .stdout("foo\nbar\n");
} }
#[test] #[test]
@ -68,7 +68,7 @@ fn concatenate() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 hello world\n 1 hello world\n"); .stdout("hello world\nhello world\n");
} }
#[test] #[test]
@ -80,7 +80,7 @@ fn concatenate_stdin() {
.write_stdin("stdin\n") .write_stdin("stdin\n")
.assert() .assert()
.success() .success()
.stdout(" 1 hello world\n 1 stdin\n 1 hello world\n"); .stdout("hello world\nstdin\nhello world\n");
} }
#[test] #[test]
@ -90,7 +90,7 @@ fn concatenate_empty_first() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 hello world\n"); .stdout("hello world\n");
} }
#[test] #[test]
@ -100,7 +100,7 @@ fn concatenate_empty_last() {
.arg("empty.txt") .arg("empty.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 hello world\n"); .stdout("hello world\n");
} }
#[test] #[test]
@ -121,7 +121,7 @@ fn concatenate_empty_between() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 hello world\n 1 hello world\n"); .stdout("hello world\nhello world\n");
} }
#[test] #[test]
@ -132,7 +132,7 @@ fn concatenate_empty_first_and_last() {
.arg("empty.txt") .arg("empty.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 hello world\n"); .stdout("hello world\n");
} }
#[test] #[test]
@ -142,7 +142,7 @@ fn concatenate_single_line() {
.arg("single-line.txt") .arg("single-line.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 Single Line 1 Single Line"); .stdout("Single LineSingle Line");
} }
#[test] #[test]
@ -153,7 +153,7 @@ fn concatenate_single_line_empty() {
.arg("single-line.txt") .arg("single-line.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 Single Line 1 Single Line"); .stdout("Single LineSingle Line");
} }
#[test] #[test]
@ -174,7 +174,7 @@ fn line_range_2_3() {
.arg("--line-range=2:3") .arg("--line-range=2:3")
.assert() .assert()
.success() .success()
.stdout(" 2 line 2\n 3 line 3\n"); .stdout("line 2\nline 3\n");
} }
#[test] #[test]
@ -184,7 +184,7 @@ fn line_range_first_two() {
.arg("--line-range=:2") .arg("--line-range=:2")
.assert() .assert()
.success() .success()
.stdout(" 1 line 1\n 2 line 2\n"); .stdout("line 1\nline 2\n");
} }
#[test] #[test]
@ -194,7 +194,7 @@ fn line_range_last_3() {
.arg("--line-range=2:") .arg("--line-range=2:")
.assert() .assert()
.success() .success()
.stdout(" 2 line 2\n 3 line 3\n 4 line 4\n"); .stdout("line 2\nline 3\nline 4\n");
} }
#[test] #[test]
@ -205,7 +205,7 @@ fn line_range_multiple() {
.arg("--line-range=4:4") .arg("--line-range=4:4")
.assert() .assert()
.success() .success()
.stdout(" 1 line 1\n 2 line 2\n 4 line 4\n"); .stdout("line 1\nline 2\nline 4\n");
} }
#[test] #[test]
@ -215,7 +215,7 @@ fn squeeze_blank() {
.arg("--squeeze-blank") .arg("--squeeze-blank")
.assert() .assert()
.success() .success()
.stdout(" 1 line 1\n 2 \n 5 line 5\n 6 \n 20 line 20\n 21 line 21\n 22 \n 24 line 24\n 25 \n 26 line 26\n 27 \n 30 line 30\n"); .stdout("line 1\n\nline 5\n\nline 20\nline 21\n\nline 24\n\nline 26\n\nline 30\n");
} }
#[test] #[test]
@ -238,7 +238,7 @@ fn squeeze_limit() {
.arg("--squeeze-limit=2") .arg("--squeeze-limit=2")
.assert() .assert()
.success() .success()
.stdout(" 1 line 1\n 2 \n 3 \n 5 line 5\n 6 \n 7 \n 20 line 20\n 21 line 21\n 22 \n 23 \n 24 line 24\n 25 \n 26 line 26\n 27 \n 28 \n 30 line 30\n"); .stdout("line 1\n\n\nline 5\n\n\nline 20\nline 21\n\n\nline 24\n\nline 26\n\n\nline 30\n");
bat() bat()
.arg("empty_lines.txt") .arg("empty_lines.txt")
@ -246,7 +246,7 @@ fn squeeze_limit() {
.arg("--squeeze-limit=5") .arg("--squeeze-limit=5")
.assert() .assert()
.success() .success()
.stdout(" 1 line 1\n 2 \n 3 \n 4 \n 5 line 5\n 6 \n 7 \n 8 \n 9 \n 10 \n 20 line 20\n 21 line 21\n 22 \n 23 \n 24 line 24\n 25 \n 26 line 26\n 27 \n 28 \n 29 \n 30 line 30\n"); .stdout("line 1\n\n\n\nline 5\n\n\n\n\n\nline 20\nline 21\n\n\nline 24\n\nline 26\n\n\n\nline 30\n");
} }
#[test] #[test]
@ -693,7 +693,7 @@ fn do_not_exit_directory() {
.arg("sub_directory") .arg("sub_directory")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.stdout(" 1 hello world\n") .stdout("hello world\n")
.failure(); .failure();
} }
@ -752,7 +752,7 @@ fn pager_disable() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq(" 1 hello world\n").normalize()); .stdout(predicate::eq("hello world\n").normalize());
} }
#[test] #[test]
@ -833,7 +833,7 @@ fn env_var_pager_value_bat() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq(" 1 hello world\n").normalize()); .stdout(predicate::eq("hello world\n").normalize());
} }
#[test] #[test]
@ -871,7 +871,7 @@ fn pager_most_from_pager_env_var() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq(" 1 hello world\n").normalize()); .stdout(predicate::eq("hello world\n").normalize());
}); });
} }
@ -917,7 +917,7 @@ fn pager_most_with_arg() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq(" 1 hello world\n").normalize()); .stdout(predicate::eq("hello world\n").normalize());
}); });
} }
@ -932,7 +932,7 @@ fn pager_more() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq(" 1 hello world\n").normalize()); .stdout(predicate::eq("hello world\n").normalize());
}); });
} }
@ -944,7 +944,7 @@ fn alias_pager_disable() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq(" 1 hello world\n").normalize()); .stdout(predicate::eq("hello world\n").normalize());
} }
#[test] #[test]
@ -971,7 +971,7 @@ fn disable_pager_if_disable_paging_flag_comes_after_paging() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq(" 1 hello world\n").normalize()); .stdout(predicate::eq("hello world\n").normalize());
} }
#[test] #[test]
@ -1043,7 +1043,7 @@ fn basic_set_terminal_title() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout("\u{1b}]0;bat: test.txt\x07 1 hello world\n") .stdout("\u{1b}]0;bat: test.txt\x07hello world\n")
.stderr(""); .stderr("");
} }
@ -1294,7 +1294,7 @@ fn can_print_file_named_cache() {
.arg("cache") .arg("cache")
.assert() .assert()
.success() .success()
.stdout(" 1 test\n") .stdout("test\n")
.stderr(""); .stderr("");
} }
@ -1305,7 +1305,7 @@ fn can_print_file_named_cache_with_additional_argument() {
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(" 1 test\n 1 hello world\n") .stdout("test\nhello world\n")
.stderr(""); .stderr("");
} }
@ -1315,7 +1315,7 @@ fn can_print_file_starting_with_cache() {
.arg("cache.c") .arg("cache.c")
.assert() .assert()
.success() .success()
.stdout(" 1 test\n") .stdout("test\n")
.stderr(""); .stderr("");
} }
@ -2666,3 +2666,16 @@ fn highlighting_independant_from_map_syntax_case() {
.stdout(expected) .stdout(expected)
.stderr(""); .stderr("");
} }
#[test]
fn piped_output_with_lines() {
let expected = " 1 hello\n 2 world\n";
bat()
.arg("-n")
.write_stdin("hello\nworld\n")
.assert()
.success()
.stdout(expected)
.stderr("");
}