From 2cc9666599539763f72e625a863be4db9e6ae1c2 Mon Sep 17 00:00:00 2001 From: Robin Quintero Date: Wed, 6 Mar 2024 12:04:29 -0500 Subject: [PATCH] feat(build): #23 add progress bars - Add progress bars to the cloning and file complexity evaluation processes. - Add indicatif crate to Cargo.toml. --- Cargo.lock | 60 +++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/cognitive_complexity/mod.rs | 45 ++++++++++++++++++++++--- 3 files changed, 102 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 993a24c..5cc6943 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,6 +124,7 @@ dependencies = [ "csv", "env_logger", "ignore", + "indicatif", "log", "pyo3", "rayon", @@ -131,6 +132,19 @@ dependencies = [ "tempfile", ] +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -220,6 +234,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "env_filter" version = "0.1.0" @@ -314,12 +334,34 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "indicatif" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "763a5a8f45087d6bcea4222e7b72c291a054edf80e4ef6efd2a4979878c7bea3" +dependencies = [ + "console", + "instant", + "number_prefix", + "portable-atomic", + "unicode-width", +] + [[package]] name = "indoc" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + [[package]] name = "is-macro" version = "0.3.5" @@ -353,6 +395,12 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f35c735096c0293d313e8f2a641627472b83d01b937177fe76e5e2708d31e0d" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.153" @@ -470,6 +518,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + [[package]] name = "once_cell" version = "1.19.0" @@ -543,6 +597,12 @@ dependencies = [ "siphasher", ] +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + [[package]] name = "ppv-lite86" version = "0.2.17" diff --git a/Cargo.toml b/Cargo.toml index 25ed569..0f4a7f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ crate-type = ["cdylib"] csv = "1.3.0" env_logger = "0.11.1" ignore = "0.4.22" +indicatif = "0.17.8" log = "0.4.20" pyo3 = "0.19.0" rayon = "1.8.1" diff --git a/src/cognitive_complexity/mod.rs b/src/cognitive_complexity/mod.rs index 55ff6cb..b860024 100644 --- a/src/cognitive_complexity/mod.rs +++ b/src/cognitive_complexity/mod.rs @@ -2,6 +2,8 @@ pub mod utils; use crate::classes::{FileComplexity, FunctionComplexity}; use ignore::Walk; +use indicatif::ProgressBar; +use indicatif::ProgressStyle; use pyo3::prelude::*; use rayon::prelude::*; use rustpython_parser::{ @@ -11,6 +13,8 @@ use rustpython_parser::{ use std::env; use std::path; use std::process; +use std::sync::{Arc, Mutex}; +use std::thread; use tempfile::tempdir; use utils::{count_bool_ops, get_repo_name, is_decorator}; @@ -31,10 +35,30 @@ pub fn main( env::set_current_dir(&dir)?; - let _output = process::Command::new("git") - .args(&["clone", path]) - .output() - .expect("failed to execute process"); + let cloning_done = Arc::new(Mutex::new(false)); + let cloning_done_clone = Arc::clone(&cloning_done); + let path_clone = path.to_owned(); // Clone the path variable + + thread::spawn(move || { + let _output = process::Command::new("git") + .args(&["clone", &path_clone]) // Use the cloned path variable + .output() + .expect("failed to execute process"); + + let mut done = cloning_done_clone.lock().unwrap(); + *done = true; + }); + + let pb = ProgressBar::new_spinner(); + pb.set_style(ProgressStyle::default_spinner()); + pb.set_message("Cloning repository..."); + + while !*cloning_done.lock().unwrap() { + pb.tick(); + thread::sleep(std::time::Duration::from_millis(100)); + } + + pb.finish_with_message("Repository cloned!"); let repo_path = dir.path().join(&repo_name).to_str().unwrap().to_string(); @@ -84,9 +108,20 @@ fn evaluate_dir( } } + let pb = ProgressBar::new(files_paths.len() as u64); + pb.set_style( + indicatif::ProgressStyle::default_bar() + .template( + "{spiner:.green} [{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}", + ) + .unwrap() + .progress_chars("##-"), + ); + let files_complexity_result: Result, PyErr> = files_paths .par_iter() .map(|file_path| { + pb.inc(1); match cognitive_complexity(file_path, parent_dir, max_complexity, file_level) { Ok(file_complexity) => Ok(file_complexity), Err(e) => Err(e), @@ -94,6 +129,8 @@ fn evaluate_dir( }) .collect(); + pb.finish_with_message("Done!"); + match files_complexity_result { Ok(files_complexity) => Ok(files_complexity), Err(e) => Err(e),