diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 75e66854..4a6d4961 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -222,6 +222,7 @@ impl App { .transpose()? .unwrap_or_else(|| vec![LineRange { lower: 0, upper: 0 }]), ), + 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 f234085a..41827cd8 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/lib.rs b/src/lib.rs index 2172892f..437288b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -129,4 +129,7 @@ pub struct Config<'a> { /// Lines to highlight pub highlight_lines: LineRanges, + + /// Name of file to display when printing + pub filename: Option<&'a str>, } diff --git a/src/printer.rs b/src/printer.rs index 71cbf028..203a125b 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 {