// Search patterns
let bullet = Regex::new(r"^(\s*[\*-]\s*)").unwrap();
+ let number = Regex::new(r"^(\s*)([0-9]+)([).]\s*)").unwrap();
let prio = Regex::new(r"^\s*[0-9]+\s*%\s*").unwrap();
+ // Use index of the longest second group match to create conf template
+ if DEBUG {
+ eprintln!(
+ "Match: '{}', '{}', '{}', '{}'",
+ match number.captures(&list[0]){None => "", Some(x) => x.get(0).unwrap().as_str()},
+ match number.captures(&list[0]){None => "", Some(x) => x.get(1).unwrap().as_str()},
+ match number.captures(&list[0]){None => "", Some(x) => x.get(2).unwrap().as_str()},
+ match number.captures(&list[0]){None => "", Some(x) => x.get(3).unwrap().as_str()},
+ );
+ }
+
+ let lens: Vec<usize> = list.iter().map(
+ |y| match number.captures(y) {
+ None => 0,
+ Some(x) => x.get(2).unwrap().as_str().len(),
+ }).collect();
+
+ let maxi = lens.iter().enumerate().fold(
+ (0, 0),
+ |max, (ind, &val)| if val > max.1 {(ind, val)} else {max});
+
+ let tmpl = match number.captures(&list[maxi.0]) {
+ None => "".to_string(),
+ Some(x) => {
+ if DEBUG {
+ eprintln!("Test matches '{}', '{}', '{}'",
+ x.get(1).unwrap().as_str(),
+ x.get(2).unwrap().as_str(),
+ x.get(3).unwrap().as_str());
+ }
+ format!(
+ "{}{{}}{}",
+ x.get(1).unwrap().as_str(),
+ x.get(3).unwrap().as_str())
+ },
+ };
+
+ if DEBUG {
+ eprint!("Lens index {}, {}: ", maxi.0, maxi.1);
+ for n in lens {eprint!("{}, ", n);}
+ eprintln!("\x08\x08.");
+ eprintln!("Pattern: '{}'", tmpl);
+ }
+
let conf = Conf {
bullet: match bullet.captures(&list[0]) {
None => "".to_string(),
Some(x) => x.get(1).unwrap().as_str().to_string(),
},
+ number: tmpl,
};
if DEBUG {
// Remove patterns and trim
for l in &mut *list {
*l = bullet.replace_all(&l, "").to_string();
+ *l = number.replace_all(&l, "").to_string();
*l = prio.replace_all(&l, "").to_string();
*l = l.trim().to_owned();
}
}
fn output(conf: &Conf, prio: &Vec<f64>, ranked: &Vec<String>) {
- for (p, l) in prio.iter().zip(ranked.iter()) {
- println!(
- "{}{:2.0} %\t{}",
- &conf.bullet,
- p * 100.0,
- l);
+
+ let digs: usize = (prio.len() as f32).log(10.0).ceil() as usize;
+
+ for (i, (p, l)) in prio.iter().zip(ranked).enumerate() {
+
+ let item = if conf.number != "" {
+ conf.number.replace("{}", format!( // Replace template due
+ "{:digs$}", i + 1).as_str()) // to smt::fmt
+ } else {
+ conf.bullet.clone()
+ };
+
+ println!("{}{:2.0} %\t{}", item, 100.0 * p, l)
}
}
struct Conf {
bullet: String,
+ number: String,
}
#[derive(Parser, Debug)]
let (conf, res) = winnow(arg);
assert_eq!(exp, res);
assert_eq!(conf.bullet, " * ".to_owned());
+ assert_eq!(conf.number, "".to_owned());
+}
+
+#[test]
+
+fn numbers() {
+ let arg: Vec<String> =
+ "1. Hej du\n 459. glade\n 2)ta en\n"
+ .split("\n").map(|x| x.to_owned())
+ .collect();
+ let exp: Vec<String> =
+ "Hej du\nglade\nta en"
+ .split("\n").map(|x| x.to_owned()).collect();
+ let (conf, res) = winnow(arg);
+ assert_eq!(exp, res);
+ assert_eq!(conf.bullet, "".to_owned());
+ assert_eq!(conf.number, " {}. ".to_owned());
}
#[test]
#[test]
fn bullout() {
- let conf = Conf {bullet: " * ".to_owned()};
+ let conf = Conf {
+ bullet: " * ".to_owned(),
+ number: "".to_owned()};
let prio = vec![0.6, 0.3, 0.1];
let ranked: Vec<String> =
"Hej du\nglade\nta en"