Struct kdam::Spinner

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

Generic spinner for rendering spinner animations.

See more styles at rich repository.

Implementations§

source§

impl Spinner

source

pub fn new(frames: &[&str], interval: f32, speed: f32) -> Self

Create a new Spinner.

Example
use kdam::Spinner;

let spinner = Spinner::new(
    &[
        "▁▂▃", "▂▃▄", "▃▄▅", "▄▅▆", "▅▆▇",
        "▆▇█", "▇█▇", "█▇▆", "▇▆▅", "▆▅▄",
        "▅▄▃", "▄▃▂", "▃▂▁"
    ],
    30.0,
    1.0
);

println!("{}", spinner.render_frame(2.89));
Examples found in repository?
examples/showcase/spinner.rs (lines 8-26)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
fn main() {
    let spin = Spinner::new(
        &[
            "▁▂▃",
            "▂▃▄",
            "▃▄▅",
            "▄▅▆",
            "▅▆▇",
            "▆▇█",
            "▇█▇",
            "█▇▆",
            "▇▆▅",
            "▆▅▄",
            "▅▄▃",
            "▄▃▂",
            "▃▂▁",
        ],
        30.0,
        1.0,
    );

    let timer = Instant::now();

    loop {
        std::thread::sleep(Duration::from_secs_f32(0.02));
        Writer::Stderr
            .print_at(
                0,
                spin.render_frames(
                    timer.elapsed().as_secs_f32(),
                    NonZeroI16::new((term::width().unwrap_or(30) / 3) as i16).unwrap(),
                )
                .as_bytes(),
            )
            .unwrap();
    }
}
More examples
Hide additional examples
examples/template.rs (lines 10-14)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() -> Result<()> {
    let mut pb = tqdm!(
        total = 300,
        ncols = 40,
        force_refresh = true,
        bar_format = "{desc suffix=' '}|{animation}| {spinner} {count}/{total} [{percentage:.0}%] in {elapsed human=true} ({rate:.1}/s, eta: {remaining human=true})",
        spinner = Spinner::new(
            &["▁▂▃", "▂▃▄", "▃▄▅", "▄▅▆", "▅▆▇", "▆▇█", "▇█▇", "█▇▆", "▇▆▅", "▆▅▄", "▅▄▃", "▄▃▂", "▃▂▁"],
            30.0,
            1.0,
        )
    );

    for _ in 0..300 {
        std::thread::sleep(std::time::Duration::from_secs_f32(0.02));
        pb.update(1)?;
    }

    pb.set_bar_format("{desc suffix=' '}|{animation}| {count}/{total} [{percentage:.0}%] in {elapsed human=true} ({rate:.1}/s)").unwrap();
    pb.clear()?;
    pb.refresh()?;
    eprintln!();

    Ok(())
}
examples/rich.rs (lines 16-20)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
fn main() -> Result<()> {
    term::init(stderr().is_terminal());
    term::hide_cursor()?;

    let mut pb = RichProgress::new(
        tqdm!(
            total = 231231231,
            unit_scale = true,
            unit_divisor = 1024,
            unit = "B"
        ),
        vec![
            Column::Spinner(Spinner::new(
                &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
                80.0,
                1.0,
            )),
            Column::Text("[bold blue]?".to_owned()),
            Column::Animation,
            Column::Percentage(1),
            Column::Text("•".to_owned()),
            Column::CountTotal,
            Column::Text("•".to_owned()),
            Column::Rate,
            Column::Text("•".to_owned()),
            Column::RemainingTime,
        ],
    );

    pb.write("download will begin in 5 seconds".colorize("bold red"))?;

    while pb.pb.elapsed_time() <= 5.0 {
        pb.refresh()?;
    }

    pb.replace(1, Column::Text("[bold blue]docker.exe".to_owned()));
    pb.write("downloading docker.exe".colorize("bold cyan"))?;

    let total_size = 231231231;
    let mut downloaded = 0;

    while downloaded < total_size {
        let new = std::cmp::min(downloaded + 223211, total_size);
        downloaded = new;
        pb.update_to(new)?;
        std::thread::sleep(std::time::Duration::from_millis(12));
    }

    pb.write("downloaded docker.exe".colorize("bold green"))?;
    eprintln!();

    Ok(())
}
source

pub fn render_frame(&self, elapsed_time: f32) -> String

Render a single frame.

source

pub fn render_frames(&self, elapsed_time: f32, ncols: NonZeroI16) -> String

Render multiple frames upto ncols with an pulsating animation.

Examples found in repository?
examples/showcase/spinner.rs (lines 35-38)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
fn main() {
    let spin = Spinner::new(
        &[
            "▁▂▃",
            "▂▃▄",
            "▃▄▅",
            "▄▅▆",
            "▅▆▇",
            "▆▇█",
            "▇█▇",
            "█▇▆",
            "▇▆▅",
            "▆▅▄",
            "▅▄▃",
            "▄▃▂",
            "▃▂▁",
        ],
        30.0,
        1.0,
    );

    let timer = Instant::now();

    loop {
        std::thread::sleep(Duration::from_secs_f32(0.02));
        Writer::Stderr
            .print_at(
                0,
                spin.render_frames(
                    timer.elapsed().as_secs_f32(),
                    NonZeroI16::new((term::width().unwrap_or(30) / 3) as i16).unwrap(),
                )
                .as_bytes(),
            )
            .unwrap();
    }
}

Trait Implementations§

source§

impl Clone for Spinner

source§

fn clone(&self) -> Spinner

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Spinner

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<T> Ungil for Twhere T: Send,