Struct SBState

Source
pub struct SBState { /* private fields */ }

Implementations§

Source§

impl SBState

Source

pub fn new(config: SBStateConfig) -> Self

Examples found in repository?
examples/minimal.rs (lines 6-9)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // When this handle drops the task completes
12    let _handle = state.add_task(format!("Some super basic task"), Status::Started);
13
14    std::thread::sleep(Duration::from_secs(10));
15}
More examples
Hide additional examples
examples/demo.rs (lines 6-11)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        task_name_width: TaskNameWidth::ExactRatio(0.25),
9        grow_if_no_progress: false,
10        ..Default::default()
11    });
12
13    // The handles for these tasks are immediately dropped, so we automatically remove them.
14    // Except for error/info, which we display for a little while before removing automatically.
15    {
16        {
17            let task_id = state.add_task("finished task", Status::Finished);
18            state.add_subtask(&task_id, Status::Started);
19        }
20        let _ = state.add_task("error task", Status::Error);
21        let _ = state.add_task("started task", Status::Started);
22        let _ = state.add_task("queued task", Status::Queued);
23    }
24
25    let mut tasks = (0..10)
26        .into_iter()
27        .map(|index| {
28            let state = state.clone();
29            std::thread::spawn(move || {
30                std::thread::sleep(Duration::from_secs(index));
31                let task_id = state.add_task(format!("Task {index}"), Status::Started);
32                std::thread::sleep(Duration::from_secs(index.min(3)));
33                let sub_tasks = (0..10)
34                    .into_iter()
35                    .map(|_| state.add_subtask(&task_id, Status::Started))
36                    .collect::<Vec<_>>();
37
38                for sub_task_id in sub_tasks {
39                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
40                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
41                }
42
43                state.update_task(&task_id, Status::Finished);
44            })
45        })
46        .collect::<Vec<_>>();
47
48    tasks.push(std::thread::spawn({
49        let state = state.clone();
50        move || {
51            let task_id = state.add_task(
52                format!("Task with looooooooooooooooooooooooooooooooooooooong message. I mean, it's soooooooooooooooooooooooooooooooo loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnngggggggggggggggggg"),
53                Status::Started,
54            );
55            std::thread::sleep(Duration::from_secs(10));
56            state.update_task(&task_id, Status::Finished);
57        }
58    }));
59
60    tasks.push(std::thread::spawn({
61        let state = state.clone();
62        move || {
63            let task_id = state.add_task(format!("Task with no children"), Status::Started);
64            std::thread::sleep(Duration::from_secs(10));
65            state.update_task(&task_id, Status::Finished);
66        }
67    }));
68
69    tasks.push(std::thread::spawn(move || {
70        std::thread::sleep(Duration::from_secs(4));
71        state.info("Here's an informational message");
72        std::thread::sleep(Duration::from_secs(8));
73        state.info("Here's another one :)");
74    }));
75
76    for task in tasks {
77        task.join().unwrap();
78    }
79}
Source

pub fn error<S: ToString>(&self, display_name: S)

Source

pub fn info<S: ToString>(&self, display_name: S)

