From a05772014cb65edac0cc664296802036bdc48ca4 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 3 Oct 2024 20:48:45 -0700 Subject: [PATCH] !colsep to separate !col columns --- Changelog | 1 + src/lib.rs | 63 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/Changelog b/Changelog index fbee5bd..37cde58 100644 --- a/Changelog +++ b/Changelog @@ -1,6 +1,7 @@ ## [Unreleased] - Read column threshold from `!col_threshold ` in input - Allow forcing column layout with `!col ` in input +- `!colsep` to separate `!col` columns ## [0.3.0] - 2024-10-02 - Center text in each cell diff --git a/src/lib.rs b/src/lib.rs index 09e2e7a..e8a2ea0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ use std::iter::Iterator; #[derive(PartialEq, Eq, Debug)] struct Config { column_threshold: usize, - static_columns: Vec, + static_columns: Vec>, } 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#" 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 { 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#""#), + }) .collect::(); 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#""#) - + &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#"
{col_header}
"# - ) + + &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#"
{col_header}
"# + ) + } + None => write!(&mut acc, r#""#), + } .unwrap(); acc - }, - ) + }) + "\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#"nopefrz +"# + ) + ); + 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#"nopefr "# ) ); -- 2.44.1