spinners icon indicating copy to clipboard operation
spinners copied to clipboard

Add a configurable drop behavior for Spinner

Open rustn00b opened this issue 3 years ago • 0 comments

This commit adds an on_drop method to Spinner, the method is a "Builder method" (called on a Spinner taking ownership and returns a new Spinner instance). The existing Spinner API therefore continue to work as they did before with no changes to methods or their parameters.

By default, i.e. when on_drop is not used to set the default behavior of a Spinner, its behavior remains the same as prior to this commit. When on_drop is called on a Spinner, a new Spinner instance is returned. If this instance is dropped without any of its stop* methods having been called, then the StopBehavior set in the on_drop call is triggered.

This change allows the following simple construction:

{
   let sp = Spinner::new(...).on_drop(StopBehavior::Message("Failed!".into()));
   do_something_fallible()?;
   sp.stop_with_message("Yay, it worked".into());
}

If do_something_fallible() returns a Result::Ok the Spinner is stopped with the "success" message. Conversely, if do_something_fallible() returns a Result::Err, the Spinner is stopped with the "Failed!" message when it is dropped.

All feedback is very welcome!

rustn00b avatar Jul 16 '22 14:07 rustn00b