Examples found in repository?
examples/demo.rs (line 71)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        task_name_width: TaskNameWidth::ExactRatio(0.25),
9        grow_if_no_progress: false,
10        ..Default::default()
11    });
12
13    // The handles for these tasks are immediately dropped, so we automatically remove them.
14    // Except for error/info, which we display for a little while before removing automatically.
15    {
16        {
17            let task_id = state.add_task("finished task", Status::Finished);
18            state.add_subtask(&task_id, Status::Started);
19        }
20        let _ = state.add_task("error task", Status::Error);
21        let _ = state.add_task("started task", Status::Started);
22        let _ = state.add_task("queued task", Status::Queued);
23    }
24
25    let mut tasks = (0..10)
26        .into_iter()
27        .map(|index| {
28            let state = state.clone();
29            std::thread::spawn(move || {
30                std::thread::sleep(Duration::from_secs(index));
31                let task_id = state.add_task(format!("Task {index}"), Status::Started);
32                std::thread::sleep(Duration::from_secs(index.min(3)));
33                let sub_tasks = (0..10)
34                    .into_iter()
35                    .map(|_| state.add_subtask(&task_id, Status::Started))
36                    .collect::<Vec<_>>();
37
38                for sub_task_id in sub_tasks {
39                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
40                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
41                }
42
43                state.update_task(&task_id, Status::Finished);
44            })
45        })
46        .collect::<Vec<_>>();
47
48    tasks.push(std::thread::spawn({
49        let state = state.clone();
50        move || {
51            let task_id = state.add_task(
52                format!("Task with looooooooooooooooooooooooooooooooooooooong message. I mean, it's soooooooooooooooooooooooooooooooo loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnngggggggggggggggggg"),
53                Status::Started,
54            );
55            std::thread::sleep(Duration::from_secs(10));
56            state.update_task(&task_id, Status::Finished);
57        }
58    }));
59
60    tasks.push(std::thread::spawn({
61        let state = state.clone();
62        move || {
63            let task_id = state.add_task(format!("Task with no children"), Status::Started);
64            std::thread::sleep(Duration::from_secs(10));
65            state.update_task(&task_id, Status::Finished);
66        }
67    }));
68
69    tasks.push(std::thread::spawn(move || {
70        std::thread::sleep(Duration::from_secs(4));
71        state.info("Here's an informational message");
72        std::thread::sleep(Duration::from_secs(8));
73        state.info("Here's another one :)");
74    }));
75
76    for task in tasks {
77        task.join().unwrap();
78    }
79}
Source

pub fn add_task<S: ToString>(&self, display_name: S, status: Status) -> TaskId

Examples found in repository?
examples/minimal.rs (line 12)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // When this handle drops the task completes
12    let _handle = state.add_task(format!("Some super basic task"), Status::Started);
13
14    std::thread::sleep(Duration::from_secs(10));
15}
More examples
Hide additional examples
examples/demo.rs (line 17)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        task_name_width: TaskNameWidth::ExactRatio(0.25),
9        grow_if_no_progress: false,
10        ..Default::default()
11    });
12
13    // The handles for these tasks are immediately dropped, so we automatically remove them.
14    // Except for error/info, which we display for a little while before removing automatically.
15    {
16        {
17            let task_id = state.add_task("finished task", Status::Finished);
18            state.add_subtask(&task_id, Status::Started);
19        }
20        let _ = state.add_task("error task", Status::Error);
21        let _ = state.add_task("started task", Status::Started);
22        let _ = state.add_task("queued task", Status::Queued);
23    }
24
25    let mut tasks = (0..10)
26        .into_iter()
27        .map(|index| {
28            let state = state.clone();
29            std::thread::spawn(move || {
30                std::thread::sleep(Duration::from_secs(index));
31                let task_id = state.add_task(format!("Task {index}"), Status::Started);
32                std::thread::sleep(Duration::from_secs(index.min(3)));
33                let sub_tasks = (0..10)
34                    .into_iter()
35                    .map(|_| state.add_subtask(&task_id, Status::Started))
36                    .collect::<Vec<_>>();
37
38                for sub_task_id in sub_tasks {
39                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
40                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
41                }
42
43                state.update_task(&task_id, Status::Finished);
44            })
45        })
46        .collect::<Vec<_>>();
47
48    tasks.push(std::thread::spawn({
49        let state = state.clone();
50        move || {
51            let task_id = state.add_task(
52                format!("Task with looooooooooooooooooooooooooooooooooooooong message. I mean, it's soooooooooooooooooooooooooooooooo loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnngggggggggggggggggg"),
53                Status::Started,
54            );
55            std::thread::sleep(Duration::from_secs(10));
56            state.update_task(&task_id, Status::Finished);
57        }
58    }));
59
60    tasks.push(std::thread::spawn({
61        let state = state.clone();
62        move || {
63            let task_id = state.add_task(format!("Task with no children"), Status::Started);
64            std::thread::sleep(Duration::from_secs(10));
65            state.update_task(&task_id, Status::Finished);
66        }
67    }));
68
69    tasks.push(std::thread::spawn(move || {
70        std::thread::sleep(Duration::from_secs(4));
71        state.info("Here's an informational message");
72        std::thread::sleep(Duration::from_secs(8));
73        state.info("Here's another one :)");
74    }));
75
76    for task in tasks {
77        task.join().unwrap();
78    }
79}
Source

