X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/fdc441dab9c793956ea5c6e4a7c766eb87b16588..9321d710d6048b57691ff08a62c93c1c8e30a721:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index 731457e..ac52b1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,9 +5,14 @@ use std::io::BufRead; use std::iter::Iterator; fn tally_marks(n: usize, mark: Option<&str>) -> String { - let fives = { 0..n / 5 }.map(|_| '𝍸'); - let ones = { 0..n % 5 }.map(|_| '𝍷'); - fives.chain(ones).collect() + match mark { + None => { + let fives = { 0..n / 5 }.map(|_| '𝍸'); + let ones = { 0..n % 5 }.map(|_| '𝍷'); + fives.chain(ones).collect() + } + Some(m) => { 0..n }.map(|_| m).collect(), + } } #[derive(PartialEq, Eq, Debug)] @@ -45,6 +50,16 @@ impl Config { .substitute_labels .insert(col.to_owned(), label.to_owned()), }; + } else if let Some(directive) = cmd.strip_prefix("mark ") { + match directive.split_once(':') { + None => { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + format!("line {line_num}: Mark missing ':'"), + )) + } + Some((col, label)) => self.mark.insert(col.to_owned(), label.to_owned()), + }; } else { return Err(std::io::Error::new( std::io::ErrorKind::InvalidInput, @@ -192,9 +207,7 @@ impl<'cfg, Input: Iterator>> Reader<'cfg, } } } -impl<'cfg, Input: Iterator>> Iterator - for Reader<'cfg, Input> -{ +impl>> Iterator for Reader<'_, Input> { type Item = Result; fn next(&mut self) -> Option { loop { @@ -408,7 +421,7 @@ fn column_header_labels<'a>( let dynamic_columns = columns.iter().map(Some); static_columns .chain(dynamic_columns) - .filter(|ocol| ocol.map_or(true, |col| !config.hidden_columns.contains(col))) + .filter(|ocol| ocol.is_none_or(|col| !config.hidden_columns.contains(col))) .map(|ocol| { ocol.map(|col| match config.substitute_labels.get(col) { None => col, @@ -444,7 +457,7 @@ fn render_column_headers(config: &Config, columns: &[String]) -> HTML { /// * there's an i/o error while reading `input` /// * the log has invalid syntax: /// * an indented line with no preceding non-indented line -pub fn tablify(input: impl std::io::Read) -> Result { +pub fn tablify(input: R) -> Result { let (rows, config) = read_input(input)?; let columns = column_order(&config, &rows); Ok(HTML(format!( @@ -502,6 +515,11 @@ mod tests { assert_eq!(tally_marks(9, None), "𝍸𝍷𝍷𝍷𝍷"); assert_eq!(tally_marks(10, None), "𝍸𝍸"); assert_eq!(tally_marks(11, None), "𝍸𝍸𝍷"); + assert_eq!(tally_marks(1, Some("x")), "x"); + assert_eq!(tally_marks(4, Some("x")), "xxxx"); + assert_eq!(tally_marks(5, Some("x")), "xxxxx"); + assert_eq!(tally_marks(6, Some("x")), "xxxxxx"); + assert_eq!(tally_marks(2, Some("🍔")), "🍔🍔"); } fn read_rows(input: impl std::io::Read) -> Result, std::io::Error> { @@ -631,6 +649,7 @@ mod tests { .substitute_labels["foo"], "bar" ); + assert_eq!(read_config(&b"!mark foo:*"[..]).unwrap().mark["foo"], "*"); let bad_command = read_config(&b"!no such command"[..]); assert!(bad_command.is_err()); @@ -643,6 +662,10 @@ mod tests { let bad_sub = read_config(&b"!label foo"[..]); assert!(bad_sub.is_err()); assert!(format!("{bad_sub:?}").contains("line 1: Annotation missing ':'")); + + let bad_mark = read_config(&b"!mark foo"[..]); + assert!(bad_mark.is_err()); + assert!(format!("{bad_mark:?}").contains("line 1: Mark missing ':'")); } #[test] @@ -821,6 +844,26 @@ mod tests { r#"𝍷"# ) ); + assert_eq!( + render_cell( + &Config { + column_threshold: 2, + static_columns: vec![], + hidden_columns: HashSet::new(), + substitute_labels: HashMap::new(), + mark: HashMap::from([("foo".to_owned(), "🦄".to_owned())]), + }, + "foo", + &mut Row { + label: "nope".to_owned(), + entries: HashMap::from([("foo".to_owned(), vec![None])]), + } + ), + HTML::from( + r#"🦄"# + ) + ); + let mut r = Row { label: "nope".to_owned(), entries: HashMap::from([ @@ -891,6 +934,22 @@ mod tests { ), HTML::from("") ); + assert_eq!( + render_all_leftovers( + &Config { + column_threshold: 2, + static_columns: vec![], + hidden_columns: HashSet::new(), + substitute_labels: HashMap::new(), + mark: HashMap::from([("foo".to_owned(), "🌈".to_owned())]), + }, + &Row { + label: "nope".to_owned(), + entries: HashMap::from([("foo".to_owned(), vec![None, None])]), + } + ), + HTML::from("foo: 🌈🌈") + ); } #[test]