Permissive error handling, closes #17

This commit is contained in:
sharkdp 2018-05-19 11:46:41 +02:00 committed by David Peter
parent a0ae089c4a
commit 2a9f5a24ed
2 changed files with 46 additions and 24 deletions

View File

@ -128,11 +128,7 @@ impl HighlightingAssets {
})?)
}
pub fn get_syntax(
&self,
language: Option<&str>,
filename: Option<&str>,
) -> Result<&SyntaxDefinition> {
pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition {
let syntax = match (language, filename) {
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
(None, Some(filename)) => {
@ -142,10 +138,14 @@ impl HighlightingAssets {
// Do not peek at the file (to determine the syntax) if it is a FIFO because they can
// only be read once.
#[cfg(unix)]
let may_read_from_file = !fs::metadata(filename)?.file_type().is_fifo();
let may_read_from_file = !fs::metadata(filename)
.map(|m| m.file_type().is_fifo())
.unwrap_or(false);
if may_read_from_file {
self.syntax_set.find_syntax_for_file(filename)?
self.syntax_set
.find_syntax_for_file(filename)
.unwrap_or(None)
} else {
None
}
@ -153,7 +153,7 @@ impl HighlightingAssets {
(None, None) => None,
};
Ok(syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text()))
syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
}
}

View File

@ -168,7 +168,9 @@ fn print_file(
Ok(())
}
fn run() -> Result<()> {
/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
/// `Some(false)` if any intermediate errors occured (were printed).
fn run() -> Result<bool> {
let app = App::new();
match app.matches.subcommand() {
@ -190,6 +192,8 @@ fn run() -> Result<()> {
} else if cache_matches.is_present("config-dir") {
println!("{}", config_dir());
}
return Ok(true);
}
_ => {
let config = app.config()?;
@ -239,7 +243,7 @@ fn run() -> Result<()> {
println!();
}
return Ok(());
return Ok(true);
}
if app.matches.is_present("list-themes") {
@ -247,37 +251,55 @@ fn run() -> Result<()> {
for (theme, _) in themes.iter() {
println!("{}", theme);
}
return Ok(());
return Ok(true);
}
let mut output_type = OutputType::from_mode(config.paging_mode);
let handle = output_type.handle()?;
let mut printer = Printer::new(handle, &config);
let mut no_errors: bool = true;
for file in &config.files {
printer.line_changes = file.and_then(|filename| get_git_diff(filename));
let syntax = assets.get_syntax(config.language, *file)?;
let syntax = assets.get_syntax(config.language, *file);
print_file(theme, &syntax, &mut printer, *file)?;
let result = print_file(theme, &syntax, &mut printer, *file);
if let Err(error) = result {
handle_error(&error);
no_errors = false;
}
}
Ok(no_errors)
}
}
}
Ok(())
fn handle_error(error: &Error) {
match error {
&Error(ErrorKind::Io(ref io_error), _) if io_error.kind() == io::ErrorKind::BrokenPipe => {
process::exit(0);
}
_ => {
eprintln!("{}: {}", Red.paint("[bat error]"), error);
}
};
}
fn main() {
let result = run();
if let Err(error) = result {
match error {
Error(ErrorKind::Io(ref io_error), _)
if io_error.kind() == io::ErrorKind::BrokenPipe => {}
_ => {
eprintln!("{}: {}", Red.paint("[bat error]"), error);
process::exit(1);
}
};
match result {
Err(error) => {
handle_error(&error);
process::exit(1);
}
Ok(false) => {
process::exit(1);
}
Ok(true) => {
process::exit(0);
}
}
}