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
1 changed files with 11 additions and 6 deletions

View File

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