mirror of
https://github.com/starship/starship.git
synced 2024-11-07 17:05:09 +01:00
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
This commit is contained in:
parent
e56e755b66
commit
3f2d6cc068
@ -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
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user