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

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

Examples found in repository?
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        ..Default::default()
10    });
11
12    // The handles for these tasks are immediately dropped, so we automatically remove them.
13    // Except for error/info, which we display for a little while before removing automatically.
14    {
15        {
16            let task_id = state.add_task("finished task", Status::Finished);
17            state.add_subtask(&task_id, Status::Started);
18        }
19        let _ = state.add_task("error task", Status::Error);
20        let _ = state.add_task("started task", Status::Started);
21        let _ = state.add_task("queued task", Status::Queued);
22    }
23
24    let mut tasks = (0..10)
25        .into_iter()
26        .map(|index| {
27            let state = state.clone();
28            std::thread::spawn(move || {
29                std::thread::sleep(Duration::from_secs(index));
30                let task_id = state.add_task(format!("Task {index}"), Status::Started);
31                std::thread::sleep(Duration::from_secs(index.min(3)));
32                let sub_tasks = (0..10)
33                    .into_iter()
34                    .map(|_| state.add_subtask(&task_id, Status::Started))
35                    .collect::<Vec<_>>();
36
37                for sub_task_id in sub_tasks {
38                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
39                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
40                }
41
42                state.update_task(&task_id, Status::Finished);
43            })
44        })
45        .collect::<Vec<_>>();
46
47    tasks.push(std::thread::spawn({
48        let state = state.clone();
49        move || {
50            let task_id = state.add_task(format!("Task with no children"), Status::Started);
51            std::thread::sleep(Duration::from_secs(10));
52            state.update_task(&task_id, Status::Finished);
53        }
54    }));
55
56    tasks.push(std::thread::spawn(move || {
57        std::thread::sleep(Duration::from_secs(4));
58        state.info("Here's an informational message");
59        std::thread::sleep(Duration::from_secs(8));
60        state.info("Here's another one :)");
61    }));
62
63    for task in tasks {
64        task.join().unwrap();
65    }
66}
Source

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

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

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.