pub fn output(conf: &Conf, prio: &Vec<f64>, ranked: &Vec<String>) {
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
} 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)
}
normalize(prio)
}
+pub fn icalprio(prio: &Vec<f64>) -> Vec<String> {
+ 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 {
.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);
+}