pub fn set_task_display_name(&self, task_id: &TaskId, display_name: String)

Source

pub fn delete_task(&self, task_id: &TaskId)

Source

pub fn update_task(&self, task_id: &TaskId, new_status: Status)

Examples found in repository?
examples/demo.rs (line 43)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        task_name_width: TaskNameWidth::ExactRatio(0.25),
9        grow_if_no_progress: false,
10        ..Default::default()
11    });
12
13    // The handles for these tasks are immediately dropped, so we automatically remove them.
14    // Except for error/info, which we display for a little while before removing automatically.
15    {
16        {
17            let task_id = state.add_task("finished task", Status::Finished);
18            state.add_subtask(&task_id, Status::Started);
19        }
20        let _ = state.add_task("error task", Status::Error);
21        let _ = state.add_task("started task", Status::Started);
22        let _ = state.add_task("queued task", Status::Queued);
23    }
24
25    let mut tasks = (0..10)
26        .into_iter()
27        .map(|index| {
28            let state = state.clone();
29            std::thread::spawn(move || {
30                std::thread::sleep(Duration::from_secs(index));
31                let task_id = state.add_task(format!("Task {index}"), Status::Started);
32                std::thread::sleep(Duration::from_secs(index.min(3)));
33                let sub_tasks = (0..10)
34                    .into_iter()
35                    .map(|_| state.add_subtask(&task_id, Status::Started))
36                    .collect::<Vec<_>>();
37
38                for sub_task_id in sub_tasks {
39                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
40                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
41                }
42
43                state.update_task(&task_id, Status::Finished);
44            })
45        })
46        .collect::<Vec<_>>();
47
48    tasks.push(std::thread::spawn({
49        let state = state.clone();
50        move || {
51            let task_id = state.add_task(
52                format!("Task with looooooooooooooooooooooooooooooooooooooong message. I mean, it's soooooooooooooooooooooooooooooooo loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnngggggggggggggggggg"),
53                Status::Started,
54            );
55            std::thread::sleep(Duration::from_secs(10));
56            state.update_task(&task_id, Status::Finished);
57        }
58    }));
59
60    tasks.push(std::thread::spawn({
61        let state = state.clone();
62        move || {
63            let task_id = state.add_task(format!("Task with no children"), Status::Started);
64            std::thread::sleep(Duration::from_secs(10));
65            state.update_task(&task_id, Status::Finished);
66        }
67    }));
68
69    tasks.push(std::thread::spawn(move || {
70        std::thread::sleep(Duration::from_secs(4));
71        state.info("Here's an informational message");
72        std::thread::sleep(Duration::from_secs(8));
73        state.info("Here's another one :)");
74    }));
75
76    for task in tasks {
77        task.join().unwrap();
78    }
79}
Source

pub fn add_subtask(&self, task_id: &TaskId, status: Status) -> TaskId

