From 3f2d6cc068fc5fe9cfbf62dbeff5dfb1aa1f9114 Mon Sep 17 00:00:00 2001 From: Anthony Huang Date: Tue, 20 Apr 2021 09:30:22 -0700 Subject: [PATCH] fix(jobs): Handle zero jobs with zero thresholds (#2613) * Handle zero jobs with negative thresholds * Allow for zero threshold only * Log when threshold is less than zero * Address comments * Add docs --- docs/config/README.md | 3 ++- src/modules/jobs.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 1433f4691..b7e53246b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1453,7 +1453,8 @@ symbol = "🌟 " The `jobs` module shows the current number of jobs running. The module will be shown only if there are background jobs running. The module will show the number of jobs running if there is more than 1 job, or -more than the `threshold` config value, if it exists. +more than the `threshold` config value, if it exists. If `threshold` is set to 0, +then the module will also show when there are 0 jobs running. ::: warning diff --git a/src/modules/jobs.rs b/src/modules/jobs.rs index ecb75aff4..e045ec0fd 100644 --- a/src/modules/jobs.rs +++ b/src/modules/jobs.rs @@ -15,11 +15,20 @@ pub fn module<'a>(context: &'a Context) -> Option> { .trim() .parse::() .ok()?; - if num_of_jobs == 0 { + + if config.threshold < 0 { + log::warn!( + "threshold in [jobs] ({}) was less than zero", + config.threshold + ); return None; } - let module_number = if num_of_jobs > config.threshold { + if num_of_jobs == 0 && config.threshold > 0 { + return None; + } + + let module_number = if num_of_jobs > config.threshold || config.threshold == 0 { num_of_jobs.to_string() } else { "".to_string() @@ -109,4 +118,32 @@ mod test { let expected = Some(format!("{} ", Color::Blue.bold().paint("✦3"))); assert_eq!(expected, actual); } + + #[test] + fn config_0_job_0() { + let actual = ModuleRenderer::new("jobs") + .config(toml::toml! { + [jobs] + threshold = 0 + }) + .jobs(0) + .collect(); + + let expected = Some(format!("{} ", Color::Blue.bold().paint("✦0"))); + assert_eq!(expected, actual); + } + + #[test] + fn config_0_job_1() { + let actual = ModuleRenderer::new("jobs") + .config(toml::toml! { + [jobs] + threshold = 0 + }) + .jobs(1) + .collect(); + + let expected = Some(format!("{} ", Color::Blue.bold().paint("✦1"))); + assert_eq!(expected, actual); + } }