]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Tweak appearance of leftovers column
[tablify] / src / lib.rs
index bad71d0b2c2116d9134495c08f8316b84ffd4a7b..cbe9ae9a328deb4629b9bef0a57749c1425049c7 100644 (file)
@@ -4,7 +4,9 @@ use std::fmt::Write;
 use std::io::BufRead;
 use std::iter::Iterator;
 
 use std::io::BufRead;
 use std::iter::Iterator;
 
-pub struct Config {}
+pub struct Config {
+    pub column_threshold: usize,
+}
 
 const HEADER: &str = r#"<!DOCTYPE html>
 <html>
 
 const HEADER: &str = r#"<!DOCTYPE html>
 <html>
@@ -21,6 +23,7 @@ const HEADER: &str = r#"<!DOCTYPE html>
     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; }
     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; }
     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
 }
     counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
     counts
 }
-fn column_order(rows: &[Row]) -> Vec<String> {
+fn column_order(config: &Config, rows: &[Row]) -> Vec<String> {
     column_counts(rows)
         .into_iter()
     column_counts(rows)
         .into_iter()
-        .map(|(_, col)| col)
+        .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col))
         .collect()
 }
 
         .collect()
 }
 
@@ -258,7 +261,7 @@ fn render_row(columns: &[String], row: &mut Row) -> HTML {
         .collect::<HTML>();
     let leftovers = render_all_leftovers(row);
     HTML(format!(
         .collect::<HTML>();
     let leftovers = render_all_leftovers(row);
     HTML(format!(
-        "<tr><th id=\"{row_label}\">{row_label}</th>{cells}<td onmouseover=\"highlight('{row_label}')\" onmouseout=\"clear_highlight('{row_label}')\">{leftovers}</td></tr>\n"
+        "<tr><th id=\"{row_label}\">{row_label}</th>{cells}<td class=\"leftover\" onmouseover=\"highlight('{row_label}')\" onmouseout=\"clear_highlight('{row_label}')\">{leftovers}</td></tr>\n"
     ))
 }
 
     ))
 }
 
@@ -286,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<HTML, std::io::Error> {
     let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
 ///     * an indented line with no preceding non-indented line
 pub fn tablify(config: &Config, input: impl std::io::Read) -> Result<HTML, std::io::Error> {
     let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
-    let columns = column_order(&rows);
+    let columns = column_order(config, &rows);
     Ok(HTML(format!(
         "{HEADER}{}{}{FOOTER}",
         render_column_headers(&columns),
     Ok(HTML(format!(
         "{HEADER}{}{}{FOOTER}",
         render_column_headers(&columns),
@@ -621,7 +624,7 @@ mod tests {
                 }
             ),
             HTML::from(
                 }
             ),
             HTML::from(
-                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>
+                r#"<tr><th id="nope">nope</th><td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')">bar</td></tr>
 "#
             )
         );
 "#
             )
         );