Examples found in repository?
examples/demo.rs (line 18)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        task_name_width: TaskNameWidth::ExactRatio(0.25),
9        grow_if_no_progress: false,
10        ..Default::default()
11    });
12
13    // The handles for these tasks are immediately dropped, so we automatically remove them.
14    // Except for error/info, which we display for a little while before removing automatically.
15    {
16        {
17            let task_id = state.add_task("finished task", Status::Finished);
18            state.add_subtask(&task_id, Status::Started);
19        }
20        let _ = state.add_task("error task", Status::Error);
21        let _ = state.add_task("started task", Status::Started);
22        let _ = state.add_task("queued task", Status::Queued);
23    }
24
25    let mut tasks = (0..10)
26        .into_iter()
27        .map(|index| {
28            let state = state.clone();
29            std::thread::spawn(move || {
30                std::thread::sleep(Duration::from_secs(index));
31                let task_id = state.add_task(format!("Task {index}"), Status::Started);
32                std::thread::sleep(Duration::from_secs(index.min(3)));
33                let sub_tasks = (0..10)
34                    .into_iter()
35                    .map(|_| state.add_subtask(&task_id, Status::Started))
36                    .collect::<Vec<_>>();
37
38                for sub_task_id in sub_tasks {
39                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
40                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
41                }
42
43                state.update_task(&task_id, Status::Finished);
44            })
45        })
46        .collect::<Vec<_>>();
47
48    tasks.push(std::thread::spawn({
49        let state = state.clone();
50        move || {
51            let task_id = state.add_task(
52                format!("Task with looooooooooooooooooooooooooooooooooooooong message. I mean, it's soooooooooooooooooooooooooooooooo loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnngggggggggggggggggg"),
53                Status::Started,
54            );
55            std::thread::sleep(Duration::from_secs(10));
56            state.update_task(&task_id, Status::Finished);
57        }
58    }));
59
60    tasks.push(std::thread::spawn({
61        let state = state.clone();
62        move || {
63            let task_id = state.add_task(format!("Task with no children"), Status::Started);
64            std::thread::sleep(Duration::from_secs(10));
65            state.update_task(&task_id, Status::Finished);
66        }
67    }));
68
69    tasks.push(std::thread::spawn(move || {
70        std::thread::sleep(Duration::from_secs(4));
71        state.info("Here's an informational message");
72        std::thread::sleep(Duration::from_secs(8));
73        state.info("Here's another one :)");
74    }));
75
76    for task in tasks {
77        task.join().unwrap();
78    }
79}
Source

pub fn update_subtask( &self, task_id: &TaskId, sub_task_id: &TaskId, status: Status, )

Examples found in repository?
examples/demo.rs (line 40)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        task_name_width: TaskNameWidth::ExactRatio(0.25),
9        grow_if_no_progress: false,
10        ..Default::default()
11    });
12
13    // The handles for these tasks are immediately dropped, so we automatically remove them.
14    // Except for error/info, which we display for a little while before removing automatically.
15    {
16        {
17            let task_id = state.add_task("finished task", Status::Finished);
18            state.add_subtask(&task_id, Status::Started);
19        }
20        let _ = state.add_task("error task", Status::Error);
21        let _ = state.add_task("started task", Status::Started);
22        let _ = state.add_task("queued task", Status::Queued);
23    }
24
25    let mut tasks = (0..10)
26        .into_iter()
27        .map(|index| {
28            let state = state.clone();
29            std::thread::spawn(move || {
30                std::thread::sleep(Duration::from_secs(index));
31                let task_id = state.add_task(format!("Task {index}"), Status::Started);
32                std::thread::sleep(Duration::from_secs(index.min(3)));
33                let sub_tasks = (0..10)
34                    .into_iter()
35                    .map(|_| state.add_subtask(&task_id, Status::Started))
36                    .collect::<Vec<_>>();
37
38                for sub_task_id in sub_tasks {
39                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
40                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
41                }
42
43                state.update_task(&task_id, Status::Finished);
44            })
45        })
46        .collect::<Vec<_>>();
47
48    tasks.push(std::thread::spawn({
49        let state = state.clone();
50        move || {
51            let task_id = state.add_task(
52                format!("Task with looooooooooooooooooooooooooooooooooooooong message. I mean, it's soooooooooooooooooooooooooooooooo loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnngggggggggggggggggg"),
53                Status::Started,
54            );
55            std::thread::sleep(Duration::from_secs(10));
56            state.update_task(&task_id, Status::Finished);
57        }
58    }));
59
60    tasks.push(std::thread::spawn({
61        let state = state.clone();
62        move || {
63            let task_id = state.add_task(format!("Task with no children"), Status::Started);
64            std::thread::sleep(Duration::from_secs(10));
65            state.update_task(&task_id, Status::Finished);
66        }
67    }));
68
69    tasks.push(std::thread::spawn(move || {
70        std::thread::sleep(Duration::from_secs(4));
71        state.info("Here's an informational message");
72        std::thread::sleep(Duration::from_secs(8));
73        state.info("Here's another one :)");
74    }));
75
76    for task in tasks {
77        task.join().unwrap();
78    }
79}

Trait Implementations§

Source§

impl Clone for SBState

Source§

fn clone(&self) -> SBState

Returns a duplicate 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 SBState

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 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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