X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/f915bc906e6b8001bc748a1a5f3b13fafadcb86b..36bc3a39ef440cc1af77d7d88844cacbe6b4387d:/src/lib.rs?ds=sidebyside diff --git a/src/lib.rs b/src/lib.rs index 1396c9b..cbe9ae9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,9 @@ use std::fmt::Write; use std::io::BufRead; use std::iter::Iterator; -pub struct Config {} +pub struct Config { + pub column_threshold: usize, +} const HEADER: &str = r#" @@ -21,6 +23,7 @@ const HEADER: &str = r#" tr.key > th > div { width: 1em; } tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) } td { border: thin solid gray; } + td.leftover { text-align: left; border: none; padding-left: .4em; } td.yes { border: thin solid gray; background-color: #ddd; } /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */ .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; } @@ -180,10 +183,10 @@ 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() } @@ -227,14 +230,38 @@ fn render_cell(col: &str, row: &mut Row) -> HTML { )) } +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: &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}{cells}\n" + "{row_label}{cells}{leftovers}\n" )) } @@ -262,7 +289,7 @@ fn render_column_headers(columns: &[String]) -> HTML { /// * an indented line with no preceding non-indented line 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), @@ -555,6 +582,37 @@ mod tests { 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!( @@ -566,7 +624,7 @@ mod tests { } ), HTML::from( - r#"nope + r#"nopebar "# ) );