mirror of
https://github.com/sharkdp/bat.git
synced 2025-04-14 06:40:39 +01:00
Permissive error handling, closes #17
This commit is contained in:
parent
a0ae089c4a
commit
2a9f5a24ed
@ -128,11 +128,7 @@ impl HighlightingAssets {
|
|||||||
})?)
|
})?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_syntax(
|
pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition {
|
||||||
&self,
|
|
||||||
language: Option<&str>,
|
|
||||||
filename: Option<&str>,
|
|
||||||
) -> Result<&SyntaxDefinition> {
|
|
||||||
let syntax = match (language, filename) {
|
let syntax = match (language, filename) {
|
||||||
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
||||||
(None, Some(filename)) => {
|
(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
|
// Do not peek at the file (to determine the syntax) if it is a FIFO because they can
|
||||||
// only be read once.
|
// only be read once.
|
||||||
#[cfg(unix)]
|
#[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 {
|
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 {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -153,7 +153,7 @@ impl HighlightingAssets {
|
|||||||
(None, None) => None,
|
(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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
54
src/main.rs
54
src/main.rs
@ -168,7 +168,9 @@ fn print_file(
|
|||||||
Ok(())
|
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();
|
let app = App::new();
|
||||||
|
|
||||||
match app.matches.subcommand() {
|
match app.matches.subcommand() {
|
||||||
@ -190,6 +192,8 @@ fn run() -> Result<()> {
|
|||||||
} else if cache_matches.is_present("config-dir") {
|
} else if cache_matches.is_present("config-dir") {
|
||||||
println!("{}", config_dir());
|
println!("{}", config_dir());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(true);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let config = app.config()?;
|
let config = app.config()?;
|
||||||
@ -239,7 +243,7 @@ fn run() -> Result<()> {
|
|||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if app.matches.is_present("list-themes") {
|
if app.matches.is_present("list-themes") {
|
||||||
@ -247,37 +251,55 @@ fn run() -> Result<()> {
|
|||||||
for (theme, _) in themes.iter() {
|
for (theme, _) in themes.iter() {
|
||||||
println!("{}", theme);
|
println!("{}", theme);
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut output_type = OutputType::from_mode(config.paging_mode);
|
let mut output_type = OutputType::from_mode(config.paging_mode);
|
||||||
let handle = output_type.handle()?;
|
let handle = output_type.handle()?;
|
||||||
let mut printer = Printer::new(handle, &config);
|
let mut printer = Printer::new(handle, &config);
|
||||||
|
let mut no_errors: bool = true;
|
||||||
|
|
||||||
for file in &config.files {
|
for file in &config.files {
|
||||||
printer.line_changes = file.and_then(|filename| get_git_diff(filename));
|
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() {
|
fn main() {
|
||||||
let result = run();
|
let result = run();
|
||||||
|
|
||||||
if let Err(error) = result {
|
match result {
|
||||||
match error {
|
Err(error) => {
|
||||||
Error(ErrorKind::Io(ref io_error), _)
|
handle_error(&error);
|
||||||
if io_error.kind() == io::ErrorKind::BrokenPipe => {}
|
process::exit(1);
|
||||||
_ => {
|
}
|
||||||
eprintln!("{}: {}", Red.paint("[bat error]"), error);
|
Ok(false) => {
|
||||||
|
process::exit(1);
|
||||||
process::exit(1);
|
}
|
||||||
}
|
Ok(true) => {
|
||||||
};
|
process::exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user