#[derive(PartialEq, Eq, Debug)]
struct Config {
column_threshold: usize,
- static_columns: Vec<String>,
+ static_columns: Vec<Option<String>>,
}
impl Config {
fn apply_command(&mut self, line_num: usize, cmd: &str) -> Result<(), std::io::Error> {
)
})?;
} 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,
Ok(())
}
}
+impl Default for Config {
+ fn default() -> Self {
+ Self {
+ column_threshold: 2,
+ static_columns: vec![],
+ }
+ }
+}
const HEADER: &str = r#"<!DOCTYPE html>
<html>
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; }
}
fn read_input(input: impl std::io::Read) -> Result<(Vec<Rowlike>, Config), std::io::Error> {
- let mut config = Config {
- column_threshold: 2,
- static_columns: vec![],
- };
+ let mut config = Config::default();
let reader = Reader::new(&mut config, std::io::BufReader::new(input).lines());
reader
.collect::<Result<Vec<_>, _>>()
let static_columns: HashSet<&str> = config
.static_columns
.iter()
+ .flatten()
.map(std::string::String::as_str)
.collect();
column_counts(rows)
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#"<td class="spacer_col"></td>"#),
+ })
.collect::<HTML>();
let dynamic_cells = columns
.iter()
}
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#"<tr class="key"><th></th>"#)
- + &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#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
- )
+ + &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#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
+ )
+ }
+ None => write!(&mut acc, r#"<th class="col_spacer"></th>"#),
+ }
.unwrap();
acc
- },
- )
+ })
+ "</tr>\n",
)
}
);
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"[..]);
fn test_render_row() {
assert_eq!(
render_row(
- &Config {
- column_threshold: 0,
- static_columns: vec![],
- },
+ &Config::default(),
&["foo".to_owned()],
&mut Rowlike::Row(Row {
label: "nope".to_owned(),
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 {
),
HTML::from(
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>
+"#
+ )
+ );
+ 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#"<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>
"#
)
);