on master help sign in
--file-name for normal files. integration tests.
commitcfa2cb6ec77f8faaab134c296350b982ff1ffe40 authorKyle Criddle <criddle.kyle@gmail.com> dateThu 19 Mar 2020, 20:46 (7 years ago) parent517be5c
3 files changed+70−4
src/printer.rs+13−4 tests/examples/test.binary tests/integration_tests.rs+57
src/printer.rs+13−4
@@ -228,9 +228,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
228228 if !self.config.output_components.header() {
229229 if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
230230 let input = match file {
231− InputFile::Ordinary(filename) => {
232− format!("file '{}'", filename.to_string_lossy())
233− }
231+ InputFile::Ordinary(filename) => format!(
232+ "file '{}'",
233+ self.config.filename.unwrap_or(&filename.to_string_lossy())
234+ ),
234235 _ => self.config.filename.unwrap_or("STDIN").to_owned(),
235236 };
236237
@@ -266,7 +267,15 @@ impl<'a> Printer for InteractivePrinter<'a> {
266267 }
267268
268269 let (prefix, name) = match file {
269− InputFile::Ordinary(filename) => ("File: ", filename.to_string_lossy()),
270+ InputFile::Ordinary(filename) => (
271+ "File: ",
272+ Cow::from(
273+ self.config
274+ .filename
275+ .unwrap_or(&filename.to_string_lossy())
276+ .to_owned(),
277+ ),
278+ ),
270279 _ => (
271280 "File: ",
272281 Cow::from(self.config.filename.unwrap_or("STDIN").to_owned()),
tests/examples/test.binarynew file

Binary file not shown.

tests/integration_tests.rs+57
@@ -541,3 +541,60 @@ fn empty_file_leads_to_empty_output_with_grid_enabled() {
541541 .success()
542542 .stdout("");
543543}
544+
545+#[test]
546+fn filename_basic() {
547+ bat()
548+ .arg("test.txt")
549+ .arg("--decorations=always")
550+ .arg("--style=header")
551+ .arg("-r=0:0")
552+ .arg("--file-name=foo")
553+ .assert()
554+ .success()
555+ .stdout("File: foo\n")
556+ .stderr("");
557+}
558+
559+#[test]
560+fn filename_binary() {
561+ bat()
562+ .arg("test.binary")
563+ .arg("--decorations=always")
564+ .arg("--style=header")
565+ .arg("-r=0:0")
566+ .arg("--file-name=foo")
567+ .assert()
568+ .success()
569+ .stdout("File: foo <BINARY>\n")
570+ .stderr("");
571+}
572+
573+#[test]
574+fn filename_stdin() {
575+ bat()
576+ .arg("--decorations=always")
577+ .arg("--style=header")
578+ .arg("-r=0:0")
579+ .arg("-")
580+ .write_stdin("stdin\n")
581+ .arg("--file-name=foo")
582+ .assert()
583+ .success()
584+ .stdout("File: foo\n")
585+ .stderr("");
586+}
587+
588+#[test]
589+fn filename_stdin_binary() {
590+ let vec = vec![0; 1];
591+ bat_with_config()
592+ .arg("--decorations=always")
593+ .arg("--style=header")
594+ .write_stdin(vec)
595+ .arg("--file-name=foo")
596+ .assert()
597+ .success()
598+ .stdout("File: foo <BINARY>\n")
599+ .stderr("");
600+}