on master help sign in
Write error messages to pager, if attached

closes #946

commit48a7ce3bf23edee81274e6448cf98288ec6f7d31 authorsharkdp <davidpeter@web.de> dateSat 25 Apr 2020, 12:25 (7 years ago) parent3bcc4d0
4 files changed+37−10
src/bin/bat/main.rs+2−1 src/controller.rs+14−5 src/error.rs+7−4 src/output.rs+14
src/bin/bat/main.rs+2−1
@@ -210,7 +210,8 @@ fn main() {
210210
211211 match result {
212212 Err(error) => {
213− default_error_handler(&error);
213+ let stderr = std::io::stderr();
214+ default_error_handler(&error, &mut stderr.lock());
214215 process::exit(1);
215216 }
216217 Ok(false) => {
src/controller.rs+14−5
@@ -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)?;
src/error.rs+7−4
@@ -1,4 +1,5 @@
11use error_chain::error_chain;
2+use std::io::Write;
23
34error_chain! {
45 foreign_links {
@@ -11,7 +12,7 @@ error_chain! {
1112 }
1213}
1314
14−pub fn default_error_handler(error: &Error) {
15+pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
1516 use ansi_term::Colour::Red;
1617
1718 match error {
@@ -21,14 +22,16 @@ pub fn default_error_handler(error: &Error) {
2122 ::std::process::exit(0);
2223 }
2324 Error(ErrorKind::SerdeYamlError(_), _) => {
24− eprintln!(
25+ writeln!(
26+ output,
2527 "{}: Error while parsing metadata.yaml file: {}",
2628 Red.paint("[bat error]"),
2729 error
28− );
30+ )
31+ .ok();
2932 }
3033 _ => {
31− eprintln!("{}: {}", Red.paint("[bat error]"), error);
34+ writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
3235 }
3336 };
3437}
src/output.rs+14
@@ -125,6 +125,20 @@ impl OutputType {
125125 OutputType::Stdout(io::stdout())
126126 }
127127
128+ #[cfg(feature = "paging")]
129+ pub(crate) fn is_pager(&self) -> bool {
130+ if let OutputType::Pager(_) = self {
131+ true
132+ } else {
133+ false
134+ }
135+ }
136+
137+ #[cfg(not(feature = "paging"))]
138+ pub(crate) fn is_pager(&self) -> bool {
139+ false
140+ }
141+
128142 pub fn handle(&mut self) -> Result<&mut dyn Write> {
129143 Ok(match *self {
130144 #[cfg(feature = "paging")]