Skip to content

Commit

Permalink
zung_mini progbar (feat): Added ability to write message
Browse files Browse the repository at this point in the history
Now a message such as "Loading" can be displayed next to the spinner.
  • Loading branch information
ishaan26 committed Sep 30, 2024
1 parent 86eb6f5 commit f9f3968
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
9 changes: 6 additions & 3 deletions zung_mini/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ enum MiniCommands {
#[derive(Clone, Subcommand, Debug)]
#[command(arg_required_else_help = true)]
enum ProgBarCommands {
UnBounded,
UnBounded {
#[arg(short, long, default_value_t = String::from("Simulating Loading..."))]
message: String,
},
Bounded {
#[arg(long, default_value_t = String::from("["))]
delim_start: String,
Expand All @@ -53,9 +56,9 @@ impl MiniArgs {
use std::time::Duration;

match command {
ProgBarCommands::UnBounded => {
ProgBarCommands::UnBounded { message } => {
// test run UnBounded
for _ in (0..).progbar() {
for _ in (0..).progbar().with_message(message) {
sleep(Duration::from_millis(50))
}
}
Expand Down
15 changes: 13 additions & 2 deletions zung_mini/src/progbar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ pub struct Bounded<D: Display> {
bar: BarStyle,
}

/// Creates a ProgBar type where the `progbar()` method is called over any iterator.
/// Created through the [`progbar()`](ProgBarExt::progbar()) method called over any iterator.
#[derive(Debug)]
pub struct ProgBar<T, Bound> {
iterator: T,
step: usize,
bound: Bound,
message: String,
}

impl<T> ProgBar<T, UnBounded> {
Expand All @@ -142,12 +143,18 @@ impl<T> ProgBar<T, UnBounded> {
Self {
iterator,
step: 0,
message: String::from("Loading..."),
bound: UnBounded {
spinner: &['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
spinner_step: Cell::new(0),
},
}
}

pub fn with_message(mut self, msg: String) -> Self {
self.message = msg;
self
}
}

impl<T, Bound> ProgBar<T, Bound> {}
Expand All @@ -164,7 +171,10 @@ impl ProgBarDisplay for UnBounded {
progress.bound.spinner_step.set(0);
}

print!(" {}\r", progress.bound.spinner[spinner_step]);
print!(
" {} {}\r",
progress.bound.spinner[spinner_step], progress.message
);
io::stdout().flush().unwrap();

thread::sleep(Duration::from_millis(50));
Expand Down Expand Up @@ -251,6 +261,7 @@ where
iterator: self.iterator,
step: self.step,
bound,
message: String::new(),
}
}
}
Expand Down

0 comments on commit f9f3968

Please sign in to comment.