From 1f2bcf57baf92c83f8668f7908532d2b849e83ee Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 22 Apr 2018 14:37:32 +0200 Subject: [PATCH] Handle broken pipes, closes #9 --- src/main.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index cc7054ad..904a6873 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ extern crate syntect; extern crate clap; use std::collections::HashMap; -use std::io::{self, BufRead, Result}; +use std::io::{self, BufRead, Result, Write, ErrorKind, StdoutLock}; use std::path::Path; use std::process; @@ -37,11 +37,13 @@ type LineChanges = HashMap; const PANEL_WIDTH: usize = 7; const GRID_COLOR: u8 = 238; -fn print_horizontal_line(grid_char: char, term_width: usize) { +fn print_horizontal_line(handle: &mut StdoutLock, grid_char: char, term_width: usize) -> io::Result<()> { let bar = "─".repeat(term_width - (PANEL_WIDTH + 1)); let line = format!("{}{}{}", "─".repeat(PANEL_WIDTH), grid_char, bar); - println!("{}", Fixed(GRID_COLOR).paint(line)); + write!(handle, "{}\n", Fixed(GRID_COLOR).paint(line))?; + + Ok(()) } fn print_file>( @@ -52,20 +54,24 @@ fn print_file>( ) -> io::Result<()> { let mut highlighter = HighlightFile::new(filename.as_ref().clone(), syntax_set, theme)?; + let stdout = io::stdout(); + let mut handle = stdout.lock(); + let term = Term::stdout(); let (_, term_width) = term.size(); let term_width = term_width as usize; - print_horizontal_line('┬', term_width); + print_horizontal_line(&mut handle, '┬', term_width)?; - println!( - "{}{} {}", + write!( + handle, + "{}{} {}\n", " ".repeat(PANEL_WIDTH), Fixed(GRID_COLOR).paint("│"), White.bold().paint(filename.as_ref().to_string_lossy()) - ); + )?; - print_horizontal_line('┼', term_width); + print_horizontal_line(&mut handle, '┼', term_width)?; for (idx, maybe_line) in highlighter.reader.lines().enumerate() { let line_nr = idx + 1; @@ -84,16 +90,17 @@ fn print_file>( Style::default().paint(" ") }; - println!( - "{} {} {} {}", + write!( + handle, + "{} {} {} {}\n", Fixed(244).paint(format!("{:4}", line_nr)), line_change, Fixed(GRID_COLOR).paint("│"), as_24_bit_terminal_escaped(®ions, false) - ); + )?; } - print_horizontal_line('┴', term_width); + print_horizontal_line(&mut handle, '┴', term_width)?; Ok(()) } @@ -196,7 +203,9 @@ fn main() { let result = run(&matches); if let Err(e) = result { - eprintln!("{}: {}", Red.paint("[bat error]"), e); - process::exit(1); + if e.kind() != ErrorKind::BrokenPipe { + eprintln!("{}: {}", Red.paint("[bat error]"), e); + process::exit(1); + } } }