Skip to content

Commit

Permalink
Refactor progress indicator
Browse files Browse the repository at this point in the history
To minimize unnecessary negation - with an immutable, local, stack-allocated boolean variable
  • Loading branch information
jqnatividad committed May 20, 2022
1 parent 0862a27 commit 72b2f9a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
12 changes: 7 additions & 5 deletions src/cmd/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,20 +439,22 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

// prep progress bar
let progress = ProgressBar::new(0);
if !args.flag_quiet {
if args.flag_quiet {
progress.set_draw_target(ProgressDrawTarget::hidden());
} else {
let record_count = util::count_rows(&rconfig);
util::prep_progress(&progress, record_count);
} else {
progress.set_draw_target(ProgressDrawTarget::hidden());
}

let not_quiet = !args.flag_quiet;

#[allow(unused_assignments)]
let mut record = csv::StringRecord::new();
let records_iter = rdr.records();
for result in records_iter {
record = result?;

if !args.flag_quiet {
if not_quiet {
progress.inc(1);
}
let mut cell = record[column_index].to_owned();
Expand Down Expand Up @@ -505,7 +507,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

wtr.write_record(&record)?;
}
if !args.flag_quiet {
if not_quiet {
if args.cmd_geocode {
util::update_cache_info!(progress, SEARCH_CACHED);
}
Expand Down
12 changes: 7 additions & 5 deletions src/cmd/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,15 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
// prep progress bar
let progress = ProgressBar::new(0);
let mut record_count = 0;
if !args.flag_quiet {
if args.flag_quiet {
progress.set_draw_target(ProgressDrawTarget::hidden());
} else {
record_count = util::count_rows(&rconfig);
util::prep_progress(&progress, record_count);
} else {
progress.set_draw_target(ProgressDrawTarget::hidden());
}

let not_quiet = args.flag_quiet;

#[allow(unused_assignments)]
let mut record = csv::ByteRecord::new();
#[allow(unused_assignments)]
Expand All @@ -299,7 +301,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}

while rdr.read_byte_record(&mut record)? {
if !args.flag_quiet {
if not_quiet {
progress.inc(1);
}

Expand Down Expand Up @@ -359,7 +361,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}
}

if !args.flag_quiet {
if not_quiet {
if args.flag_redis {
util::update_cache_info!(progress, redis_cache_hits, record_count);
} else {
Expand Down
12 changes: 7 additions & 5 deletions src/cmd/foreach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

// prep progress bar
let progress = ProgressBar::new(0);
if !args.flag_quiet {
if args.flag_quiet {
progress.set_draw_target(ProgressDrawTarget::hidden());
} else {
let record_count = util::count_rows(&rconfig);
util::prep_progress(&progress, record_count);
} else {
progress.set_draw_target(ProgressDrawTarget::hidden());
}

let not_quiet = args.flag_quiet;

while rdr.read_byte_record(&mut record)? {
if !args.flag_quiet {
if not_quiet {
progress.inc(1);
}
let current_value = &record[column_index];
Expand Down Expand Up @@ -167,7 +169,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
cmd.wait().unwrap();
}
}
if !args.flag_quiet {
if not_quiet {
util::finish_progress(&progress);
}
Ok(())
Expand Down
12 changes: 7 additions & 5 deletions src/cmd/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,19 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
debug!("lua program: {:?}", lua_program);

let progress = ProgressBar::new(0);
if !args.flag_quiet {
if args.flag_quiet {
progress.set_draw_target(ProgressDrawTarget::hidden());
} else {
let record_count = util::count_rows(&rconfig);
util::prep_progress(&progress, record_count);
} else {
progress.set_draw_target(ProgressDrawTarget::hidden());
}

let not_quiet = args.flag_quiet;

let mut record = csv::StringRecord::new();

while rdr.read_record(&mut record)? {
if !args.flag_quiet {
if not_quiet {
progress.inc(1);
}

Expand Down Expand Up @@ -223,7 +225,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}
}
}
if !args.flag_quiet {
if not_quiet {
util::finish_progress(&progress);
}

Expand Down
12 changes: 7 additions & 5 deletions src/cmd/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,20 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}

let progress = ProgressBar::new(0);
if !args.flag_quiet {
if args.flag_quiet {
progress.set_draw_target(ProgressDrawTarget::hidden());
} else {
let record_count = util::count_rows(&rconfig);
util::prep_progress(&progress, record_count);
} else {
progress.set_draw_target(ProgressDrawTarget::hidden());
}

let not_quiet = args.flag_quiet;

let header_vec = util::safe_header_names(&headers, true);

let mut record = csv::StringRecord::new();
while rdr.read_record(&mut record)? {
if !args.flag_quiet {
if not_quiet {
progress.inc(1);
}

Expand Down Expand Up @@ -224,7 +226,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}
}
}
if !args.flag_quiet {
if not_quiet {
util::finish_progress(&progress);
}

Expand Down
12 changes: 7 additions & 5 deletions src/cmd/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
rconfig = rconfig.flexible(false);

#[cfg(any(feature = "full", feature = "lite"))]
if !args.flag_quiet {
util::prep_progress(&progress, record_count);
} else {
if args.flag_quiet {
progress.set_draw_target(ProgressDrawTarget::hidden());
} else {
util::prep_progress(&progress, record_count);
}

let not_quiet = args.flag_quiet;

// parse and compile supplied JSON Schema
let (schema_json, schema_compiled): (Value, JSONSchema) =
match load_json(&args.arg_json_schema.unwrap()) {
Expand Down Expand Up @@ -299,7 +301,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}

#[cfg(any(feature = "full", feature = "lite"))]
if !args.flag_quiet {
if not_quiet {
progress.inc(batch_size as u64);
}

Expand All @@ -310,7 +312,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
} // end infinite loop

#[cfg(any(feature = "full", feature = "lite"))]
if !args.flag_quiet {
if not_quiet {
progress.set_message(format!(
" validated {} records.",
progress.length().separate_with_commas()
Expand Down

0 comments on commit 72b2f9a

Please sign in to comment.