From 8adce9fae8eb9ceb5dac44546a35c592378607fc Mon Sep 17 00:00:00 2001 From: Kyle Criddle Date: Tue, 17 Mar 2020 20:24:48 -0600 Subject: [PATCH 1/2] Implement --file-name option - can specify filename to be displayed when printing. - useful for when piping data from STDIN Closes #654 --- src/bin/bat/app.rs | 1 + src/bin/bat/clap_app.rs | 12 ++++++++++++ src/config.rs | 3 +++ src/printer.rs | 7 +++++-- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index ae889453..edcffe86 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -222,6 +222,7 @@ impl App { .map(LineRanges::from) .map(|lr| HighlightedLineRanges(lr)) .unwrap_or_default(), + filename: self.matches.value_of("file-name").or_else(|| None), }) } diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index 0016a345..a430b844 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -93,6 +93,18 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { '--highlight-line 40:' highlights lines 40 to the end of the file" ), ) + .arg( + Arg::with_name("file-name") + .long("file-name") + .takes_value(true) + .number_of_values(1) + .multiple(true) + .value_name("name") + .help("Specify the name to display for a file.") + .long_help("Specify the name to display for a file. Useful when piping \ + data to bat from STDIN when bat does not otherwise know \ + the filename."), + ) .arg( Arg::with_name("tabs") .long("tabs") diff --git a/src/config.rs b/src/config.rs index 19e91484..c9859a97 100644 --- a/src/config.rs +++ b/src/config.rs @@ -70,6 +70,9 @@ pub struct Config<'a> { /// Ranges of lines which should be highlighted with a special background color pub highlighted_lines: HighlightedLineRanges, + + /// Name of file to display when printing + pub filename: Option<&'a str>, } #[test] diff --git a/src/printer.rs b/src/printer.rs index 629b11da..9313d3bd 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -231,7 +231,7 @@ impl<'a> Printer for InteractivePrinter<'a> { InputFile::Ordinary(filename) => { format!("file '{}'", filename.to_string_lossy()) } - _ => "STDIN".into(), + _ => self.config.filename.unwrap_or("STDIN").to_owned(), }; writeln!( @@ -267,7 +267,10 @@ impl<'a> Printer for InteractivePrinter<'a> { let (prefix, name) = match file { InputFile::Ordinary(filename) => ("File: ", filename.to_string_lossy()), - _ => ("", Cow::from("STDIN")), + _ => ( + "File: ", + Cow::from(self.config.filename.unwrap_or("STDIN").to_owned()), + ), }; let mode = match self.content_type { From fb3c775c8b4cf25b648bbdc422b99f93608914c9 Mon Sep 17 00:00:00 2001 From: Kyle Criddle Date: Thu, 19 Mar 2020 20:46:19 -0600 Subject: [PATCH 2/2] --file-name for normal files. integration tests. --- src/printer.rs | 17 ++++++++--- tests/examples/test.binary | Bin 0 -> 4 bytes tests/integration_tests.rs | 57 +++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/examples/test.binary diff --git a/src/printer.rs b/src/printer.rs index 9313d3bd..d032d11f 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -228,9 +228,10 @@ impl<'a> Printer for InteractivePrinter<'a> { if !self.config.style_components.header() { if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable { let input = match file { - InputFile::Ordinary(filename) => { - format!("file '{}'", filename.to_string_lossy()) - } + InputFile::Ordinary(filename) => format!( + "file '{}'", + self.config.filename.unwrap_or(&filename.to_string_lossy()) + ), _ => self.config.filename.unwrap_or("STDIN").to_owned(), }; @@ -266,7 +267,15 @@ impl<'a> Printer for InteractivePrinter<'a> { } let (prefix, name) = match file { - InputFile::Ordinary(filename) => ("File: ", filename.to_string_lossy()), + InputFile::Ordinary(filename) => ( + "File: ", + Cow::from( + self.config + .filename + .unwrap_or(&filename.to_string_lossy()) + .to_owned(), + ), + ), _ => ( "File: ", Cow::from(self.config.filename.unwrap_or("STDIN").to_owned()), diff --git a/tests/examples/test.binary b/tests/examples/test.binary new file mode 100644 index 0000000000000000000000000000000000000000..593f4708db84ac8fd0f5cc47c634f38c013fe9e4 GIT binary patch literal 4 LcmZQzU|;|M00aO5 literal 0 HcmV?d00001 diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 0d5f4a3c..f2722339 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -541,3 +541,60 @@ fn empty_file_leads_to_empty_output_with_grid_enabled() { .success() .stdout(""); } + +#[test] +fn filename_basic() { + bat() + .arg("test.txt") + .arg("--decorations=always") + .arg("--style=header") + .arg("-r=0:0") + .arg("--file-name=foo") + .assert() + .success() + .stdout("File: foo\n") + .stderr(""); +} + +#[test] +fn filename_binary() { + bat() + .arg("test.binary") + .arg("--decorations=always") + .arg("--style=header") + .arg("-r=0:0") + .arg("--file-name=foo") + .assert() + .success() + .stdout("File: foo \n") + .stderr(""); +} + +#[test] +fn filename_stdin() { + bat() + .arg("--decorations=always") + .arg("--style=header") + .arg("-r=0:0") + .arg("-") + .write_stdin("stdin\n") + .arg("--file-name=foo") + .assert() + .success() + .stdout("File: foo\n") + .stderr(""); +} + +#[test] +fn filename_stdin_binary() { + let vec = vec![0; 1]; + bat_with_config() + .arg("--decorations=always") + .arg("--style=header") + .write_stdin(vec) + .arg("--file-name=foo") + .assert() + .success() + .stdout("File: foo \n") + .stderr(""); +}