summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Otto <th1000s@posteo.net>2021-10-14 00:15:56 +0200
committerThomas Otto <th1000s@posteo.net>2021-10-16 14:07:28 +0200
commit636b56729be21a17a5520988f0d23f5d26e9a277 (patch)
tree93098335afe26490f872fc937fff4683e32a2036
parent85ba3ba5782e04d514998f33f97b5f44cf859e86 (diff)
Add fatal() function to handle exiting on errors
Panic when testing or exit with return value 2.
-rw-r--r--src/config.rs6
-rw-r--r--src/main.rs13
2 files changed, 15 insertions, 4 deletions
diff --git a/src/config.rs b/src/config.rs
index fbd58044..5a013a1b 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -13,6 +13,7 @@ use crate::cli;
use crate::color;
use crate::delta::State;
use crate::env;
+use crate::fatal;
use crate::features::navigate;
use crate::features::side_by_side;
use crate::git_config::{GitConfig, GitConfigEntry};
@@ -193,10 +194,7 @@ impl From<cli::Opt> for Config {
// Note that "default" is not documented
Some("ansi") | Some("default") | None => BgFillMethod::TryAnsiSequence,
Some("spaces") => BgFillMethod::Spaces,
- _ => {
- eprintln!("Invalid option for line-fill-method: Expected \"ansi\" or \"spaces\".");
- process::exit(1);
- }
+ _ => fatal("Invalid option for line-fill-method: Expected \"ansi\" or \"spaces\"."),
};
let navigate_regexp = if opt.navigate || opt.show_themes {
diff --git a/src/main.rs b/src/main.rs
index 1fdf9365..a3587341 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -35,6 +35,19 @@ use crate::bat_utils::assets::{list_languages, HighlightingAssets};
use crate::bat_utils::output::OutputType;
use crate::delta::delta;
+pub fn fatal<T>(errmsg: T) -> !
+where
+ T: AsRef<str> + std::fmt::Display,
+{
+ #[cfg(not(test))]
+ {
+ eprintln!("{}", errmsg);
+ process::exit(2);
+ }
+ #[cfg(test)]
+ panic!("{}\n", errmsg);
+}
+
pub mod errors {
error_chain! {
foreign_links {