mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
fix(jobs): Add the symbol and number thresholds respecting the threshold option (#2908)
* feat: Add the symbol and number thresholds respecting the threshold option * fix: Maintain the old behavior + add lots of tests * docs: Fix the jobs module documentation
This commit is contained in:
committed by
GitHub
parent
91fe1b747f
commit
7038ae2ec8
@@ -6,6 +6,8 @@ use starship_module_config_derive::ModuleConfig;
|
||||
#[derive(Clone, ModuleConfig, Serialize)]
|
||||
pub struct JobsConfig<'a> {
|
||||
pub threshold: i64,
|
||||
pub symbol_threshold: i64,
|
||||
pub number_threshold: i64,
|
||||
pub format: &'a str,
|
||||
pub symbol: &'a str,
|
||||
pub style: &'a str,
|
||||
@@ -16,6 +18,8 @@ impl<'a> Default for JobsConfig<'a> {
|
||||
fn default() -> Self {
|
||||
JobsConfig {
|
||||
threshold: 1,
|
||||
symbol_threshold: 1,
|
||||
number_threshold: 2,
|
||||
format: "[$symbol$number]($style) ",
|
||||
symbol: "✦",
|
||||
style: "bold blue",
|
||||
|
||||
+203
-14
@@ -8,14 +8,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let mut module = context.new_module("jobs");
|
||||
let config = JobsConfig::try_load(module.config);
|
||||
|
||||
let props = &context.properties;
|
||||
let num_of_jobs = props
|
||||
.get("jobs")
|
||||
.map_or("0", String::as_str)
|
||||
.trim()
|
||||
.parse::<i64>()
|
||||
.ok()?;
|
||||
|
||||
if config.threshold < 0 {
|
||||
log::warn!(
|
||||
"threshold in [jobs] ({}) was less than zero",
|
||||
@@ -24,20 +16,68 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
return None;
|
||||
}
|
||||
|
||||
if num_of_jobs == 0 && config.threshold > 0 {
|
||||
if config.symbol_threshold < 0 {
|
||||
log::warn!(
|
||||
"symbol_threshold in [jobs] ({}) was less than zero",
|
||||
config.symbol_threshold
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
let module_number = if num_of_jobs > config.threshold || config.threshold == 0 {
|
||||
num_of_jobs.to_string()
|
||||
if config.number_threshold < 0 {
|
||||
log::warn!(
|
||||
"number_threshold in [jobs] ({}) was less than zero",
|
||||
config.number_threshold
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
let props = &context.properties;
|
||||
let num_of_jobs = props
|
||||
.get("jobs")
|
||||
.map_or("0", String::as_str)
|
||||
.trim()
|
||||
.parse::<i64>()
|
||||
.ok()?;
|
||||
|
||||
if num_of_jobs == 0
|
||||
&& config.threshold > 0
|
||||
&& config.number_threshold > 0
|
||||
&& config.symbol_threshold > 0
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if num_of_jobs > config.threshold || config.threshold == 0 {
|
||||
module_symbol = config.symbol;
|
||||
module_number = num_of_jobs.to_string();
|
||||
}
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
||||
formatter
|
||||
.map_meta(|var, _| match var {
|
||||
"symbol" => Some(config.symbol),
|
||||
"symbol" => Some(module_symbol),
|
||||
_ => None,
|
||||
})
|
||||
.map_style(|variable| match variable {
|
||||
@@ -91,6 +131,96 @@ mod test {
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_2_job_2() {
|
||||
let actual = ModuleRenderer::new("jobs")
|
||||
@@ -105,6 +235,35 @@ mod test {
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_2_job_3() {
|
||||
let actual = ModuleRenderer::new("jobs")
|
||||
@@ -119,6 +278,36 @@ mod test {
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_0_job_0() {
|
||||
let actual = ModuleRenderer::new("jobs")
|
||||
|
||||
Reference in New Issue
Block a user