From: Scott Worley Date: Sat, 5 Oct 2024 01:33:48 +0000 (-0700) Subject: Tally marks X-Git-Tag: v0.5.0~4 X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/f8c2cab25a0438491b3bfd7cbdf1e5d78f8b40a7?hp=d669f60d11bad036e4ac6c3da39f067d7bd2153a Tally marks --- diff --git a/Changelog b/Changelog index cab5053..cb3b4c5 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,7 @@ ## [Unreleased] - `!label col:new label` to substitute a different label for a column +- Use Unicode tally marks to coalesce multiple no-colon entries + (If you need a font for these, I recommend "BabelStone Han") ## [0.4.0] - 2024-10-03 - Read column threshold from `!col_threshold ` in input diff --git a/src/lib.rs b/src/lib.rs index f5d12f8..84cb336 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,12 @@ use std::fmt::Write; use std::io::BufRead; use std::iter::Iterator; +fn tally_marks(n: usize) -> String { + let fives = { 0..n / 5 }.map(|_| '𝍸'); + let ones = { 0..n % 5 }.map(|_| '𝍷'); + fives.chain(ones).collect() +} + #[derive(PartialEq, Eq, Debug)] struct Config { column_threshold: usize, @@ -279,29 +285,30 @@ fn column_order(config: &Config, rows: &[Rowlike]) -> Vec { .collect() } -fn render_one_instance(instance: &Option) -> HTML { - match instance { - None => HTML::from("✓"), - Some(instance) => HTML::escape(instance.as_ref()), - } -} - fn render_instances(instances: &[Option]) -> HTML { - let all_empty = instances.iter().all(Option::is_none); - if all_empty && instances.len() == 1 { - HTML::from("") - } else if all_empty { - HTML(format!("{}", instances.len())) - } else { - HTML( - instances - .iter() - .map(render_one_instance) - .map(|html| html.0) // Waiting for slice_concat_trait to stabilize - .collect::>() - .join(" "), - ) + let mut tally = 0; + let mut out = vec![]; + for ins in instances { + match ins { + None => tally += 1, + Some(content) => { + if tally > 0 { + out.push(HTML(tally_marks(tally))); + tally = 0; + } + out.push(HTML::escape(content)); + } + } + } + if tally > 0 { + out.push(HTML(tally_marks(tally))); } + HTML( + out.into_iter() + .map(|html| html.0) // Waiting for slice_concat_trait to stabilize + .collect::>() + .join(" "), + ) } fn render_cell(col: &str, row: &mut Row) -> HTML { @@ -321,10 +328,10 @@ fn render_cell(col: &str, row: &mut Row) -> HTML { fn render_leftover(notcol: &str, instances: &[Option]) -> HTML { let label = HTML::escape(notcol); - let rest = render_instances(instances); - if rest == HTML::from("") { + if instances.len() == 1 && instances[0].is_none() { HTML(format!("{label}")) } else { + let rest = render_instances(instances); HTML(format!("{label}: {rest}")) } } @@ -462,6 +469,21 @@ mod tests { ); } + #[test] + fn test_tally_marks() { + assert_eq!(tally_marks(1), "𝍷"); + assert_eq!(tally_marks(2), "𝍷𝍷"); + assert_eq!(tally_marks(3), "𝍷𝍷𝍷"); + assert_eq!(tally_marks(4), "𝍷𝍷𝍷𝍷"); + assert_eq!(tally_marks(5), "𝍸"); + assert_eq!(tally_marks(6), "𝍸𝍷"); + assert_eq!(tally_marks(7), "𝍸𝍷𝍷"); + assert_eq!(tally_marks(8), "𝍸𝍷𝍷𝍷"); + assert_eq!(tally_marks(9), "𝍸𝍷𝍷𝍷𝍷"); + assert_eq!(tally_marks(10), "𝍸𝍸"); + assert_eq!(tally_marks(11), "𝍸𝍸𝍷"); + } + fn read_rows(input: impl std::io::Read) -> Result, std::io::Error> { read_input(input).map(|(rows, _)| rows) } @@ -692,7 +714,7 @@ mod tests { } ), HTML::from( - r#""# + r#"𝍷"# ) ); assert_eq!( @@ -704,7 +726,7 @@ mod tests { } ), HTML::from( - r#"2"# + r#"𝍷𝍷"# ) ); assert_eq!( @@ -731,7 +753,7 @@ mod tests { } ), HTML::from( - r#"5 ✓"# + r#"5 𝍷"# ) ); assert_eq!( @@ -755,7 +777,7 @@ mod tests { } ), HTML::from( - r#""# + r#"𝍷"# ) ); let mut r = Row { @@ -810,7 +832,7 @@ mod tests { ]), } ), - HTML::from("bar: 2, foo") + HTML::from("bar: 𝍷𝍷, foo") ); assert_eq!( render_all_leftovers(