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
|
|
|
|
2019-10-20 08:26:27 +00:00
|
|
|
let props = &context.properties;
|
|
|
|
let num_of_jobs = props
|
|
|
|
.get("jobs")
|
|
|
|
.unwrap_or(&"0".into())
|
2019-08-13 03:41:59 +00:00
|
|
|
.trim()
|
2019-08-12 17:42:33 +00:00
|
|
|
.parse::<i64>()
|
|
|
|
.ok()?;
|
|
|
|
if num_of_jobs == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
|
|
|
|
let module_number = if num_of_jobs > config.threshold {
|
|
|
|
num_of_jobs.to_string()
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|var, _| match var {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"number" => Some(Ok(module_number.clone())),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_blank_job_0() -> io::Result<()> {
|
|
|
|
let actual = ModuleRenderer::new("jobs").jobs(0).collect();
|
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_blank_job_1() -> io::Result<()> {
|
|
|
|
let actual = ModuleRenderer::new("jobs").jobs(1).collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_blank_job_2() -> io::Result<()> {
|
|
|
|
let actual = ModuleRenderer::new("jobs").jobs(2).collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} ", Color::Blue.bold().paint("✦2")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_2_job_2() -> io::Result<()> {
|
|
|
|
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);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_2_job_3() -> io::Result<()> {
|
|
|
|
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);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|