From 44ea09d7d357ea8cbaaaca7cdd6c756f8bce4754 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Fri, 26 Aug 2022 13:50:54 +1000 Subject: [PATCH] Allow customising report style in CLI HTML output --- src/cli/stv.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/cli/stv.rs b/src/cli/stv.rs index a306224..7d9fa86 100644 --- a/src/cli/stv.rs +++ b/src/cli/stv.rs @@ -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(mut election: Election, filename: &str, opts: STVOptions) -> Result<(), i32> +fn count_election_html(mut election: Election, 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 "" 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 "" 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 "" let idx = result_rows[row].len() - 5; result_rows[row].insert_str(idx, &cell);