Skip to main content

Spinner

Struct Spinner 

Source
pub struct Spinner { /* private fields */ }
Available on crate feature spinner only.
Expand description

A single-line terminal spinner with a live message.

Implementations§

Source§

impl Spinner

Source

pub fn start(message: impl Into<String>) -> Self

Start the spinner with an initial message.

When stderr is not a TTY no thread is spawned and ticks are suppressed; terminal completion lines (success/failure) are still printed so logs remain informative.

The first TTY-bound call also installs a process-wide SIGINT / SIGTERM / SIGHUP handler and a panic hook that restore the cursor before letting the process die: Drop is skipped on signal-induced termination and on panic = abort, so without these hooks cursor::Hide from the render loop would leak past process exit.

Examples found in repository?
examples/spinner.rs (line 12)
11fn main() {
12    let spinner = Spinner::start("Resolving MX records for example.com…");
13    sleep(Duration::from_secs(1));
14
15    spinner.set_message("Probing imap.example.com:993");
16    sleep(Duration::from_secs(1));
17
18    spinner.set_message("Probing smtp.example.com:465");
19    sleep(Duration::from_secs(1));
20
21    spinner.success("Found IMAP at imap.example.com:993 and SMTP at smtp.example.com:465");
22
23    let spinner = Spinner::start("Probing pop.example.com:995");
24    sleep(Duration::from_secs(1));
25    spinner.failure("No POP server reachable at pop.example.com:995");
26}
Source

pub fn set_message(&self, message: impl Into<String>)

Replace the message displayed next to the spinner frame.

Examples found in repository?
examples/spinner.rs (line 15)
11fn main() {
12    let spinner = Spinner::start("Resolving MX records for example.com…");
13    sleep(Duration::from_secs(1));
14
15    spinner.set_message("Probing imap.example.com:993");
16    sleep(Duration::from_secs(1));
17
18    spinner.set_message("Probing smtp.example.com:465");
19    sleep(Duration::from_secs(1));
20
21    spinner.success("Found IMAP at imap.example.com:993 and SMTP at smtp.example.com:465");
22
23    let spinner = Spinner::start("Probing pop.example.com:995");
24    sleep(Duration::from_secs(1));
25    spinner.failure("No POP server reachable at pop.example.com:995");
26}
Source

pub fn success(self, message: impl AsRef<str>)

Stop the spinner and print a green followed by message.

Examples found in repository?
examples/spinner.rs (line 21)
11fn main() {
12    let spinner = Spinner::start("Resolving MX records for example.com…");
13    sleep(Duration::from_secs(1));
14
15    spinner.set_message("Probing imap.example.com:993");
16    sleep(Duration::from_secs(1));
17
18    spinner.set_message("Probing smtp.example.com:465");
19    sleep(Duration::from_secs(1));
20
21    spinner.success("Found IMAP at imap.example.com:993 and SMTP at smtp.example.com:465");
22
23    let spinner = Spinner::start("Probing pop.example.com:995");
24    sleep(Duration::from_secs(1));
25    spinner.failure("No POP server reachable at pop.example.com:995");
26}
Source

pub fn failure(self, message: impl AsRef<str>)

Stop the spinner and print a red followed by message.

Examples found in repository?
examples/spinner.rs (line 25)
11fn main() {
12    let spinner = Spinner::start("Resolving MX records for example.com…");
13    sleep(Duration::from_secs(1));
14
15    spinner.set_message("Probing imap.example.com:993");
16    sleep(Duration::from_secs(1));
17
18    spinner.set_message("Probing smtp.example.com:465");
19    sleep(Duration::from_secs(1));
20
21    spinner.success("Found IMAP at imap.example.com:993 and SMTP at smtp.example.com:465");
22
23    let spinner = Spinner::start("Probing pop.example.com:995");
24    sleep(Duration::from_secs(1));
25    spinner.failure("No POP server reachable at pop.example.com:995");
26}
Source

pub fn clear(self)

Stop the spinner and clear its line without printing anything in its place.

Trait Implementations§

Source§

impl Drop for Spinner

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.