1 use std::borrow::ToOwned;
2 use std::collections::{HashMap, HashSet};
5 use std::iter::Iterator;
7 #[derive(PartialEq, Eq, Debug)]
9 column_threshold: usize,
10 static_columns: Vec<Option<String>>,
11 hidden_columns: HashSet<String>,
14 fn apply_command(&mut self, line_num: usize, cmd: &str) -> Result<(), std::io::Error> {
15 if let Some(threshold) = cmd.strip_prefix("col_threshold ") {
16 self.column_threshold = threshold.parse().map_err(|e| {
18 std::io::ErrorKind::InvalidInput,
19 format!("line {line_num}: col_threshold must be numeric: {e}"),
22 } else if let Some(col) = cmd.strip_prefix("hide ") {
23 self.hidden_columns.insert(col.to_owned());
24 } else if let Some(col) = cmd.strip_prefix("col ") {
25 self.static_columns.push(Some(col.to_owned()));
26 } else if cmd == "colsep" {
27 self.static_columns.push(None);
29 return Err(std::io::Error::new(
30 std::io::ErrorKind::InvalidInput,
31 format!("line {line_num}: Unknown command: {cmd}"),
37 impl Default for Config {
38 fn default() -> Self {
41 static_columns: vec![],
42 hidden_columns: HashSet::new(),
47 const HEADER: &str = r#"<!DOCTYPE html>
50 <meta charset="utf-8">
51 <meta name="viewport" content="width=device-width, initial-scale=1">
53 td { text-align: center; }
54 /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
55 th, td { white-space: nowrap; }
56 th { text-align: left; font-weight: normal; }
57 th.spacer_row { height: .3em; }
58 .spacer_col { border: none; width: .2em; }
59 table { border-collapse: collapse }
60 tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
61 tr.key > th > div { width: 1em; }
62 tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
63 td { border: thin solid gray; }
64 td.leftover { text-align: left; border: none; padding-left: .4em; }
65 td.yes { border: thin solid gray; background-color: #ddd; }
66 /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */
67 .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
70 function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( "highlight"); } }
71 function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove("highlight"); } }
72 function h2(a, b) { highlight(a); highlight(b); }
73 function ch2(a, b) { clear_highlight(a); clear_highlight(b); }
80 const FOOTER: &str = " </tbody>
85 #[derive(PartialEq, Eq, Debug)]
86 pub struct HTML(String);
88 fn escape(value: &str) -> HTML {
89 let mut escaped: String = String::new();
90 for c in value.chars() {
92 '>' => escaped.push_str(">"),
93 '<' => escaped.push_str("<"),
94 '\'' => escaped.push_str("'"),
95 '"' => escaped.push_str("""),
96 '&' => escaped.push_str("&"),
97 ok_c => escaped.push(ok_c),
103 impl From<&str> for HTML {
104 fn from(value: &str) -> HTML {
105 HTML(String::from(value))
108 impl FromIterator<HTML> for HTML {
109 fn from_iter<T>(iter: T) -> HTML
111 T: IntoIterator<Item = HTML>,
113 HTML(iter.into_iter().map(|html| html.0).collect::<String>())
116 impl std::fmt::Display for HTML {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118 write!(f, "{}", self.0)
122 #[derive(Debug, PartialEq, Eq)]
126 Entry(&'a str, Option<&'a str>),
129 impl<'a> From<&'a str> for InputLine<'a> {
130 fn from(value: &'a str) -> InputLine<'a> {
131 let trimmed = value.trim_end();
132 if trimmed.is_empty() {
134 } else if let Some(cmd) = trimmed.strip_prefix('!') {
135 InputLine::Command(cmd)
136 } else if !trimmed.starts_with(' ') {
137 InputLine::RowHeader(value.trim())
139 match value.split_once(':') {
140 None => InputLine::Entry(value.trim(), None),
141 Some((col, instance)) => InputLine::Entry(col.trim(), Some(instance.trim())),
147 #[derive(Debug, PartialEq, Eq)]
150 entries: HashMap<String, Vec<Option<String>>>,
153 #[derive(Debug, PartialEq, Eq)]
159 struct Reader<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> {
160 input: std::iter::Enumerate<Input>,
162 config: &'cfg mut Config,
164 impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Reader<'cfg, Input> {
165 fn new(config: &'cfg mut Config, input: Input) -> Self {
167 input: input.enumerate(),
173 impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator
174 for Reader<'cfg, Input>
176 type Item = Result<Rowlike, std::io::Error>;
177 fn next(&mut self) -> Option<Self::Item> {
179 match self.input.next() {
180 None => return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose(),
181 Some((_, Err(e))) => return Some(Err(e)),
182 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
183 InputLine::Command(cmd) => {
184 if let Err(e) = self.config.apply_command(n + 1, cmd) {
188 InputLine::Blank if self.row.is_some() => {
189 return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose()
191 InputLine::Blank => return Some(Ok(Rowlike::Spacer)),
192 InputLine::Entry(col, instance) => match &mut self.row {
194 return Some(Err(std::io::Error::other(format!(
195 "line {}: Entry with no header",
199 Some(ref mut row) => {
201 .entry(col.to_owned())
202 .and_modify(|is| is.push(instance.map(ToOwned::to_owned)))
203 .or_insert_with(|| vec![instance.map(ToOwned::to_owned)]);
206 InputLine::RowHeader(row) => {
207 let prev = std::mem::take(&mut self.row);
208 self.row = Some(Row {
209 label: row.to_owned(),
210 entries: HashMap::new(),
213 return Ok(prev.map(Rowlike::Row)).transpose();
222 fn read_input(input: impl std::io::Read) -> Result<(Vec<Rowlike>, Config), std::io::Error> {
223 let mut config = Config::default();
224 let reader = Reader::new(&mut config, std::io::BufReader::new(input).lines());
226 .collect::<Result<Vec<_>, _>>()
227 .map(|rows| (rows, config))
230 fn column_counts(rows: &[Rowlike]) -> Vec<(usize, String)> {
231 let empty = HashMap::new();
232 let mut counts: Vec<_> = rows
234 .flat_map(|rl| match rl {
235 Rowlike::Row(r) => r.entries.keys(),
236 Rowlike::Spacer => empty.keys(),
238 .fold(HashMap::new(), |mut cs, col| {
239 cs.entry(col.to_owned())
240 .and_modify(|n| *n += 1)
245 .map(|(col, n)| (n, col))
247 counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
250 fn column_order(config: &Config, rows: &[Rowlike]) -> Vec<String> {
251 let static_columns: HashSet<&str> = config
255 .map(std::string::String::as_str)
259 .filter_map(|(n, col)| {
260 (n >= config.column_threshold
261 && !static_columns.contains(col.as_str())
262 && !config.hidden_columns.contains(&col))
268 fn render_one_instance(instance: &Option<String>) -> HTML {
270 None => HTML::from("✓"),
271 Some(instance) => HTML::escape(instance.as_ref()),
275 fn render_instances(instances: &[Option<String>]) -> HTML {
276 let all_empty = instances.iter().all(Option::is_none);
277 if all_empty && instances.len() == 1 {
279 } else if all_empty {
280 HTML(format!("{}", instances.len()))
285 .map(render_one_instance)
286 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
293 fn render_cell(col: &str, row: &mut Row) -> HTML {
294 let row_label = HTML::escape(row.label.as_ref());
295 let col_label = HTML::escape(col);
296 let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
297 let class = HTML::from(if instances.is_none() { "" } else { "yes" });
298 let contents = match instances {
299 None => HTML::from(""),
300 Some(is) => render_instances(is),
302 row.entries.remove(col);
304 r#"<td class="{class}" onmouseover="h2('{row_label}','{col_label}')" onmouseout="ch2('{row_label}','{col_label}')">{contents}</td>"#
308 fn render_leftover(notcol: &str, instances: &[Option<String>]) -> HTML {
309 let label = HTML::escape(notcol);
310 let rest = render_instances(instances);
311 if rest == HTML::from("") {
312 HTML(format!("{label}"))
314 HTML(format!("{label}: {rest}"))
318 fn render_all_leftovers(config: &Config, row: &Row) -> HTML {
319 let mut order: Vec<_> = row
322 .filter(|&col| !config.hidden_columns.contains(col))
324 order.sort_unstable();
328 .map(|notcol| render_leftover(notcol, row.entries.get(notcol).expect("Key vanished?!")))
329 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
335 fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTML {
337 Rowlike::Spacer => HTML::from("<tr><th class=\"spacer_row\"></th></tr>\n"),
338 Rowlike::Row(row) => {
339 let row_label = HTML::escape(row.label.as_ref());
340 let static_cells = config
343 .map(|ocol| match ocol {
344 Some(col) if config.hidden_columns.contains(col) => HTML::from(""),
345 Some(col) => render_cell(col, row),
346 None => HTML::from(r#"<td class="spacer_col"></td>"#),
349 let dynamic_cells = columns
351 .filter(|&col| !config.hidden_columns.contains(col))
352 .map(|col| render_cell(col, row))
354 let leftovers = render_all_leftovers(config, row);
356 "<tr><th id=\"{row_label}\">{row_label}</th>{static_cells}{dynamic_cells}<td class=\"leftover\" onmouseover=\"highlight('{row_label}')\" onmouseout=\"clear_highlight('{row_label}')\">{leftovers}</td></tr>\n"
362 fn render_column_headers(config: &Config, columns: &[String]) -> HTML {
363 let static_columns = config.static_columns.iter().map(|oc| oc.as_ref());
364 let dynamic_columns = columns.iter().map(Some);
366 String::from(r#"<tr class="key"><th></th>"#)
368 .chain(dynamic_columns)
370 ocol.map(|col| !config.hidden_columns.contains(col))
373 .fold(String::new(), |mut acc, ocol| {
376 let col_header = HTML::escape(col);
379 r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
382 None => write!(&mut acc, r#"<th class="col_spacer"></th>"#),
393 /// Will return `Err` if
394 /// * there's an i/o error while reading `input`
395 /// * the log has invalid syntax:
396 /// * an indented line with no preceding non-indented line
397 pub fn tablify(input: impl std::io::Read) -> Result<HTML, std::io::Error> {
398 let (rows, config) = read_input(input)?;
399 let columns = column_order(&config, &rows);
401 "{HEADER}{}{}{FOOTER}",
402 render_column_headers(&config, &columns),
404 .map(|mut r| render_row(&config, &columns, &mut r))
414 fn test_parse_line() {
415 assert_eq!(InputLine::from(""), InputLine::Blank);
416 assert_eq!(InputLine::from(" "), InputLine::Blank);
417 assert_eq!(InputLine::from("foo"), InputLine::RowHeader("foo"));
418 assert_eq!(InputLine::from("foo "), InputLine::RowHeader("foo"));
419 assert_eq!(InputLine::from(" foo"), InputLine::Entry("foo", None));
421 InputLine::from(" foo:bar"),
422 InputLine::Entry("foo", Some("bar"))
425 InputLine::from(" foo: bar"),
426 InputLine::Entry("foo", Some("bar"))
429 InputLine::from(" foo: bar "),
430 InputLine::Entry("foo", Some("bar"))
433 InputLine::from(" foo: bar "),
434 InputLine::Entry("foo", Some("bar"))
437 InputLine::from(" foo : bar "),
438 InputLine::Entry("foo", Some("bar"))
442 fn read_rows(input: impl std::io::Read) -> Result<Vec<Rowlike>, std::io::Error> {
443 read_input(input).map(|(rows, _)| rows)
445 fn read_config(input: impl std::io::Read) -> Result<Config, std::io::Error> {
446 read_input(input).map(|(_, config)| config)
449 fn test_read_rows() {
451 read_rows(&b"foo"[..]).unwrap(),
452 vec![Rowlike::Row(Row {
453 label: "foo".to_owned(),
454 entries: HashMap::new(),
458 read_rows(&b"bar"[..]).unwrap(),
459 vec![Rowlike::Row(Row {
460 label: "bar".to_owned(),
461 entries: HashMap::new(),
465 read_rows(&b"foo\nbar\n"[..]).unwrap(),
468 label: "foo".to_owned(),
469 entries: HashMap::new(),
472 label: "bar".to_owned(),
473 entries: HashMap::new(),
478 read_rows(&b"foo\n bar\n"[..]).unwrap(),
479 vec![Rowlike::Row(Row {
480 label: "foo".to_owned(),
481 entries: HashMap::from([("bar".to_owned(), vec![None])]),
485 read_rows(&b"foo\n bar\n baz\n"[..]).unwrap(),
486 vec![Rowlike::Row(Row {
487 label: "foo".to_owned(),
488 entries: HashMap::from([
489 ("bar".to_owned(), vec![None]),
490 ("baz".to_owned(), vec![None])
495 read_rows(&b"foo\n\nbar\n"[..]).unwrap(),
498 label: "foo".to_owned(),
499 entries: HashMap::new(),
502 label: "bar".to_owned(),
503 entries: HashMap::new(),
508 read_rows(&b"foo\n\n\nbar\n"[..]).unwrap(),
511 label: "foo".to_owned(),
512 entries: HashMap::new(),
516 label: "bar".to_owned(),
517 entries: HashMap::new(),
522 read_rows(&b"foo\n \nbar\n"[..]).unwrap(),
525 label: "foo".to_owned(),
526 entries: HashMap::new(),
529 label: "bar".to_owned(),
530 entries: HashMap::new(),
535 read_rows(&b"foo \n bar \n"[..]).unwrap(),
536 vec![Rowlike::Row(Row {
537 label: "foo".to_owned(),
538 entries: HashMap::from([("bar".to_owned(), vec![None])]),
542 let bad = read_rows(&b" foo"[..]);
543 assert!(bad.is_err());
544 assert!(format!("{bad:?}").contains("line 1: Entry with no header"));
546 let bad2 = read_rows(&b"foo\n\n bar"[..]);
547 assert!(bad2.is_err());
548 assert!(format!("{bad2:?}").contains("line 3: Entry with no header"));
552 fn test_read_config() {
554 read_config(&b"!col_threshold 10"[..])
560 read_config(&b"!col foo"[..]).unwrap().static_columns,
561 vec![Some("foo".to_owned())]
564 let bad_command = read_config(&b"!no such command"[..]);
565 assert!(bad_command.is_err());
566 assert!(format!("{bad_command:?}").contains("line 1: Unknown command"));
568 let bad_num = read_config(&b"!col_threshold foo"[..]);
569 assert!(bad_num.is_err());
570 assert!(format!("{bad_num:?}").contains("line 1: col_threshold must be numeric"));
574 fn test_column_counts() {
576 column_counts(&read_rows(&b"foo\n bar\n baz\n"[..]).unwrap()),
577 vec![(1, String::from("bar")), (1, String::from("baz"))]
580 column_counts(&read_rows(&b"foo\n bar\n baz\nquux\n baz"[..]).unwrap()),
581 vec![(2, String::from("baz")), (1, String::from("bar"))]
584 column_counts(&read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..]).unwrap()),
585 vec![(2, String::from("baz")), (1, String::from("bar"))]
589 &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..]).unwrap()
591 vec![(2, String::from("baz")), (1, String::from("bar"))]
596 fn test_render_cell() {
601 label: "nope".to_owned(),
602 entries: HashMap::new(),
606 r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
613 label: "nope".to_owned(),
614 entries: HashMap::from([("bar".to_owned(), vec![None])]),
618 r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
625 label: "nope".to_owned(),
626 entries: HashMap::from([("foo".to_owned(), vec![None])]),
630 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
637 label: "nope".to_owned(),
638 entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
642 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">2</td>"#
649 label: "nope".to_owned(),
650 entries: HashMap::from([(
652 vec![Some("5".to_owned()), Some("10".to_owned())]
657 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 10</td>"#
664 label: "nope".to_owned(),
665 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
669 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 ✓</td>"#
676 label: "nope".to_owned(),
677 entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
681 r#"<td class="yes" onmouseover="h2('nope','heart')" onmouseout="ch2('nope','heart')"><3</td>"#
688 label: "bob's".to_owned(),
689 entries: HashMap::from([("foo".to_owned(), vec![None])]),
693 r#"<td class="yes" onmouseover="h2('bob's','foo')" onmouseout="ch2('bob's','foo')"></td>"#
697 label: "nope".to_owned(),
698 entries: HashMap::from([
699 ("foo".to_owned(), vec![None]),
700 ("baz".to_owned(), vec![None]),
703 assert_eq!(r.entries.len(), 2);
704 render_cell("foo", &mut r);
705 assert_eq!(r.entries.len(), 1);
706 render_cell("bar", &mut r);
707 assert_eq!(r.entries.len(), 1);
708 render_cell("baz", &mut r);
709 assert_eq!(r.entries.len(), 0);
713 fn test_render_leftovers() {
715 render_all_leftovers(
718 label: "nope".to_owned(),
719 entries: HashMap::from([("foo".to_owned(), vec![None])]),
725 render_all_leftovers(
728 label: "nope".to_owned(),
729 entries: HashMap::from([
730 ("foo".to_owned(), vec![None]),
731 ("bar".to_owned(), vec![None])
735 HTML::from("bar, foo")
738 render_all_leftovers(
741 label: "nope".to_owned(),
742 entries: HashMap::from([
743 ("foo".to_owned(), vec![None]),
744 ("bar".to_owned(), vec![None, None])
748 HTML::from("bar: 2, foo")
751 render_all_leftovers(
754 static_columns: vec![],
755 hidden_columns: HashSet::from(["private".to_owned()]),
758 label: "nope".to_owned(),
759 entries: HashMap::from([("private".to_owned(), vec![None]),]),
767 fn test_render_row() {
772 &mut Rowlike::Row(Row {
773 label: "nope".to_owned(),
774 entries: HashMap::from([("bar".to_owned(), vec![None])]),
778 r#"<tr><th id="nope">nope</th><td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')">bar</td></tr>
786 static_columns: vec![Some("foo".to_owned()), Some("bar".to_owned())],
787 hidden_columns: HashSet::new(),
790 &mut Rowlike::Row(Row {
791 label: "nope".to_owned(),
792 entries: HashMap::from([
793 ("bar".to_owned(), vec![Some("r".to_owned())]),
794 ("baz".to_owned(), vec![Some("z".to_owned())]),
795 ("foo".to_owned(), vec![Some("f".to_owned())]),
800 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>
808 static_columns: vec![Some("foo".to_owned()), None, Some("bar".to_owned())],
809 hidden_columns: HashSet::new(),
812 &mut Rowlike::Row(Row {
813 label: "nope".to_owned(),
814 entries: HashMap::from([
815 ("bar".to_owned(), vec![Some("r".to_owned())]),
816 ("foo".to_owned(), vec![Some("f".to_owned())]),
821 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>
829 static_columns: vec![],
830 hidden_columns: HashSet::from(["foo".to_owned()]),
833 &mut Rowlike::Row(Row {
834 label: "nope".to_owned(),
835 entries: HashMap::from([("foo".to_owned(), vec![Some("f".to_owned())]),]),
839 r#"<tr><th id="nope">nope</th><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')"></td></tr>
847 static_columns: vec![Some("foo".to_owned())],
848 hidden_columns: HashSet::from(["foo".to_owned()]),
851 &mut Rowlike::Row(Row {
852 label: "nope".to_owned(),
853 entries: HashMap::from([("foo".to_owned(), vec![Some("f".to_owned())]),]),
857 r#"<tr><th id="nope">nope</th><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')"></td></tr>