]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
!colsep to separate !col columns
[tablify] / src / lib.rs
index 09e2e7ad11801dd754e20d98ce0eb37a8459bec7..e8a2ea01f7474809877b7b41094ce6061e53f123 100644 (file)
@@ -7,7 +7,7 @@ use std::iter::Iterator;
 #[derive(PartialEq, Eq, Debug)]
 struct Config {
     column_threshold: usize,
-    static_columns: Vec<String>,
+    static_columns: Vec<Option<String>>,
 }
 impl Config {
     fn apply_command(&mut self, line_num: usize, cmd: &str) -> Result<(), std::io::Error> {
@@ -19,7 +19,9 @@ impl Config {
                 )
             })?;
         } else if let Some(col) = cmd.strip_prefix("col ") {
-            self.static_columns.push(col.to_owned());
+            self.static_columns.push(Some(col.to_owned()));
+        } else if cmd == "colsep" {
+            self.static_columns.push(None);
         } else {
             return Err(std::io::Error::new(
                 std::io::ErrorKind::InvalidInput,
@@ -41,6 +43,7 @@ const HEADER: &str = r#"<!DOCTYPE html>
     th, td { white-space: nowrap; }
     th { text-align: left; font-weight: normal; }
     th.spacer_row { height: .3em; }
+    .spacer_col { border: none; width: .2em; }
     table { border-collapse: collapse }
     tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
     tr.key > th > div { width: 1em; }
@@ -239,6 +242,7 @@ fn column_order(config: &Config, rows: &[Rowlike]) -> Vec<String> {
     let static_columns: HashSet<&str> = config
         .static_columns
         .iter()
+        .flatten()
         .map(std::string::String::as_str)
         .collect();
     column_counts(rows)
@@ -320,7 +324,10 @@ fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTM
             let static_cells = config
                 .static_columns
                 .iter()
-                .map(|col| render_cell(col, row))
+                .map(|ocol| match ocol {
+                    Some(col) => render_cell(col, row),
+                    None => HTML::from(r#"<td class="spacer_col"></td>"#),
+                })
                 .collect::<HTML>();
             let dynamic_cells = columns
                 .iter()
@@ -335,20 +342,26 @@ fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTM
 }
 
 fn render_column_headers(config: &Config, columns: &[String]) -> HTML {
+    let static_columns = config.static_columns.iter().map(|oc| oc.as_ref());
+    let dynamic_columns = columns.iter().map(Some);
     HTML(
         String::from(r#"<tr class="key"><th></th>"#)
-            + &config.static_columns.iter().chain(columns.iter()).fold(
-                String::new(),
-                |mut acc, col| {
-                    let col_header = HTML::escape(col.as_ref());
-                    write!(
-                        &mut acc,
-                        r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
-                    )
+            + &static_columns
+                .chain(dynamic_columns)
+                .fold(String::new(), |mut acc, ocol| {
+                    match ocol {
+                        Some(col) => {
+                            let col_header = HTML::escape(col);
+                            write!(
+                                &mut acc,
+                                r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
+                            )
+                        }
+                        None => write!(&mut acc, r#"<th class="col_spacer"></th>"#),
+                    }
                     .unwrap();
                     acc
-                },
-            )
+                })
             + "</tr>\n",
     )
 }
@@ -523,7 +536,7 @@ mod tests {
         );
         assert_eq!(
             read_config(&b"!col foo"[..]).unwrap().static_columns,
-            vec!["foo".to_owned()]
+            vec![Some("foo".to_owned())]
         );
 
         let bad_command = read_config(&b"!no such command"[..]);
@@ -728,7 +741,7 @@ mod tests {
             render_row(
                 &Config {
                     column_threshold: 0,
-                    static_columns: vec!["foo".to_owned(), "bar".to_owned()],
+                    static_columns: vec![Some("foo".to_owned()), Some("bar".to_owned())],
                 },
                 &["baz".to_owned()],
                 &mut Rowlike::Row(Row {
@@ -742,6 +755,26 @@ mod tests {
             ),
             HTML::from(
                 r#"<tr><th id="nope">nope</th><td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">f</td><td class="yes" onmouseover="h2('nope','bar')" onmouseout="ch2('nope','bar')">r</td><td class="yes" onmouseover="h2('nope','baz')" onmouseout="ch2('nope','baz')">z</td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')"></td></tr>
+"#
+            )
+        );
+        assert_eq!(
+            render_row(
+                &Config {
+                    column_threshold: 0,
+                    static_columns: vec![Some("foo".to_owned()), None, Some("bar".to_owned())],
+                },
+                &[],
+                &mut Rowlike::Row(Row {
+                    label: "nope".to_owned(),
+                    entries: HashMap::from([
+                        ("bar".to_owned(), vec![Some("r".to_owned())]),
+                        ("foo".to_owned(), vec![Some("f".to_owned())]),
+                    ]),
+                })
+            ),
+            HTML::from(
+                r#"<tr><th id="nope">nope</th><td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">f</td><td class="spacer_col"></td><td class="yes" onmouseover="h2('nope','bar')" onmouseout="ch2('nope','bar')">r</td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')"></td></tr>
 "#
             )
         );