summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnthony Huang <anthuang@umich.edu>2021-04-20 09:30:22 -0700
committerGitHub <noreply@github.com>2021-04-20 18:30:22 +0200
commit3f2d6cc068fc5fe9cfbf62dbeff5dfb1aa1f9114 (patch)
tree5e5cdec678f94ed248fc52fd30e8576c4fcab282
parente56e755b66bdc844055a2ac9e78b04fdb4bb974a (diff)
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
-rw-r--r--docs/config/README.md3
-rw-r--r--src/modules/jobs.rs41
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<Module<'a>> {
.trim()
.parse::<i64>()
.ok()?;
- if num_of_jobs == 0 {
+
+ if config.threshold < 0 {
+ log::warn!(
+ "threshold in [jobs] ({}) was less than zero",
+ config.threshold
+ );
+ return None;
+ }
+
+ if num_of_jobs == 0 && config.threshold > 0 {
return None;
}
- let module_number = if num_of_jobs > config.threshold {
+ 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);
+ }
}