diff options
author | Catherine Noll <noll.catherine@gmail.com> | 2022-11-20 19:56:45 -0500 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2023-07-29 13:50:45 -0400 |
commit | afef5e65012559e95f72fbe743986e6e55dcea9c (patch) | |
tree | 737058077eb6ff5fabea1719cd6e913ec73a2b68 | |
parent | ffc83bae3ff690b0cff0ada0064a263ce4b41269 (diff) |
Update `--doctor` handling for *PAGER environment variables
-rw-r--r-- | src/subcommands/doctor/pager_env.rs | 76 |
1 files changed, 51 insertions, 25 deletions
diff --git a/src/subcommands/doctor/pager_env.rs b/src/subcommands/doctor/pager_env.rs index 1784cef4..eb1fe0b6 100644 --- a/src/subcommands/doctor/pager_env.rs +++ b/src/subcommands/doctor/pager_env.rs @@ -9,46 +9,61 @@ pub struct PagerEnvVars { env_vars: HashMap<String, String>, } -const UNSUPPORTED_PAGER_SUFFIX: &str = "PAGER"; +const PAGER_ENV_VARS: [&str; 4] = ["PAGER", "DELTA_PAGER", "BAT_PAGER", "GIT_PAGER"]; impl PagerEnvVars { pub fn probe() -> Self { PagerEnvVars { env_vars: env::vars() - .filter(|(k, _)| k.ends_with(UNSUPPORTED_PAGER_SUFFIX)) + .filter_map(|(k, v)| get_env_kv(k, v)) + .into_iter() .collect(), } } + + fn selected_pager_is_less(&self) -> Option<bool> { + match self.env_vars.get("DELTA_PAGER") { + Some(_v) => Some(val_contains_less(_v)), + None => match self.env_vars.get("BAT_PAGER") { + Some(_v) => Some(val_contains_less(_v)), + None => match self.env_vars.get("PAGER") { + Some(_v) => Some(val_contains_less(_v)), + None => None, + }, + }, + } + } } impl Diagnostic for PagerEnvVars { fn report(&self) -> String { - let output_str = self - .env_vars - .iter() - .map(|(k, v)| format!("- {} = {}", k, v)) - .collect::<Vec<String>>() - .join("\n"); - "`*PAGER` environment variables: \n".to_string() + &output_str + let vars = &self.env_vars; + match self.selected_pager_is_less() { + Some(v) => match v { + true => "Your selected pager is `less`".to_owned(), + false => { + let vars_str = vars + .iter() + .map(|(k, v)| format!(" - {} = {}", k, v)) + .collect::<Vec<String>>() + .join("\n"); + return "The pager specified by your *PAGER environment variables is not `less`. You have:\n".to_owned() + &vars_str; + } + }, + None => "Your selected pager is the system `less`".to_owned(), + } } fn diagnose(&self) -> Health { - let diagnosis_prefix = "Unsupported `*PAGER` environment variables are set: \n".to_string(); - let remedy_prefix = "Unset `*PAGER` environment variables: \n".to_string(); - - let output_str_items = self - .env_vars - .iter() - .map(|(k, v)| format!("- {} = {}", k, v)) - .collect::<Vec<String>>() - .join("\n"); - - match self.env_vars.keys().len() > 0 { - true => Unhealthy( - diagnosis_prefix + &*output_str_items, - remedy_prefix + &*output_str_items, - ), - false => Healthy, + let diagnosis = self.report(); + let remedy = "Set `DELTA_PAGER` to your preferred version of `less`, or unset it to use the system `less`.".to_string(); + + match self.selected_pager_is_less() { + Some(v) => match v { + true => Healthy, + false => Unhealthy(diagnosis, remedy), + }, + None => Healthy, } } @@ -59,3 +74,14 @@ impl Diagnostic for PagerEnvVars { } } } + +fn get_env_kv(k: String, v: String) -> Option<(String, String)> { + match PAGER_ENV_VARS.contains(&k.as_str()) { + true => Some((k, v)), + false => None, + } +} + +fn val_contains_less(val: &str) -> bool { + return val.to_ascii_lowercase().contains("less"); +} |