X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/88a08162523cf4a0a7c1286b1438fdf16ca6c802..31af9aac0a32a20cf4111a64af65871b188bcd72:/src/lib.rs?ds=sidebyside diff --git a/src/lib.rs b/src/lib.rs index b406f95..a7c2819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,11 +4,15 @@ use std::fmt::Write; use std::io::BufRead; use std::iter::Iterator; -const HEADER: &str = " +pub struct Config { + pub column_threshold: usize, +} + +const HEADER: &str = r#" - - + + @@ -33,7 +37,7 @@ const HEADER: &str = " -"; +"#; const FOOTER: &str = "
@@ -178,66 +182,96 @@ fn column_counts(rows: &[Row]) -> Vec<(usize, String)> { counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol))); counts } -fn column_order(rows: &[Row]) -> Vec { +fn column_order(config: &Config, rows: &[Row]) -> Vec { column_counts(rows) .into_iter() - .map(|(_, col)| col) + .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col)) .collect() } -fn render_instance(instance: &Option) -> HTML { +fn render_one_instance(instance: &Option) -> HTML { match instance { None => HTML::from("✓"), Some(instance) => HTML::escape(instance.as_ref()), } } -fn render_cell(col: &str, row: &Row) -> HTML { - let row_label = HTML::escape(row.label.as_ref()); - let col_label = HTML::escape(col); - let instances: Option<&Vec>> = row.entries.get(col); - let class = HTML::from(if instances.is_none() { "" } else { "yes" }); - let all_empty = instances - .iter() - .flat_map(|is| is.iter()) - .all(Option::is_none); - let contents = if instances.is_none() || (all_empty && instances.unwrap().len() == 1) { +fn render_instances(instances: &[Option]) -> HTML { + let all_empty = instances.iter().all(Option::is_none); + if all_empty && instances.len() == 1 { HTML::from("") } else if all_empty { - HTML(format!("{}", instances.unwrap().len())) + HTML(format!("{}", instances.len())) } else { HTML( instances - .unwrap() .iter() - .map(render_instance) + .map(render_one_instance) .map(|html| html.0) // Waiting for slice_concat_trait to stabilize .collect::>() .join(" "), ) + } +} + +fn render_cell(col: &str, row: &mut Row) -> HTML { + let row_label = HTML::escape(row.label.as_ref()); + let col_label = HTML::escape(col); + let instances: Option<&Vec>> = row.entries.get(col); + let class = HTML::from(if instances.is_none() { "" } else { "yes" }); + let contents = match instances { + None => HTML::from(""), + Some(is) => render_instances(is), }; - HTML(format!("{contents}")) + row.entries.remove(col); + HTML(format!( + r#"{contents}"# + )) +} + +fn render_leftover(notcol: &str, instances: &[Option]) -> HTML { + let label = HTML::escape(notcol); + let rest = render_instances(instances); + if rest == HTML::from("") { + HTML(format!("{label}")) + } else { + HTML(format!("{label}: {rest}")) + } +} + +fn render_all_leftovers(row: &Row) -> HTML { + let mut order: Vec<_> = row.entries.keys().collect(); + order.sort_unstable(); + HTML( + order + .into_iter() + .map(|notcol| render_leftover(notcol, row.entries.get(notcol).expect("Key vanished?!"))) + .map(|html| html.0) // Waiting for slice_concat_trait to stabilize + .collect::>() + .join(", "), + ) } -fn render_row(columns: &[String], row: &Row) -> HTML { +fn render_row(columns: &[String], row: &mut Row) -> HTML { let row_label = HTML::escape(row.label.as_ref()); + let cells = columns + .iter() + .map(|col| render_cell(col, row)) + .collect::(); + let leftovers = render_all_leftovers(row); HTML(format!( - "{row_label}{}\n", - &columns - .iter() - .map(|col| render_cell(col, row)) - .collect::() + "{row_label}{cells}{leftovers}\n" )) } fn render_column_headers(columns: &[String]) -> HTML { HTML( - String::from("") + String::from(r#""#) + &columns.iter().fold(String::new(), |mut acc, col| { let col_header = HTML::escape(col.as_ref()); write!( &mut acc, - "
{col_header}
" + r#"
{col_header}
"# ) .unwrap(); acc @@ -252,14 +286,14 @@ fn render_column_headers(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(config: &Config, input: impl std::io::Read) -> Result { let rows = read_rows(input).collect::, _>>()?; - let columns = column_order(&rows); + let columns = column_order(config, &rows); Ok(HTML(format!( "{HEADER}{}{}{FOOTER}", render_column_headers(&columns), rows.into_iter() - .map(|r| render_row(&columns, &r)) + .map(|mut r| render_row(&columns, &mut r)) .collect::() ))) } @@ -435,82 +469,163 @@ mod tests { assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::new(), } ), - HTML::from("") + HTML::from( + r#""# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("bar".to_owned(), vec![None])]), } ), - HTML::from("") + HTML::from( + r#""# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), } ), - HTML::from("") + HTML::from( + r#""# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None, None])]), } ), - HTML::from("2") + HTML::from( + r#"2"# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), - entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())])]), + entries: HashMap::from([( + "foo".to_owned(), + vec![Some("5".to_owned()), Some("10".to_owned())] + )]), } ), - HTML::from("5 10") + HTML::from( + r#"5 10"# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]), } ), - HTML::from("5 ✓") + HTML::from( + r#"5 ✓"# + ) ); assert_eq!( render_cell( "heart", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]), } ), - HTML::from("<3") + HTML::from( + r#"<3"# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "bob's".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), } ), - HTML::from("") + HTML::from( + r#""# + ) + ); + let mut r = Row { + label: "nope".to_owned(), + entries: HashMap::from([ + ("foo".to_owned(), vec![None]), + ("baz".to_owned(), vec![None]), + ]), + }; + assert_eq!(r.entries.len(), 2); + render_cell("foo", &mut r); + assert_eq!(r.entries.len(), 1); + render_cell("bar", &mut r); + assert_eq!(r.entries.len(), 1); + render_cell("baz", &mut r); + assert_eq!(r.entries.len(), 0); + } + + #[test] + fn test_render_leftovers() { + assert_eq!( + render_all_leftovers(&Row { + label: "nope".to_owned(), + entries: HashMap::from([("foo".to_owned(), vec![None])]), + }), + HTML::from("foo") + ); + assert_eq!( + render_all_leftovers(&Row { + label: "nope".to_owned(), + entries: HashMap::from([ + ("foo".to_owned(), vec![None]), + ("bar".to_owned(), vec![None]) + ]), + }), + HTML::from("bar, foo") + ); + assert_eq!( + render_all_leftovers(&Row { + label: "nope".to_owned(), + entries: HashMap::from([ + ("foo".to_owned(), vec![None]), + ("bar".to_owned(), vec![None, None]) + ]), + }), + HTML::from("bar: 2, foo") + ); + } + + #[test] + fn test_render_row() { + assert_eq!( + render_row( + &["foo".to_owned()], + &mut Row { + label: "nope".to_owned(), + entries: HashMap::from([("bar".to_owned(), vec![None])]), + } + ), + HTML::from( + r#"nopebar +"# + ) ); } }