From c5d193f483706debeef2ee7a6ef1aa6d4e828b00 Mon Sep 17 00:00:00 2001 From: Gustav Eek Date: Mon, 25 Dec 2023 19:41:10 +0100 Subject: [PATCH] Src. Provide CUA priority Add a function *lib.icalprio*, which returns the CUA string mapping of priorities. In output, add a version of post in the case theses should be printed. Test *lib.icalprio*. --- src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8cd1b48..cd6bb1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,8 +128,9 @@ pub fn input() -> (Conf, Vec) { pub fn output(conf: &Conf, prio: &Vec, ranked: &Vec) { let digs: usize = (prio.len() as f32).log(10.0).ceil() as usize; + let cua = icalprio(&prio); - for (i, (p, l)) in prio.iter().zip(ranked).enumerate() { + for (i, ((p, l), c)) in prio.iter().zip(ranked).zip(cua).enumerate() { let pre = if conf.number != "" { conf.number.replace("{}", format!( // Replace template due @@ -137,7 +138,10 @@ pub fn output(conf: &Conf, prio: &Vec, ranked: &Vec) { } else { conf.bullet.clone() }; - let pst = if conf.bullet.contains("|") {" | "} else {"\t"}; + let pst = if conf.bullet.contains("|") {" | ".to_owned()} + else if conf.ical != "" { + format!(" {} \t", conf.ical.replace("{}", &c))} + else {"\t".to_owned()}; println!("{}{:2.0} %{}{}", pre, 100.0 * p, pst, l) } @@ -213,6 +217,24 @@ pub fn lognormal(n: i32, std: f64) -> Vec { normalize(prio) } +pub fn icalprio(prio: &Vec) -> Vec { + const THR: usize = 20; + const ICALPRIOMAX: i8 = 9; + const CUA_LONG: [&str; 10] = + ["", "A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"]; + const CUA_SHOR: [&str; 10] = + ["", "A", "A", "A", "B", "B", "B", "C", "C", "C"]; + let prio_max = prio.iter().copied().fold(f64::NEG_INFINITY, f64::max); + let int = prio.iter().map( + |x| ((ICALPRIOMAX as f64) * + (1.0 + 3e-16 - x / prio_max)).ceil() as usize); + return if prio.len() >= THR { + int.map(|i| CUA_LONG[i].to_owned()).collect() + } else { + int.map(|i| CUA_SHOR[i].to_owned()).collect() + } +} + #[derive(Debug)] pub struct Conf { @@ -365,3 +387,19 @@ fn bullout() { .split("\n").map(|x| x.to_owned()).collect(); output(&conf, &prio, &ranked); } + +#[test] + +fn icalout() { + let prio = vec![0.6, 0.3, 0.1]; + let exp = vec!["A", "B", "C"]; + let ical = icalprio(&prio); + assert_eq!(exp, ical); + let prio = vec![ + 0.25, 0.17, 0.13, 0.10, 0.08, 0.07, 0.06, 0.05, 0.04, 0.03, + 0.02, 0.01]; + let exp = vec![ + "A", "A", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C"]; + let ical = icalprio(&prio); + assert_eq!(exp, ical); +} -- 2.39.2