@@ -31,7 +31,7 @@ impl<'b> Controller<'b> {
3131 pub fn run_with_error_handler(
3232 &self,
3333 inputs: Vec<Input>,
34− handle_error: impl Fn(&Error),
34+ handle_error: impl Fn(&Error, &mut dyn Write),
3535 ) -> Result<bool> {
3636 let mut output_type;
3737
@@ -62,13 +62,23 @@ impl<'b> Controller<'b> {
6262 output_type = OutputType::stdout();
6363 }
6464
65+ let attached_to_pager = output_type.is_pager();
6566 let writer = output_type.handle()?;
6667 let mut no_errors: bool = true;
6768
69+ let stderr = io::stderr();
70+ let print_error = |error: &Error, write: &mut dyn Write| {
71+ if attached_to_pager {
72+ handle_error(error, write);
73+ } else {
74+ handle_error(error, &mut stderr.lock());
75+ }
76+ };
77+
6878 for input in inputs.into_iter() {
6979 match input.open(io::stdin().lock()) {
7080 Err(error) => {
71− handle_error(&error);
81+ print_error(&error, writer);
7282 no_errors = false;
7383 }
7484 Ok(mut opened_input) => {
@@ -123,7 +133,7 @@ impl<'b> Controller<'b> {
123133 );
124134
125135 if let Err(error) = result {
126− handle_error(&error);
136+ print_error(&error, writer);
127137 no_errors = false;
128138 }
129139 }
@@ -138,8 +148,7 @@ impl<'b> Controller<'b> {
138148 printer: &mut dyn Printer,
139149 writer: &mut dyn Write,
140150 input: &mut OpenedInput,
141− #[cfg(feature = "git")]
142− line_changes: &Option<LineChanges>,
151+ #[cfg(feature = "git")] line_changes: &Option<LineChanges>,
143152 ) -> Result<()> {
144153 if !input.reader.first_line.is_empty() || self.config.style_components.header() {
145154 printer.print_header(writer, input)?;