Handle broken pipes, closes #9

pull/24/head
sharkdp 7 years ago
parent de11558ad3
commit 1f2bcf57ba

@ -8,7 +8,7 @@ extern crate syntect;
extern crate clap; extern crate clap;
use std::collections::HashMap; 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::path::Path;
use std::process; use std::process;
@ -37,11 +37,13 @@ type LineChanges = HashMap<u32, LineChange>;
const PANEL_WIDTH: usize = 7; const PANEL_WIDTH: usize = 7;
const GRID_COLOR: u8 = 238; 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 bar = "─".repeat(term_width - (PANEL_WIDTH + 1));
let line = format!("{}{}{}", "─".repeat(PANEL_WIDTH), grid_char, bar); 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<P: AsRef<Path>>( fn print_file<P: AsRef<Path>>(
@ -52,20 +54,24 @@ fn print_file<P: AsRef<Path>>(
) -> io::Result<()> { ) -> io::Result<()> {
let mut highlighter = HighlightFile::new(filename.as_ref().clone(), syntax_set, theme)?; 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 = Term::stdout();
let (_, term_width) = term.size(); let (_, term_width) = term.size();
let term_width = term_width as usize; 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), " ".repeat(PANEL_WIDTH),
Fixed(GRID_COLOR).paint("│"), Fixed(GRID_COLOR).paint("│"),
White.bold().paint(filename.as_ref().to_string_lossy()) 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() { for (idx, maybe_line) in highlighter.reader.lines().enumerate() {
let line_nr = idx + 1; let line_nr = idx + 1;
@ -84,16 +90,17 @@ fn print_file<P: AsRef<Path>>(
Style::default().paint(" ") Style::default().paint(" ")
}; };
println!( write!(
"{} {} {} {}", handle,
"{} {} {} {}\n",
Fixed(244).paint(format!("{:4}", line_nr)), Fixed(244).paint(format!("{:4}", line_nr)),
line_change, line_change,
Fixed(GRID_COLOR).paint("│"), Fixed(GRID_COLOR).paint("│"),
as_24_bit_terminal_escaped(&regions, false) as_24_bit_terminal_escaped(&regions, false)
); )?;
} }
print_horizontal_line('┴', term_width); print_horizontal_line(&mut handle, '┴', term_width)?;
Ok(()) Ok(())
} }
@ -196,7 +203,9 @@ fn main() {
let result = run(&matches); let result = run(&matches);
if let Err(e) = result { if let Err(e) = result {
eprintln!("{}: {}", Red.paint("[bat error]"), e); if e.kind() != ErrorKind::BrokenPipe {
process::exit(1); eprintln!("{}: {}", Red.paint("[bat error]"), e);
process::exit(1);
}
} }
} }

Loading…
Cancel
Save