Skip to content

Commit

Permalink
fix(workflow_queue): fix prompt dialog mangled into progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
rootCircle committed Aug 21, 2024
1 parent 376da84 commit ec365a0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 83 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) fn default_cmd_workflow() {
))),
);
match workflow_queue.execute() {
Ok(_) => {},
Ok(_) => {}
Err(err) => eprintln!("Error : {:#?}", err),
};
}
28 changes: 22 additions & 6 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -42,12 +42,20 @@ pub(crate) trait AtomicEvent {

// Hooks
fn pre_execute_hook(&self) -> Result<bool, Box<BGitError>> {
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<bool, Box<BGitError>> {
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)
}
Expand Down Expand Up @@ -81,7 +89,11 @@ pub(crate) trait AtomicEvent {

// Check against set of rules before running the event
fn check_rules(&self) -> Result<bool, Box<BGitError>> {
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 {
Expand Down Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion src/hook_executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
134 changes: 59 additions & 75 deletions src/workflow_queue.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Step, Box<BGitError>> {
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<bool, Box<BGitError>> {
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(
Expand All @@ -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()));
Expand Down

0 comments on commit ec365a0

Please sign in to comment.