From ec365a0a575fc19ace3ce458041f3c916260692e Mon Sep 17 00:00:00 2001 From: Lab Rat Date: Wed, 21 Aug 2024 10:57:26 +0530 Subject: [PATCH] fix(workflow_queue): fix prompt dialog mangled into progress bar --- Cargo.toml | 1 + src/cmd/default.rs | 2 +- src/events.rs | 28 ++++++-- src/hook_executor/execute.rs | 1 - src/workflow_queue.rs | 134 +++++++++++++++-------------------- 5 files changed, 83 insertions(+), 83 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 72ff409..5d7d400 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ repository = "https://github.com/Gyan172004/bgit" description = "User-friendly Git wrapper for beginners, automating essential tasks like adding, committing, and pushing changes. It includes smart rules to avoid common pitfalls, such as accidentally adding sensitive files or directories and has exclusive support for portable hooks!" categories = ["development-tools::testing", "command-line-utilities", "development-tools"] keywords = ["git", "cli", "wrapper", "beginner", "friendly", "portable", "libgit-rs", "hooks", "automation", "smart-rules"] +rust-version = "1.80" [dependencies] clap = { version = "4.5.15", features = ["derive"] } diff --git a/src/cmd/default.rs b/src/cmd/default.rs index cf283d1..2439e65 100644 --- a/src/cmd/default.rs +++ b/src/cmd/default.rs @@ -10,7 +10,7 @@ pub(crate) fn default_cmd_workflow() { ))), ); match workflow_queue.execute() { - Ok(_) => {}, + Ok(_) => {} Err(err) => eprintln!("Error : {:#?}", err), }; } diff --git a/src/events.rs b/src/events.rs index 97edb03..4e43402 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,6 +1,6 @@ +use colored::Colorize; use git2::Repository; use std::env; -use colored::Colorize; use crate::{ bgit_error::{BGitError, NO_RULE, NO_STEP}, @@ -42,12 +42,20 @@ pub(crate) trait AtomicEvent { // Hooks fn pre_execute_hook(&self) -> Result> { - eprintln!("{} Running pre-execute hook for {}", PENGUIN_EMOJI, self.get_name().cyan().bold()); + eprintln!( + "{} Running pre-execute hook for {}", + PENGUIN_EMOJI, + self.get_name().cyan().bold() + ); let pre_event_hook_file_name: String = format!("pre_{}", self.get_name()); self.execute_hook(&pre_event_hook_file_name) } fn post_execute_hook(&self) -> Result> { - eprintln!("{} Running post-execute hook for {}", PENGUIN_EMOJI, self.get_name().cyan().bold()); + eprintln!( + "{} Running post-execute hook for {}", + PENGUIN_EMOJI, + self.get_name().cyan().bold() + ); let post_event_hook_file_name: String = format!("post_{}", self.get_name()); self.execute_hook(&post_event_hook_file_name) } @@ -81,7 +89,11 @@ pub(crate) trait AtomicEvent { // Check against set of rules before running the event fn check_rules(&self) -> Result> { - eprintln!("{} Running pre-check rules for {}", PENGUIN_EMOJI, self.get_name().cyan().bold()); + eprintln!( + "{} Running pre-check rules for {}", + PENGUIN_EMOJI, + self.get_name().cyan().bold() + ); for rule in self.get_pre_check_rule().iter() { let rule_passed = rule.execute()?; if !rule_passed { @@ -115,8 +127,12 @@ pub(crate) trait AtomicEvent { NO_RULE, ))); } - - eprintln!("{} Running executor for event {}", PENGUIN_EMOJI, self.get_name().cyan().bold()); + + eprintln!( + "{} Running executor for event {}", + PENGUIN_EMOJI, + self.get_name().cyan().bold() + ); let raw_executor_status = self.raw_execute()?; if !raw_executor_status { return Err(Box::new(BGitError::new( diff --git a/src/hook_executor/execute.rs b/src/hook_executor/execute.rs index 6d6c26c..63c9be5 100644 --- a/src/hook_executor/execute.rs +++ b/src/hook_executor/execute.rs @@ -10,7 +10,6 @@ use std::{ #[cfg(unix)] use std::os::unix::fs::PermissionsExt; - #[cfg(not(unix))] pub(crate) fn execute_hook_util( pre_event_hook_path: &Path, diff --git a/src/workflow_queue.rs b/src/workflow_queue.rs index fa8dd53..6826560 100644 --- a/src/workflow_queue.rs +++ b/src/workflow_queue.rs @@ -1,6 +1,6 @@ use crate::bgit_error::{BGitError, NO_EVENT, NO_RULE, NO_STEP}; -use crate::step::Step; use crate::step::Task::{ActionStepTask, PromptStepTask}; +use crate::step::{Step, Task}; use colored::Colorize; use indicatif::{HumanDuration, ProgressBar, ProgressStyle}; use std::time::Duration; @@ -11,62 +11,76 @@ const HATCHING_CHICK_EMOJI: &str = "🐣"; pub(crate) struct WorkflowQueue { name: String, init_step: Step, + pb: ProgressBar, } impl WorkflowQueue { pub(crate) fn new(name: &str, init_step: Step) -> Self { + // Initialize spinner for progress indication + let pb = ProgressBar::new_spinner(); + pb.enable_steady_tick(Duration::from_millis(200)); + pb.set_style( + ProgressStyle::with_template( + "{spinner:.cyan/blue.bold} [{pos:.yellow}/?] Executing step: {wide_msg:.green}", + ) + .unwrap(), + ); + WorkflowQueue { name: name.to_owned(), init_step, + pb, + } + } + + fn run_step_and_traverse(&self, task: &Task) -> Result> { + match task { + ActionStepTask(action_step_task) => { + eprintln!( + "{} Running Action Step: {}", + HATCHING_CHICK_EMOJI, + action_step_task.get_name().cyan().bold() + ); + self.pb.set_message(format!( + "Step '{}' in progress...", + action_step_task.get_name().bold() + )); + let action_step_result = action_step_task.execute()?; + + self.pb.inc(1); + self.pb.tick(); + + Ok(action_step_result) + } + PromptStepTask(prompt_step_task) => { + self.pb.disable_steady_tick(); + eprintln!( + "{} Running Prompt Step: {}", + HATCHING_CHICK_EMOJI, + prompt_step_task.get_name().cyan().bold() + ); + + self.pb.set_message(format!( + "Step '{}' in progress...", + prompt_step_task.get_name().bold() + )); + let prompt_step_result = prompt_step_task.execute()?; + self.pb.enable_steady_tick(Duration::from_millis(200)); + + self.pb.inc(1); + self.pb.tick(); + + Ok(prompt_step_result) + } } } pub(crate) fn execute(&self) -> Result> { if let Step::Start(task) = &self.init_step { let started = Instant::now(); - // Initialize spinner for progress indication - let pb = ProgressBar::new_spinner(); - pb.enable_steady_tick(Duration::from_millis(200)); - pb.set_style( - ProgressStyle::with_template( - "{spinner:.cyan/blue.bold} [{pos:.yellow}/?] Executing step: {wide_msg:.green}", - ) - .unwrap(), - ); - - pb.inc(1); - pb.tick(); - - let mut next_step: Step = match task { - ActionStepTask(action_step_task) => { - eprintln!( - "{} Running Action Step: {}", - HATCHING_CHICK_EMOJI, - action_step_task.get_name().cyan().bold() - ); - pb.set_message(format!( - "Step '{}' in progress...", - action_step_task.get_name().bold() - )); - action_step_task.execute()? - } - PromptStepTask(prompt_step_task) => { - eprintln!( - "{} Running Prompt Step: {}", - HATCHING_CHICK_EMOJI, - prompt_step_task.get_name().cyan().bold() - ); - - pb.set_message(format!( - "Step '{}' in progress...", - prompt_step_task.get_name().bold() - )); - prompt_step_task.execute()? - } - }; - pb.inc(1); - pb.tick(); + let mut next_step: Step = self.run_step_and_traverse(task)?; + while next_step != Step::Stop { if let Step::Start(_) = next_step { return Err(Box::new(BGitError::new( @@ -80,43 +94,13 @@ impl WorkflowQueue { } if let Step::Task(task) = next_step { - next_step = match task { - ActionStepTask(action_step_task) => { - eprintln!( - "{} Running Action Step: {}", - HATCHING_CHICK_EMOJI, - action_step_task.get_name().cyan().bold() - ); - - pb.set_message(format!( - "Step '{}' in progress...", - action_step_task.get_name().bold() - )); - action_step_task.execute()? - } - PromptStepTask(prompt_step_task) => { - eprintln!( - "{} Running Prompt Task: {}", - HATCHING_CHICK_EMOJI, - prompt_step_task.get_name().cyan().bold() - ); - - pb.set_message(format!( - "Step '{}' in progress...", - prompt_step_task.get_name().bold() - )); - prompt_step_task.execute()? - } - } + next_step = self.run_step_and_traverse(&task)?; } else { unreachable!("This code is unreachable") } - - pb.inc(1); - pb.tick(); } - pb.finish_with_message("Workflow complete"); + self.pb.finish_with_message("Workflow complete"); if next_step == Step::Stop { println!("Done in {}", HumanDuration(started.elapsed()));