1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-09-12 00:02:28 +01:00

Implemented --file-name for multiple files + tests

This commit is contained in:
Kyle Criddle
2020-03-24 18:26:00 -06:00
parent cfa2cb6ec7
commit 59f2e2d58d
5 changed files with 80 additions and 21 deletions

View File

@@ -34,7 +34,12 @@ use crate::terminal::{as_terminal_escaped, to_ansi_color};
use crate::Config;
pub trait Printer {
fn print_header(&mut self, handle: &mut dyn Write, file: InputFile) -> Result<()>;
fn print_header(
&mut self,
handle: &mut dyn Write,
file: InputFile,
file_name: Option<&str>,
) -> Result<()>;
fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()>;
fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()>;
@@ -57,7 +62,12 @@ impl SimplePrinter {
}
impl Printer for SimplePrinter {
fn print_header(&mut self, _handle: &mut dyn Write, _file: InputFile) -> Result<()> {
fn print_header(
&mut self,
_handle: &mut dyn Write,
_file: InputFile,
_file_name: Option<&str>,
) -> Result<()> {
Ok(())
}
@@ -224,15 +234,20 @@ impl<'a> InteractivePrinter<'a> {
}
impl<'a> Printer for InteractivePrinter<'a> {
fn print_header(&mut self, handle: &mut dyn Write, file: InputFile) -> Result<()> {
fn print_header(
&mut self,
handle: &mut dyn Write,
file: InputFile,
file_name: Option<&str>,
) -> Result<()> {
if !self.config.output_components.header() {
if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
let input = match file {
InputFile::Ordinary(filename) => format!(
"file '{}'",
self.config.filename.unwrap_or(&filename.to_string_lossy())
file_name.unwrap_or(&filename.to_string_lossy())
),
_ => self.config.filename.unwrap_or("STDIN").to_owned(),
_ => file_name.unwrap_or("STDIN").to_owned(),
};
writeln!(
@@ -269,17 +284,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
let (prefix, name) = match file {
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()),
Cow::from(file_name.unwrap_or(&filename.to_string_lossy()).to_owned()),
),
_ => ("File: ", Cow::from(file_name.unwrap_or("STDIN").to_owned())),
};
let mode = match self.content_type {