2020-07-07 22:45:32 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
2019-08-12 17:42:33 +00:00
|
|
|
|
2019-10-10 08:21:52 +00:00
|
|
|
use crate::configs::jobs::JobsConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-10-10 08:21:52 +00:00
|
|
|
|
2019-08-12 17:42:33 +00:00
|
|
|
/// Creates a segment to show if there are any active jobs running
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("jobs");
|
2020-07-07 22:45:32 +00:00
|
|
|
let config = JobsConfig::try_load(module.config);
|
2019-08-12 17:42:33 +00:00
|
|
|
|
2021-08-14 13:29:25 +00:00
|
|
|
if config.threshold < 0 {
|
|
|
|
log::warn!(
|
|
|
|
"threshold in [jobs] ({}) was less than zero",
|
|
|
|
config.threshold
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.symbol_threshold < 0 {
|
|
|
|
log::warn!(
|
|
|
|
"symbol_threshold in [jobs] ({}) was less than zero",
|
|
|
|
config.symbol_threshold
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.number_threshold < 0 {
|
|
|
|
log::warn!(
|
|
|
|
"number_threshold in [jobs] ({}) was less than zero",
|
|
|
|
config.number_threshold
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-10-20 08:26:27 +00:00
|
|
|
let props = &context.properties;
|
|
|
|
let num_of_jobs = props
|
|
|
|
.get("jobs")
|
2021-07-29 18:27:46 +00:00
|
|
|
.map_or("0", String::as_str)
|
2019-08-13 03:41:59 +00:00
|
|
|
.trim()
|
2019-08-12 17:42:33 +00:00
|
|
|
.parse::<i64>()
|
|
|
|
.ok()?;
|
2021-04-20 16:30:22 +00:00
|
|
|
|
2021-08-14 13:29:25 +00:00
|
|
|
if num_of_jobs == 0
|
|
|
|
&& config.threshold > 0
|
|
|
|
&& config.number_threshold > 0
|
|
|
|
&& config.symbol_threshold > 0
|
|
|
|
{
|
2021-04-20 16:30:22 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-08-14 13:29:25 +00:00
|
|
|
let default_threshold = 1;
|
|
|
|
let mut module_symbol = "";
|
|
|
|
let mut module_number = String::new();
|
|
|
|
if config.threshold != default_threshold {
|
|
|
|
log::warn!("`threshold` in [jobs] is deprecated . Please remove it and use `symbol_threshold` and `number_threshold`.");
|
|
|
|
|
|
|
|
// The symbol should be shown if there are *any* background
|
|
|
|
// jobs running.
|
|
|
|
if num_of_jobs > 0 {
|
|
|
|
module_symbol = config.symbol;
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
|
2021-08-14 13:29:25 +00:00
|
|
|
if num_of_jobs > config.threshold || config.threshold == 0 {
|
|
|
|
module_symbol = config.symbol;
|
|
|
|
module_number = num_of_jobs.to_string();
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
} else {
|
2021-08-14 13:29:25 +00:00
|
|
|
if num_of_jobs >= config.symbol_threshold {
|
|
|
|
module_symbol = config.symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
if num_of_jobs >= config.number_threshold {
|
|
|
|
module_number = num_of_jobs.to_string();
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|var, _| match var {
|
2021-08-14 13:29:25 +00:00
|
|
|
"symbol" => Some(module_symbol),
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"number" => Some(Ok(module_number.clone())),
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `jobs`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-08-12 17:42:33 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::test::ModuleRenderer;
|
|
|
|
use ansi_term::Color;
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn config_blank_job_0() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("jobs").jobs(0).collect();
|
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn config_blank_job_1() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("jobs").jobs(1).collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn config_blank_job_2() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("jobs").jobs(2).collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦2")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-08-14 13:29:25 +00:00
|
|
|
#[test]
|
|
|
|
fn config_default_is_present_jobs_1() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
threshold = 1
|
|
|
|
})
|
|
|
|
.jobs(1)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_default_is_present_jobs_2() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
threshold = 1
|
|
|
|
})
|
|
|
|
.jobs(2)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦2")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_conflicting_thresholds_default_jobs_1() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
threshold = 1
|
|
|
|
number_threshold = 2
|
|
|
|
})
|
|
|
|
.jobs(1)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_conflicting_thresholds_default_no_symbol_jobs_1() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
threshold = 1
|
|
|
|
symbol_threshold = 0
|
|
|
|
number_threshold = 2
|
|
|
|
})
|
|
|
|
.jobs(1)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_conflicting_thresholds_no_symbol_jobs_1() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
threshold = 0
|
|
|
|
symbol_threshold = 0
|
|
|
|
number_threshold = 2
|
|
|
|
})
|
|
|
|
.jobs(1)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦1")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_conflicting_thresholds_jobs_2() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
threshold = 1
|
|
|
|
number_threshold = 2
|
|
|
|
})
|
|
|
|
.jobs(2)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦2")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn config_2_job_2() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
threshold = 2
|
|
|
|
})
|
|
|
|
.jobs(2)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-08-14 13:29:25 +00:00
|
|
|
#[test]
|
|
|
|
fn config_number_2_job_2() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
number_threshold = 2
|
|
|
|
})
|
|
|
|
.jobs(2)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦2")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_number_2_symbol_3_job_2() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
number_threshold = 2
|
|
|
|
symbol_threshold = 3
|
|
|
|
})
|
|
|
|
.jobs(2)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("2")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn config_2_job_3() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
threshold = 2
|
|
|
|
})
|
|
|
|
.jobs(3)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦3")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2021-04-20 16:30:22 +00:00
|
|
|
|
2021-08-14 13:29:25 +00:00
|
|
|
#[test]
|
|
|
|
fn config_thresholds_0_jobs_0() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
number_threshold = 0
|
|
|
|
symbol_threshold = 0
|
|
|
|
})
|
|
|
|
.jobs(0)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦0")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_thresholds_0_jobs_1() {
|
|
|
|
let actual = ModuleRenderer::new("jobs")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[jobs]
|
|
|
|
number_threshold = 0
|
|
|
|
symbol_threshold = 0
|
|
|
|
})
|
|
|
|
.jobs(1)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦1")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:30:22 +00:00
|
|
|
#[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);
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
}
|