]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Leftovers column
[tablify] / src / lib.rs
index 1396c9be9b9605d18af6efce8deb974b121e6408..bad71d0b2c2116d9134495c08f8316b84ffd4a7b 100644 (file)
@@ -227,14 +227,38 @@ fn render_cell(col: &str, row: &mut Row) -> HTML {
     ))
 }
 
+fn render_leftover(notcol: &str, instances: &[Option<String>]) -> 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::<Vec<_>>()
+            .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::<HTML>();
+    let leftovers = render_all_leftovers(row);
     HTML(format!(
-        "<tr><th id=\"{row_label}\">{row_label}</th>{cells}</tr>\n"
+        "<tr><th id=\"{row_label}\">{row_label}</th>{cells}<td onmouseover=\"highlight('{row_label}')\" onmouseout=\"clear_highlight('{row_label}')\">{leftovers}</td></tr>\n"
     ))
 }
 
@@ -555,6 +579,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 +621,7 @@ mod tests {
                 }
             ),
             HTML::from(
-                r#"<tr><th id="nope">nope</th><td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td></tr>
+                r#"<tr><th id="nope">nope</th><td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td><td onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')">bar</td></tr>
 "#
             )
         );