Write error messages to pager, if attached

closes #946
This commit is contained in:
sharkdp 2020-04-25 12:25:25 +02:00 committed by David Peter
parent 3bcc4d0d55
commit 48a7ce3bf2
4 changed files with 37 additions and 10 deletions

View File

@ -210,7 +210,8 @@ fn main() {
match result {
Err(error) => {
default_error_handler(&error);
let stderr = std::io::stderr();
default_error_handler(&error, &mut stderr.lock());
process::exit(1);
}
Ok(false) => {

View File

@ -31,7 +31,7 @@ impl<'b> Controller<'b> {
pub fn run_with_error_handler(
&self,
inputs: Vec<Input>,
handle_error: impl Fn(&Error),
handle_error: impl Fn(&Error, &mut dyn Write),
) -> Result<bool> {
let mut output_type;
@ -62,13 +62,23 @@ impl<'b> Controller<'b> {
output_type = OutputType::stdout();
}
let attached_to_pager = output_type.is_pager();
let writer = output_type.handle()?;
let mut no_errors: bool = true;
let stderr = io::stderr();
let print_error = |error: &Error, write: &mut dyn Write| {
if attached_to_pager {
handle_error(error, write);
} else {
handle_error(error, &mut stderr.lock());
}
};
for input in inputs.into_iter() {
match input.open(io::stdin().lock()) {
Err(error) => {
handle_error(&error);
print_error(&error, writer);
no_errors = false;
}
Ok(mut opened_input) => {
@ -123,7 +133,7 @@ impl<'b> Controller<'b> {
);
if let Err(error) = result {
handle_error(&error);
print_error(&error, writer);
no_errors = false;
}
}
@ -138,8 +148,7 @@ impl<'b> Controller<'b> {
printer: &mut dyn Printer,
writer: &mut dyn Write,
input: &mut OpenedInput,
#[cfg(feature = "git")]
line_changes: &Option<LineChanges>,
#[cfg(feature = "git")] line_changes: &Option<LineChanges>,
) -> Result<()> {
if !input.reader.first_line.is_empty() || self.config.style_components.header() {
printer.print_header(writer, input)?;

View File

@ -1,4 +1,5 @@
use error_chain::error_chain;
use std::io::Write;
error_chain! {
foreign_links {
@ -11,7 +12,7 @@ error_chain! {
}
}
pub fn default_error_handler(error: &Error) {
pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
use ansi_term::Colour::Red;
match error {
@ -21,14 +22,16 @@ pub fn default_error_handler(error: &Error) {
::std::process::exit(0);
}
Error(ErrorKind::SerdeYamlError(_), _) => {
eprintln!(
writeln!(
output,
"{}: Error while parsing metadata.yaml file: {}",
Red.paint("[bat error]"),
error
);
)
.ok();
}
_ => {
eprintln!("{}: {}", Red.paint("[bat error]"), error);
writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
}
};
}

View File

@ -125,6 +125,20 @@ impl OutputType {
OutputType::Stdout(io::stdout())
}
#[cfg(feature = "paging")]
pub(crate) fn is_pager(&self) -> bool {
if let OutputType::Pager(_) = self {
true
} else {
false
}
}
#[cfg(not(feature = "paging"))]
pub(crate) fn is_pager(&self) -> bool {
false
}
pub fn handle(&mut self) -> Result<&mut dyn Write> {
Ok(match *self {
#[cfg(feature = "paging")]