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

commit517be5c7bcb034bda56ba8572f2e5f103163bf58 authorKyle Criddle <criddle.kyle@gmail.com> dateTue 17 Mar 2020, 20:24 (7 years ago) parent260c5dc
4 files changed+21−2
src/bin/bat/app.rs+1 src/bin/bat/clap_app.rs+12 src/lib.rs+3 src/printer.rs+5−2
src/bin/bat/app.rs+1
@@ -222,6 +222,7 @@ impl App {
222222 .transpose()?
223223 .unwrap_or_else(|| vec![LineRange { lower: 0, upper: 0 }]),
224224 ),
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/lib.rs+3
@@ -129,4 +129,7 @@ pub struct Config<'a> {
129129
130130 /// Lines to highlight
131131 pub highlight_lines: LineRanges,
132+
133+ /// Name of file to display when printing
134+ pub filename: Option<&'a str>,
132135}
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 {