From: Gustav Eek Date: Mon, 27 Mar 2023 22:18:09 +0000 (+0200) Subject: Src. Add piped table parsing and printing X-Git-Tag: v.0.6~1 X-Git-Url: https://git.g-eek.se/?a=commitdiff_plain;h=cbed4896a3cf7de71a9b90a1b42a5e2d6b01b9df;p=ranknauto.git Src. Add piped table parsing and printing Identify piped table start as a *bullet* pattern. The *prio* pattern also is update to capture the possible (in case of old priorities) trailing pipe sign. The pipes are printed as new *pre*/*pst* (post) patterns. A test is also added. --- diff --git a/src/main.rs b/src/main.rs index 35eaa64..1f90fc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,11 +19,11 @@ fn winnow(mut list: Vec) -> (Conf, Vec ) { list.retain(|x| x != ""); // only keep nonempty - // Search patterns + // Search patterns. Find pipe tables as a bullet item - let bullet = Regex::new(r"^(\s*[\*-]\s*)").unwrap(); + 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(); + let prio = Regex::new(r"^\s*[0-9]+\s*%[|\s]*").unwrap(); if DEBUG { eprintln!( // debug patterns "Matches first item: '{}', '{}', '{}'", @@ -111,14 +111,15 @@ fn output(conf: &Conf, prio: &Vec, ranked: &Vec) { for (i, (p, l)) in prio.iter().zip(ranked).enumerate() { - let item = if conf.number != "" { + let pre = if conf.number != "" { conf.number.replace("{}", format!( // Replace template due "{:digs$}", i + 1).as_str()) // to smt::fmt } else { conf.bullet.clone() }; + let pst = if conf.bullet.contains("|") {" | "} else {"\t"}; - println!("{}{:2.0} %\t{}", item, 100.0 * p, l) + println!("{}{:2.0} %{}{}", pre, 100.0 * p, pst, l) } } @@ -340,6 +341,23 @@ fn prioinput() { #[test] +fn pipe() { + + let arg: Vec = + "\n| Hej du\n | glade\n| ta en\n" + .split("\n").map(|x| x.to_owned()) + .collect(); + let exp: Vec = + "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] + fn bullout() { let conf = Conf {