]> git.scottworley.com Git - tablify/blame_incremental - src/lib.rs
Tweak appearance of leftovers column
[tablify] / src / lib.rs
... / ...
CommitLineData
1use std::borrow::ToOwned;
2use std::collections::HashMap;
3use std::fmt::Write;
4use std::io::BufRead;
5use std::iter::Iterator;
6
7pub struct Config {
8 pub column_threshold: usize,
9}
10
11const HEADER: &str = r#"<!DOCTYPE html>
12<html>
13<head>
14 <meta charset="utf-8">
15 <meta name="viewport" content="width=device-width, initial-scale=1">
16 <style>
17 td { text-align: center; }
18 /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
19 th, td { white-space: nowrap; }
20 th { text-align: left; font-weight: normal; }
21 table { border-collapse: collapse }
22 tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
23 tr.key > th > div { width: 1em; }
24 tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
25 td { border: thin solid gray; }
26 td.leftover { text-align: left; border: none; padding-left: .4em; }
27 td.yes { border: thin solid gray; background-color: #ddd; }
28 /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */
29 .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
30 </style>
31 <script>
32 function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( "highlight"); } }
33 function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove("highlight"); } }
34 function h2(a, b) { highlight(a); highlight(b); }
35 function ch2(a, b) { clear_highlight(a); clear_highlight(b); }
36 </script>
37</head>
38<body>
39 <table>
40 <tbody>
41"#;
42const FOOTER: &str = " </tbody>
43 </table>
44</body>
45</html>";
46
47#[derive(PartialEq, Eq, Debug)]
48pub struct HTML(String);
49impl HTML {
50 fn escape(value: &str) -> HTML {
51 let mut escaped: String = String::new();
52 for c in value.chars() {
53 match c {
54 '>' => escaped.push_str("&gt;"),
55 '<' => escaped.push_str("&lt;"),
56 '\'' => escaped.push_str("&#39;"),
57 '"' => escaped.push_str("&quot;"),
58 '&' => escaped.push_str("&amp;"),
59 ok_c => escaped.push(ok_c),
60 }
61 }
62 HTML(escaped)
63 }
64}
65impl From<&str> for HTML {
66 fn from(value: &str) -> HTML {
67 HTML(String::from(value))
68 }
69}
70impl FromIterator<HTML> for HTML {
71 fn from_iter<T>(iter: T) -> HTML
72 where
73 T: IntoIterator<Item = HTML>,
74 {
75 HTML(iter.into_iter().map(|html| html.0).collect::<String>())
76 }
77}
78impl std::fmt::Display for HTML {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 write!(f, "{}", self.0)
81 }
82}
83
84#[derive(Debug, PartialEq, Eq)]
85enum InputLine<'a> {
86 Blank,
87 RowHeader(&'a str),
88 Entry(&'a str, Option<&'a str>),
89}
90impl<'a> From<&'a str> for InputLine<'a> {
91 fn from(value: &'a str) -> InputLine<'a> {
92 let trimmed = value.trim_end();
93 if trimmed.is_empty() {
94 InputLine::Blank
95 } else if !trimmed.starts_with(' ') {
96 InputLine::RowHeader(value.trim())
97 } else {
98 match value.split_once(':') {
99 None => InputLine::Entry(value.trim(), None),
100 Some((col, instance)) => InputLine::Entry(col.trim(), Some(instance.trim())),
101 }
102 }
103 }
104}
105
106#[derive(Debug, PartialEq, Eq)]
107struct Row {
108 label: String,
109 entries: HashMap<String, Vec<Option<String>>>,
110}
111
112struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
113 input: std::iter::Enumerate<Input>,
114 row: Option<Row>,
115}
116impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
117 fn new(input: Input) -> Self {
118 Self {
119 input: input.enumerate(),
120 row: None,
121 }
122 }
123}
124impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
125 type Item = Result<Row, std::io::Error>;
126 fn next(&mut self) -> Option<Self::Item> {
127 loop {
128 match self.input.next() {
129 None => return Ok(std::mem::take(&mut self.row)).transpose(),
130 Some((_, Err(e))) => return Some(Err(e)),
131 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
132 InputLine::Blank if self.row.is_some() => {
133 return Ok(std::mem::take(&mut self.row)).transpose()
134 }
135 InputLine::Blank => {}
136 InputLine::Entry(col, instance) => match &mut self.row {
137 None => {
138 return Some(Err(std::io::Error::other(format!(
139 "{}: Entry with no header",
140 n + 1
141 ))))
142 }
143 Some(ref mut row) => {
144 row.entries
145 .entry(col.to_owned())
146 .and_modify(|is| is.push(instance.map(ToOwned::to_owned)))
147 .or_insert_with(|| vec![instance.map(ToOwned::to_owned)]);
148 }
149 },
150 InputLine::RowHeader(row) => {
151 let prev = std::mem::take(&mut self.row);
152 self.row = Some(Row {
153 label: row.to_owned(),
154 entries: HashMap::new(),
155 });
156 if prev.is_some() {
157 return Ok(prev).transpose();
158 }
159 }
160 },
161 }
162 }
163 }
164}
165
166fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<Row, std::io::Error>> {
167 Reader::new(std::io::BufReader::new(input).lines())
168}
169
170fn column_counts(rows: &[Row]) -> Vec<(usize, String)> {
171 let mut counts: Vec<_> = rows
172 .iter()
173 .flat_map(|r| r.entries.keys())
174 .fold(HashMap::new(), |mut cs, col| {
175 cs.entry(col.to_owned())
176 .and_modify(|n| *n += 1)
177 .or_insert(1);
178 cs
179 })
180 .into_iter()
181 .map(|(col, n)| (n, col))
182 .collect();
183 counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
184 counts
185}
186fn column_order(config: &Config, rows: &[Row]) -> Vec<String> {
187 column_counts(rows)
188 .into_iter()
189 .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col))
190 .collect()
191}
192
193fn render_one_instance(instance: &Option<String>) -> HTML {
194 match instance {
195 None => HTML::from("✓"),
196 Some(instance) => HTML::escape(instance.as_ref()),
197 }
198}
199
200fn render_instances(instances: &[Option<String>]) -> HTML {
201 let all_empty = instances.iter().all(Option::is_none);
202 if all_empty && instances.len() == 1 {
203 HTML::from("")
204 } else if all_empty {
205 HTML(format!("{}", instances.len()))
206 } else {
207 HTML(
208 instances
209 .iter()
210 .map(render_one_instance)
211 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
212 .collect::<Vec<_>>()
213 .join(" "),
214 )
215 }
216}
217
218fn render_cell(col: &str, row: &mut Row) -> HTML {
219 let row_label = HTML::escape(row.label.as_ref());
220 let col_label = HTML::escape(col);
221 let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
222 let class = HTML::from(if instances.is_none() { "" } else { "yes" });
223 let contents = match instances {
224 None => HTML::from(""),
225 Some(is) => render_instances(is),
226 };
227 row.entries.remove(col);
228 HTML(format!(
229 r#"<td class="{class}" onmouseover="h2('{row_label}','{col_label}')" onmouseout="ch2('{row_label}','{col_label}')">{contents}</td>"#
230 ))
231}
232
233fn render_leftover(notcol: &str, instances: &[Option<String>]) -> HTML {
234 let label = HTML::escape(notcol);
235 let rest = render_instances(instances);
236 if rest == HTML::from("") {
237 HTML(format!("{label}"))
238 } else {
239 HTML(format!("{label}: {rest}"))
240 }
241}
242
243fn render_all_leftovers(row: &Row) -> HTML {
244 let mut order: Vec<_> = row.entries.keys().collect();
245 order.sort_unstable();
246 HTML(
247 order
248 .into_iter()
249 .map(|notcol| render_leftover(notcol, row.entries.get(notcol).expect("Key vanished?!")))
250 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
251 .collect::<Vec<_>>()
252 .join(", "),
253 )
254}
255
256fn render_row(columns: &[String], row: &mut Row) -> HTML {
257 let row_label = HTML::escape(row.label.as_ref());
258 let cells = columns
259 .iter()
260 .map(|col| render_cell(col, row))
261 .collect::<HTML>();
262 let leftovers = render_all_leftovers(row);
263 HTML(format!(
264 "<tr><th id=\"{row_label}\">{row_label}</th>{cells}<td class=\"leftover\" onmouseover=\"highlight('{row_label}')\" onmouseout=\"clear_highlight('{row_label}')\">{leftovers}</td></tr>\n"
265 ))
266}
267
268fn render_column_headers(columns: &[String]) -> HTML {
269 HTML(
270 String::from(r#"<tr class="key"><th></th>"#)
271 + &columns.iter().fold(String::new(), |mut acc, col| {
272 let col_header = HTML::escape(col.as_ref());
273 write!(
274 &mut acc,
275 r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
276 )
277 .unwrap();
278 acc
279 })
280 + "</tr>\n",
281 )
282}
283
284/// # Errors
285///
286/// Will return `Err` if
287/// * there's an i/o error while reading `input`
288/// * the log has invalid syntax:
289/// * an indented line with no preceding non-indented line
290pub fn tablify(config: &Config, input: impl std::io::Read) -> Result<HTML, std::io::Error> {
291 let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
292 let columns = column_order(config, &rows);
293 Ok(HTML(format!(
294 "{HEADER}{}{}{FOOTER}",
295 render_column_headers(&columns),
296 rows.into_iter()
297 .map(|mut r| render_row(&columns, &mut r))
298 .collect::<HTML>()
299 )))
300}
301
302#[cfg(test)]
303mod tests {
304 use super::*;
305
306 #[test]
307 fn test_parse_line() {
308 assert_eq!(InputLine::from(""), InputLine::Blank);
309 assert_eq!(InputLine::from(" "), InputLine::Blank);
310 assert_eq!(InputLine::from("foo"), InputLine::RowHeader("foo"));
311 assert_eq!(InputLine::from("foo "), InputLine::RowHeader("foo"));
312 assert_eq!(InputLine::from(" foo"), InputLine::Entry("foo", None));
313 assert_eq!(
314 InputLine::from(" foo:bar"),
315 InputLine::Entry("foo", Some("bar"))
316 );
317 assert_eq!(
318 InputLine::from(" foo: bar"),
319 InputLine::Entry("foo", Some("bar"))
320 );
321 assert_eq!(
322 InputLine::from(" foo: bar "),
323 InputLine::Entry("foo", Some("bar"))
324 );
325 assert_eq!(
326 InputLine::from(" foo: bar "),
327 InputLine::Entry("foo", Some("bar"))
328 );
329 assert_eq!(
330 InputLine::from(" foo : bar "),
331 InputLine::Entry("foo", Some("bar"))
332 );
333 }
334
335 #[test]
336 fn test_read_rows() {
337 assert_eq!(
338 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
339 vec![Row {
340 label: "foo".to_owned(),
341 entries: HashMap::new(),
342 }]
343 );
344 assert_eq!(
345 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
346 vec![Row {
347 label: "bar".to_owned(),
348 entries: HashMap::new(),
349 }]
350 );
351 assert_eq!(
352 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
353 vec![
354 Row {
355 label: "foo".to_owned(),
356 entries: HashMap::new(),
357 },
358 Row {
359 label: "bar".to_owned(),
360 entries: HashMap::new(),
361 }
362 ]
363 );
364 assert_eq!(
365 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
366 vec![Row {
367 label: "foo".to_owned(),
368 entries: HashMap::from([("bar".to_owned(), vec![None])]),
369 }]
370 );
371 assert_eq!(
372 read_rows(&b"foo\n bar\n baz\n"[..])
373 .flatten()
374 .collect::<Vec<_>>(),
375 vec![Row {
376 label: "foo".to_owned(),
377 entries: HashMap::from([
378 ("bar".to_owned(), vec![None]),
379 ("baz".to_owned(), vec![None])
380 ]),
381 }]
382 );
383 assert_eq!(
384 read_rows(&b"foo\n\nbar\n"[..])
385 .flatten()
386 .collect::<Vec<_>>(),
387 vec![
388 Row {
389 label: "foo".to_owned(),
390 entries: HashMap::new(),
391 },
392 Row {
393 label: "bar".to_owned(),
394 entries: HashMap::new(),
395 }
396 ]
397 );
398 assert_eq!(
399 read_rows(&b"foo\n \nbar\n"[..])
400 .flatten()
401 .collect::<Vec<_>>(),
402 vec![
403 Row {
404 label: "foo".to_owned(),
405 entries: HashMap::new(),
406 },
407 Row {
408 label: "bar".to_owned(),
409 entries: HashMap::new(),
410 }
411 ]
412 );
413 assert_eq!(
414 read_rows(&b"foo \n bar \n"[..])
415 .flatten()
416 .collect::<Vec<_>>(),
417 vec![Row {
418 label: "foo".to_owned(),
419 entries: HashMap::from([("bar".to_owned(), vec![None])]),
420 }]
421 );
422
423 let bad = read_rows(&b" foo"[..]).next().unwrap();
424 assert!(bad.is_err());
425 assert!(format!("{bad:?}").contains("1: Entry with no header"));
426
427 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
428 assert!(bad2.is_err());
429 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
430 }
431
432 #[test]
433 fn test_column_counts() {
434 assert_eq!(
435 column_counts(
436 &read_rows(&b"foo\n bar\n baz\n"[..])
437 .collect::<Result<Vec<_>, _>>()
438 .unwrap()
439 ),
440 vec![(1, String::from("bar")), (1, String::from("baz"))]
441 );
442 assert_eq!(
443 column_counts(
444 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
445 .collect::<Result<Vec<_>, _>>()
446 .unwrap()
447 ),
448 vec![(2, String::from("baz")), (1, String::from("bar"))]
449 );
450 assert_eq!(
451 column_counts(
452 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
453 .collect::<Result<Vec<_>, _>>()
454 .unwrap()
455 ),
456 vec![(2, String::from("baz")), (1, String::from("bar"))]
457 );
458 assert_eq!(
459 column_counts(
460 &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..])
461 .collect::<Result<Vec<_>, _>>()
462 .unwrap()
463 ),
464 vec![(2, String::from("baz")), (1, String::from("bar"))]
465 );
466 }
467
468 #[test]
469 fn test_render_cell() {
470 assert_eq!(
471 render_cell(
472 "foo",
473 &mut Row {
474 label: "nope".to_owned(),
475 entries: HashMap::new(),
476 }
477 ),
478 HTML::from(
479 r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
480 )
481 );
482 assert_eq!(
483 render_cell(
484 "foo",
485 &mut Row {
486 label: "nope".to_owned(),
487 entries: HashMap::from([("bar".to_owned(), vec![None])]),
488 }
489 ),
490 HTML::from(
491 r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
492 )
493 );
494 assert_eq!(
495 render_cell(
496 "foo",
497 &mut Row {
498 label: "nope".to_owned(),
499 entries: HashMap::from([("foo".to_owned(), vec![None])]),
500 }
501 ),
502 HTML::from(
503 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
504 )
505 );
506 assert_eq!(
507 render_cell(
508 "foo",
509 &mut Row {
510 label: "nope".to_owned(),
511 entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
512 }
513 ),
514 HTML::from(
515 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">2</td>"#
516 )
517 );
518 assert_eq!(
519 render_cell(
520 "foo",
521 &mut Row {
522 label: "nope".to_owned(),
523 entries: HashMap::from([(
524 "foo".to_owned(),
525 vec![Some("5".to_owned()), Some("10".to_owned())]
526 )]),
527 }
528 ),
529 HTML::from(
530 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 10</td>"#
531 )
532 );
533 assert_eq!(
534 render_cell(
535 "foo",
536 &mut Row {
537 label: "nope".to_owned(),
538 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
539 }
540 ),
541 HTML::from(
542 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 ✓</td>"#
543 )
544 );
545 assert_eq!(
546 render_cell(
547 "heart",
548 &mut Row {
549 label: "nope".to_owned(),
550 entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
551 }
552 ),
553 HTML::from(
554 r#"<td class="yes" onmouseover="h2('nope','heart')" onmouseout="ch2('nope','heart')">&lt;3</td>"#
555 )
556 );
557 assert_eq!(
558 render_cell(
559 "foo",
560 &mut Row {
561 label: "bob's".to_owned(),
562 entries: HashMap::from([("foo".to_owned(), vec![None])]),
563 }
564 ),
565 HTML::from(
566 r#"<td class="yes" onmouseover="h2('bob&#39;s','foo')" onmouseout="ch2('bob&#39;s','foo')"></td>"#
567 )
568 );
569 let mut r = Row {
570 label: "nope".to_owned(),
571 entries: HashMap::from([
572 ("foo".to_owned(), vec![None]),
573 ("baz".to_owned(), vec![None]),
574 ]),
575 };
576 assert_eq!(r.entries.len(), 2);
577 render_cell("foo", &mut r);
578 assert_eq!(r.entries.len(), 1);
579 render_cell("bar", &mut r);
580 assert_eq!(r.entries.len(), 1);
581 render_cell("baz", &mut r);
582 assert_eq!(r.entries.len(), 0);
583 }
584
585 #[test]
586 fn test_render_leftovers() {
587 assert_eq!(
588 render_all_leftovers(&Row {
589 label: "nope".to_owned(),
590 entries: HashMap::from([("foo".to_owned(), vec![None])]),
591 }),
592 HTML::from("foo")
593 );
594 assert_eq!(
595 render_all_leftovers(&Row {
596 label: "nope".to_owned(),
597 entries: HashMap::from([
598 ("foo".to_owned(), vec![None]),
599 ("bar".to_owned(), vec![None])
600 ]),
601 }),
602 HTML::from("bar, foo")
603 );
604 assert_eq!(
605 render_all_leftovers(&Row {
606 label: "nope".to_owned(),
607 entries: HashMap::from([
608 ("foo".to_owned(), vec![None]),
609 ("bar".to_owned(), vec![None, None])
610 ]),
611 }),
612 HTML::from("bar: 2, foo")
613 );
614 }
615
616 #[test]
617 fn test_render_row() {
618 assert_eq!(
619 render_row(
620 &["foo".to_owned()],
621 &mut Row {
622 label: "nope".to_owned(),
623 entries: HashMap::from([("bar".to_owned(), vec![None])]),
624 }
625 ),
626 HTML::from(
627 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>
628"#
629 )
630 );
631 }
632}