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 { match result {
Err(error) => { Err(error) => {
default_error_handler(&error); let stderr = std::io::stderr();
default_error_handler(&error, &mut stderr.lock());
process::exit(1); process::exit(1);
} }
Ok(false) => { Ok(false) => {

View File

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

View File

@ -1,4 +1,5 @@
use error_chain::error_chain; use error_chain::error_chain;
use std::io::Write;
error_chain! { error_chain! {
foreign_links { 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; use ansi_term::Colour::Red;
match error { match error {
@ -21,14 +22,16 @@ pub fn default_error_handler(error: &Error) {
::std::process::exit(0); ::std::process::exit(0);
} }
Error(ErrorKind::SerdeYamlError(_), _) => { Error(ErrorKind::SerdeYamlError(_), _) => {
eprintln!( writeln!(
output,
"{}: Error while parsing metadata.yaml file: {}", "{}: Error while parsing metadata.yaml file: {}",
Red.paint("[bat error]"), Red.paint("[bat error]"),
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()) 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> { pub fn handle(&mut self) -> Result<&mut dyn Write> {
Ok(match *self { Ok(match *self {
#[cfg(feature = "paging")] #[cfg(feature = "paging")]