on master help sign in
Implement --file-name<name> option

- can specify filename to be displayed when printing. - useful for when piping data from STDIN Closes #654

commit8adce9fae8eb9ceb5dac44546a35c592378607fc authorKyle Criddle <criddle.kyle@gmail.com> dateTue 17 Mar 2020, 20:24 (7 years ago) parenteb2f3dd
4 files changed+21−2
src/bin/bat/app.rs+1 src/bin/bat/clap_app.rs+12 src/config.rs+3 src/printer.rs+5−2
src/bin/bat/app.rs+1
@@ -222,6 +222,7 @@ impl App {
222222 .map(LineRanges::from)
223223 .map(|lr| HighlightedLineRanges(lr))
224224 .unwrap_or_default(),
225+ filename: self.matches.value_of("file-name").or_else(|| None),
225226 })
226227 }
227228
src/bin/bat/clap_app.rs+12
@@ -93,6 +93,18 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
9393 '--highlight-line 40:' highlights lines 40 to the end of the file"
9494 ),
9595 )
96+ .arg(
97+ Arg::with_name("file-name")
98+ .long("file-name")
99+ .takes_value(true)
100+ .number_of_values(1)
101+ .multiple(true)
102+ .value_name("name")
103+ .help("Specify the name to display for a file.")
104+ .long_help("Specify the name to display for a file. Useful when piping \
105+ data to bat from STDIN when bat does not otherwise know \
106+ the filename."),
107+ )
96108 .arg(
97109 Arg::with_name("tabs")
98110 .long("tabs")
src/config.rs+3
@@ -70,6 +70,9 @@ pub struct Config<'a> {
7070
7171 /// Ranges of lines which should be highlighted with a special background color
7272 pub highlighted_lines: HighlightedLineRanges,
73+
74+ /// Name of file to display when printing
75+ pub filename: Option<&'a str>,
7376}
7477
7578#[test]
src/printer.rs+5−2
@@ -231,7 +231,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
231231 InputFile::Ordinary(filename) => {
232232 format!("file '{}'", filename.to_string_lossy())
233233 }
234− _ => "STDIN".into(),
234+ _ => self.config.filename.unwrap_or("STDIN").to_owned(),
235235 };
236236
237237 writeln!(
@@ -267,7 +267,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
267267
268268 let (prefix, name) = match file {
269269 InputFile::Ordinary(filename) => ("File: ", filename.to_string_lossy()),
270− _ => ("", Cow::from("STDIN")),
270+ _ => (
271+ "File: ",
272+ Cow::from(self.config.filename.unwrap_or("STDIN").to_owned()),
273+ ),
271274 };
272275
273276 let mode = match self.content_type {