Allow customising report style in CLI HTML output

This commit is contained in:
RunasSudo 2022-08-26 13:50:54 +10:00
parent 815055d6e6
commit 44ea09d7d3
No known key found for this signature in database
GPG Key ID: 7234E476BF21C61A

View File

@ -178,6 +178,7 @@ pub struct SubcmdOptions {
// ---------------------
// -- Output settings --
/// Output format
#[clap(help_heading=Some("OUTPUT"), short, long, possible_values=&["text", "csv", "html"], default_value="text")]
output: String,
@ -196,6 +197,10 @@ pub struct SubcmdOptions {
/// Print votes to specified decimal places in results report
#[clap(help_heading=Some("OUTPUT"), long, default_value="2", value_name="dps")]
pp_decimals: usize,
/// (HTML) Report style
#[clap(help_heading=Some("OUTPUT"), long, possible_values=&["votes", "votes_transposed", "ballots_votes"], default_value="votes_transposed")]
report_style: String,
}
/// Entrypoint for subcommand
@ -327,7 +332,7 @@ where
match cmd_opts.output.as_str() {
"text" => { return count_election_text(election, &cmd_opts.filename, opts); }
"csv" => { return count_election_csv(election, opts); }
"html" => { return count_election_html(election, &cmd_opts.filename, opts); }
"html" => { return count_election_html(election, &cmd_opts.filename, opts, &cmd_opts.report_style); }
_ => unreachable!()
}
}
@ -628,7 +633,7 @@ where
// -----------------------------------
// HTML report in the style of wasm UI
fn count_election_html<N: Number>(mut election: Election<N>, filename: &str, opts: STVOptions) -> Result<(), i32>
fn count_election_html<N: Number>(mut election: Election<N>, filename: &str, opts: STVOptions, report_style: &str) -> Result<(), i32>
where
for<'r> &'r N: ops::Add<&'r N, Output=N>,
for<'r> &'r N: ops::Sub<&'r N, Output=N>,
@ -658,7 +663,7 @@ where
let mut state = CountState::new(&election);
// TODO: Enable report_style to be customised
let mut result_rows = stv::html::init_results_table(&election, &opts, "votes_transposed");
let mut result_rows = stv::html::init_results_table(&election, &opts, report_style);
let mut stage_comments = Vec::new();
@ -674,7 +679,7 @@ where
}
}
let stage_result = stv::html::update_results_table(1, &state, &opts, "votes_transposed");
let stage_result = stv::html::update_results_table(1, &state, &opts, report_style);
for (row, cell) in stage_result.into_iter().enumerate() {
// 5 characters from end to insert before "</tr>"
let idx = result_rows[row].len() - 5;
@ -702,7 +707,7 @@ where
stage_num += 1;
let stage_result = stv::html::update_results_table(stage_num, &state, &opts, "votes_transposed");
let stage_result = stv::html::update_results_table(stage_num, &state, &opts, report_style);
for (row, cell) in stage_result.into_iter().enumerate() {
// 5 characters from end to insert before "</tr>"
let idx = result_rows[row].len() - 5;
@ -715,7 +720,7 @@ where
// ----------------
// Candidate states
for (row, cell) in stv::html::finalise_results_table(&state, "votes_transposed").into_iter().enumerate() {
for (row, cell) in stv::html::finalise_results_table(&state, report_style).into_iter().enumerate() {
// 5 characters from end to insert before "</tr>"
let idx = result_rows[row].len() - 5;
result_rows[row].insert_str(idx, &cell);