mirror of
				https://github.com/sharkdp/bat.git
				synced 2025-10-30 22:54:07 +00:00 
			
		
		
		
	Implement --file-name<name> option
- can specify filename to be displayed when printing. - useful for when piping data from STDIN Closes #654
This commit is contained in:
		| @@ -222,6 +222,7 @@ impl App { | |||||||
|                 .map(LineRanges::from) |                 .map(LineRanges::from) | ||||||
|                 .map(|lr| HighlightedLineRanges(lr)) |                 .map(|lr| HighlightedLineRanges(lr)) | ||||||
|                 .unwrap_or_default(), |                 .unwrap_or_default(), | ||||||
|  |             filename: self.matches.value_of("file-name").or_else(|| None), | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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" |                      '--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( | ||||||
|             Arg::with_name("tabs") |             Arg::with_name("tabs") | ||||||
|                 .long("tabs") |                 .long("tabs") | ||||||
|   | |||||||
| @@ -70,6 +70,9 @@ pub struct Config<'a> { | |||||||
|  |  | ||||||
|     /// Ranges of lines which should be highlighted with a special background color |     /// Ranges of lines which should be highlighted with a special background color | ||||||
|     pub highlighted_lines: HighlightedLineRanges, |     pub highlighted_lines: HighlightedLineRanges, | ||||||
|  |  | ||||||
|  |     /// Name of file to display when printing | ||||||
|  |     pub filename: Option<&'a str>, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
|   | |||||||
| @@ -231,7 +231,7 @@ impl<'a> Printer for InteractivePrinter<'a> { | |||||||
|                     InputFile::Ordinary(filename) => { |                     InputFile::Ordinary(filename) => { | ||||||
|                         format!("file '{}'", filename.to_string_lossy()) |                         format!("file '{}'", filename.to_string_lossy()) | ||||||
|                     } |                     } | ||||||
|                     _ => "STDIN".into(), |                     _ => self.config.filename.unwrap_or("STDIN").to_owned(), | ||||||
|                 }; |                 }; | ||||||
|  |  | ||||||
|                 writeln!( |                 writeln!( | ||||||
| @@ -267,7 +267,10 @@ impl<'a> Printer for InteractivePrinter<'a> { | |||||||
|  |  | ||||||
|         let (prefix, name) = match file { |         let (prefix, name) = match file { | ||||||
|             InputFile::Ordinary(filename) => ("File: ", filename.to_string_lossy()), |             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 { |         let mode = match self.content_type { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user