From: Scott Worley Date: Thu, 26 Sep 2024 01:36:50 +0000 (-0700) Subject: Don't allocate columns to rare events X-Git-Tag: v0.3.0~8 X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/31af9aac0a32a20cf4111a64af65871b188bcd72 Don't allocate columns to rare events --- diff --git a/Changelog b/Changelog index 33d7162..6f09172 100644 --- a/Changelog +++ b/Changelog @@ -2,6 +2,7 @@ - Center text in each cell - Escape HTML characters properly - Fix an unnecessary O(n^2)ism +- Rare events appear as end-of-line notes rather than mostly-empty columns ## [0.2.1] - 2024-08-20 - A little more space up top diff --git a/src/lib.rs b/src/lib.rs index bad71d0..a7c2819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,9 @@ use std::fmt::Write; use std::io::BufRead; use std::iter::Iterator; -pub struct Config {} +pub struct Config { + pub column_threshold: usize, +} const HEADER: &str = r#" @@ -180,10 +182,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 } -fn column_order(rows: &[Row]) -> Vec { +fn column_order(config: &Config, rows: &[Row]) -> Vec { column_counts(rows) .into_iter() - .map(|(_, col)| col) + .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col)) .collect() } @@ -286,7 +288,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 { let rows = read_rows(input).collect::, _>>()?; - let columns = column_order(&rows); + let columns = column_order(config, &rows); Ok(HTML(format!( "{HEADER}{}{}{FOOTER}", render_column_headers(&columns), diff --git a/src/main.rs b/src/main.rs index b4f32fe..6102247 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,12 @@ fn main() { print!( "{}", - tablify::tablify(&tablify::Config {}, std::io::stdin()).unwrap() + tablify::tablify( + &tablify::Config { + column_threshold: 2 + }, + std::io::stdin() + ) + .unwrap() ); }