]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
!colsep to separate !col columns
[tablify] / src / lib.rs
index e20d23d342fe9fffaadc9ef162fde04a70d377f4..e8a2ea01f7474809877b7b41094ce6061e53f123 100644 (file)
@@ -7,20 +7,25 @@ use std::iter::Iterator;
 #[derive(PartialEq, Eq, Debug)]
 struct Config {
     column_threshold: usize,
 #[derive(PartialEq, Eq, Debug)]
 struct Config {
     column_threshold: usize,
-    static_columns: Vec<String>,
+    static_columns: Vec<Option<String>>,
 }
 impl Config {
 }
 impl Config {
-    fn apply_command(&mut self, cmd: &str) -> Result<(), std::io::Error> {
+    fn apply_command(&mut self, line_num: usize, cmd: &str) -> Result<(), std::io::Error> {
         if let Some(threshold) = cmd.strip_prefix("col_threshold ") {
         if let Some(threshold) = cmd.strip_prefix("col_threshold ") {
-            self.column_threshold = threshold
-                .parse()
-                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;
+            self.column_threshold = threshold.parse().map_err(|e| {
+                std::io::Error::new(
+                    std::io::ErrorKind::InvalidInput,
+                    format!("line {line_num}: col_threshold must be numeric: {e}"),
+                )
+            })?;
         } else if let Some(col) = cmd.strip_prefix("col ") {
         } 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,
         } else {
             return Err(std::io::Error::new(
                 std::io::ErrorKind::InvalidInput,
-                format!("Unknown command: {cmd}"),
+                format!("line {line_num}: Unknown command: {cmd}"),
             ));
         }
         Ok(())
             ));
         }
         Ok(())
@@ -38,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; }
     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; }
     table { border-collapse: collapse }
     tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
     tr.key > th > div { width: 1em; }
@@ -163,7 +169,7 @@ impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator
                 Some((_, Err(e))) => return Some(Err(e)),
                 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
                     InputLine::Command(cmd) => {
                 Some((_, Err(e))) => return Some(Err(e)),
                 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
                     InputLine::Command(cmd) => {
-                        if let Err(e) = self.config.apply_command(cmd) {
+                        if let Err(e) = self.config.apply_command(n + 1, cmd) {
                             return Some(Err(e));
                         }
                     }
                             return Some(Err(e));
                         }
                     }
@@ -236,6 +242,7 @@ fn column_order(config: &Config, rows: &[Rowlike]) -> Vec<String> {
     let static_columns: HashSet<&str> = config
         .static_columns
         .iter()
     let static_columns: HashSet<&str> = config
         .static_columns
         .iter()
+        .flatten()
         .map(std::string::String::as_str)
         .collect();
     column_counts(rows)
         .map(std::string::String::as_str)
         .collect();
     column_counts(rows)
@@ -317,7 +324,10 @@ fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTM
             let static_cells = config
                 .static_columns
                 .iter()
             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()
                 .collect::<HTML>();
             let dynamic_cells = columns
                 .iter()
@@ -332,20 +342,26 @@ fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTM
 }
 
 fn render_column_headers(config: &Config, columns: &[String]) -> HTML {
 }
 
 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>"#)
     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
                     .unwrap();
                     acc
-                },
-            )
+                })
             + "</tr>\n",
     )
 }
             + "</tr>\n",
     )
 }
@@ -520,16 +536,16 @@ mod tests {
         );
         assert_eq!(
             read_config(&b"!col foo"[..]).unwrap().static_columns,
         );
         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"[..]);
         assert!(bad_command.is_err());
         );
 
         let bad_command = read_config(&b"!no such command"[..]);
         assert!(bad_command.is_err());
-        assert!(format!("{bad_command:?}").contains("Unknown command"));
+        assert!(format!("{bad_command:?}").contains("line 1: Unknown command"));
 
         let bad_num = read_config(&b"!col_threshold foo"[..]);
         assert!(bad_num.is_err());
 
         let bad_num = read_config(&b"!col_threshold foo"[..]);
         assert!(bad_num.is_err());
-        assert!(format!("{bad_num:?}").contains("Parse"));
+        assert!(format!("{bad_num:?}").contains("line 1: col_threshold must be numeric"));
     }
 
     #[test]
     }
 
     #[test]
@@ -725,7 +741,7 @@ mod tests {
             render_row(
                 &Config {
                     column_threshold: 0,
             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 {
                 },
                 &["baz".to_owned()],
                 &mut Rowlike::Row(Row {
@@ -739,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>
             ),
             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>
 "#
             )
         );
 "#
             )
         );