Skip to content

Conversation

@rustn00b
Copy link

@rustn00b rustn00b commented Jul 16, 2022

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!

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!");
   do_something_fallible()?;
   sp.stop_with_message("Yay, it worked);
}
```

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant