Struct Typout

Source
pub struct Typout { /* private fields */ }
Expand description

Typout represents a wrapper around the standard output of the current process (stdout).

Implementations§

Source§

impl Typout

Source

pub fn with_spinner(spinner: Spinner) -> Self

Creates a new Typout object with a configured spinner instance.

Source§

impl Typout

Source

pub fn set_spinner(&mut self, spinner: Spinner)

Sets the spinner instance.

Examples found in repository?
examples/simulate.rs (line 14)
7fn main() {
8    let now = Instant::now();
9
10    let mut spinner = Spinner::default();
11    spinner.set_frames(vec!["|", "/", "-", "\\"]);
12    spinner.set_speed(50);
13    let mut out = Typout::default();
14    out.set_spinner(spinner);
15
16    loop {
17        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
18        out.write("Tick");
19        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
20        out.flush();
21
22        out.print("Running simulation:\n");
23
24        out.pin("a", "Pin message A\n");
25        std::thread::sleep(Duration::from_secs(1));
26        out.pin("b", "Pin message B\n");
27        std::thread::sleep(Duration::from_secs(1));
28        out.pin("a", "Pin message A (updated)\n");
29        std::thread::sleep(Duration::from_secs(1));
30        out.pin("b", "Pin message B (updated)\n");
31        std::thread::sleep(Duration::from_secs(1));
32        out.unpin("a");
33        std::thread::sleep(Duration::from_secs(1));
34        out.unpin("b");
35
36        std::thread::sleep(Duration::from_secs(1));
37        out.spin("a", "Loading .\n");
38        std::thread::sleep(Duration::from_secs(1));
39        out.spin("b", format!("Progress {:.1}% ..\n", 50));
40        std::thread::sleep(Duration::from_secs(1));
41        out.spin("a", "Loading ..\n");
42        std::thread::sleep(Duration::from_secs(1));
43        out.spin("b", format!("Progress {:.1}% ..\n", 100));
44        std::thread::sleep(Duration::from_secs(1));
45        out.unpin("a");
46        std::thread::sleep(Duration::from_secs(1));
47        out.unpin("b");
48        std::thread::sleep(Duration::from_secs(1));
49    }
50}
Source

pub fn write<D>(&mut self, data: D)
where D: Into<String>,

Appends data to the output buffer.

Examples found in repository?
examples/simulate.rs (line 17)
7fn main() {
8    let now = Instant::now();
9
10    let mut spinner = Spinner::default();
11    spinner.set_frames(vec!["|", "/", "-", "\\"]);
12    spinner.set_speed(50);
13    let mut out = Typout::default();
14    out.set_spinner(spinner);
15
16    loop {
17        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
18        out.write("Tick");
19        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
20        out.flush();
21
22        out.print("Running simulation:\n");
23
24        out.pin("a", "Pin message A\n");
25        std::thread::sleep(Duration::from_secs(1));
26        out.pin("b", "Pin message B\n");
27        std::thread::sleep(Duration::from_secs(1));
28        out.pin("a", "Pin message A (updated)\n");
29        std::thread::sleep(Duration::from_secs(1));
30        out.pin("b", "Pin message B (updated)\n");
31        std::thread::sleep(Duration::from_secs(1));
32        out.unpin("a");
33        std::thread::sleep(Duration::from_secs(1));
34        out.unpin("b");
35
36        std::thread::sleep(Duration::from_secs(1));
37        out.spin("a", "Loading .\n");
38        std::thread::sleep(Duration::from_secs(1));
39        out.spin("b", format!("Progress {:.1}% ..\n", 50));
40        std::thread::sleep(Duration::from_secs(1));
41        out.spin("a", "Loading ..\n");
42        std::thread::sleep(Duration::from_secs(1));
43        out.spin("b", format!("Progress {:.1}% ..\n", 100));
44        std::thread::sleep(Duration::from_secs(1));
45        out.unpin("a");
46        std::thread::sleep(Duration::from_secs(1));
47        out.unpin("b");
48        std::thread::sleep(Duration::from_secs(1));
49    }
50}
Source

pub fn drain(&self)

Clears buffered output data.

Source

pub fn flush(&self)

Sends buffered output data to the standard output of the current process which displays the data in the terminal. The output buffer is cleared afterwards.

Examples found in repository?
examples/simulate.rs (line 20)
7fn main() {
8    let now = Instant::now();
9
10    let mut spinner = Spinner::default();
11    spinner.set_frames(vec!["|", "/", "-", "\\"]);
12    spinner.set_speed(50);
13    let mut out = Typout::default();
14    out.set_spinner(spinner);
15
16    loop {
17        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
18        out.write("Tick");
19        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
20        out.flush();
21
22        out.print("Running simulation:\n");
23
24        out.pin("a", "Pin message A\n");
25        std::thread::sleep(Duration::from_secs(1));
26        out.pin("b", "Pin message B\n");
27        std::thread::sleep(Duration::from_secs(1));
28        out.pin("a", "Pin message A (updated)\n");
29        std::thread::sleep(Duration::from_secs(1));
30        out.pin("b", "Pin message B (updated)\n");
31        std::thread::sleep(Duration::from_secs(1));
32        out.unpin("a");
33        std::thread::sleep(Duration::from_secs(1));
34        out.unpin("b");
35
36        std::thread::sleep(Duration::from_secs(1));
37        out.spin("a", "Loading .\n");
38        std::thread::sleep(Duration::from_secs(1));
39        out.spin("b", format!("Progress {:.1}% ..\n", 50));
40        std::thread::sleep(Duration::from_secs(1));
41        out.spin("a", "Loading ..\n");
42        std::thread::sleep(Duration::from_secs(1));
43        out.spin("b", format!("Progress {:.1}% ..\n", 100));
44        std::thread::sleep(Duration::from_secs(1));
45        out.unpin("a");
46        std::thread::sleep(Duration::from_secs(1));
47        out.unpin("b");
48        std::thread::sleep(Duration::from_secs(1));
49    }
50}
Source

pub fn print<D>(&mut self, data: D)
where D: Into<String>,

Appends data to the output buffer and calls the flush() method.

Examples found in repository?
examples/simulate.rs (line 22)
7fn main() {
8    let now = Instant::now();
9
10    let mut spinner = Spinner::default();
11    spinner.set_frames(vec!["|", "/", "-", "\\"]);
12    spinner.set_speed(50);
13    let mut out = Typout::default();
14    out.set_spinner(spinner);
15
16    loop {
17        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
18        out.write("Tick");
19        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
20        out.flush();
21
22        out.print("Running simulation:\n");
23
24        out.pin("a", "Pin message A\n");
25        std::thread::sleep(Duration::from_secs(1));
26        out.pin("b", "Pin message B\n");
27        std::thread::sleep(Duration::from_secs(1));
28        out.pin("a", "Pin message A (updated)\n");
29        std::thread::sleep(Duration::from_secs(1));
30        out.pin("b", "Pin message B (updated)\n");
31        std::thread::sleep(Duration::from_secs(1));
32        out.unpin("a");
33        std::thread::sleep(Duration::from_secs(1));
34        out.unpin("b");
35
36        std::thread::sleep(Duration::from_secs(1));
37        out.spin("a", "Loading .\n");
38        std::thread::sleep(Duration::from_secs(1));
39        out.spin("b", format!("Progress {:.1}% ..\n", 50));
40        std::thread::sleep(Duration::from_secs(1));
41        out.spin("a", "Loading ..\n");
42        std::thread::sleep(Duration::from_secs(1));
43        out.spin("b", format!("Progress {:.1}% ..\n", 100));
44        std::thread::sleep(Duration::from_secs(1));
45        out.unpin("a");
46        std::thread::sleep(Duration::from_secs(1));
47        out.unpin("b");
48        std::thread::sleep(Duration::from_secs(1));
49    }
50}
Source

pub fn pin<I, D>(&self, id: I, data: D)
where I: Into<String>, D: Into<String>,

Creates a new pinned message or updates an existing one. Pinned messages always stayed visible at the end of the output stream. An arbitrary number of pinned messages is allowed. Pins are uniquely identified by the provided id parameter.

Examples found in repository?
examples/simulate.rs (line 24)
7fn main() {
8    let now = Instant::now();
9
10    let mut spinner = Spinner::default();
11    spinner.set_frames(vec!["|", "/", "-", "\\"]);
12    spinner.set_speed(50);
13    let mut out = Typout::default();
14    out.set_spinner(spinner);
15
16    loop {
17        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
18        out.write("Tick");
19        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
20        out.flush();
21
22        out.print("Running simulation:\n");
23
24        out.pin("a", "Pin message A\n");
25        std::thread::sleep(Duration::from_secs(1));
26        out.pin("b", "Pin message B\n");
27        std::thread::sleep(Duration::from_secs(1));
28        out.pin("a", "Pin message A (updated)\n");
29        std::thread::sleep(Duration::from_secs(1));
30        out.pin("b", "Pin message B (updated)\n");
31        std::thread::sleep(Duration::from_secs(1));
32        out.unpin("a");
33        std::thread::sleep(Duration::from_secs(1));
34        out.unpin("b");
35
36        std::thread::sleep(Duration::from_secs(1));
37        out.spin("a", "Loading .\n");
38        std::thread::sleep(Duration::from_secs(1));
39        out.spin("b", format!("Progress {:.1}% ..\n", 50));
40        std::thread::sleep(Duration::from_secs(1));
41        out.spin("a", "Loading ..\n");
42        std::thread::sleep(Duration::from_secs(1));
43        out.spin("b", format!("Progress {:.1}% ..\n", 100));
44        std::thread::sleep(Duration::from_secs(1));
45        out.unpin("a");
46        std::thread::sleep(Duration::from_secs(1));
47        out.unpin("b");
48        std::thread::sleep(Duration::from_secs(1));
49    }
50}
Source

pub fn spin<I, D>(&mut self, id: I, data: D)
where I: Into<String>, D: Into<String>,

Creates a new animated pinned message or updates an existing one. It spawns the spinner animation thread for each new id. If the spinner with the provided id already exists, then only the message is updated.

Examples found in repository?
examples/simulate.rs (line 37)
7fn main() {
8    let now = Instant::now();
9
10    let mut spinner = Spinner::default();
11    spinner.set_frames(vec!["|", "/", "-", "\\"]);
12    spinner.set_speed(50);
13    let mut out = Typout::default();
14    out.set_spinner(spinner);
15
16    loop {
17        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
18        out.write("Tick");
19        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
20        out.flush();
21
22        out.print("Running simulation:\n");
23
24        out.pin("a", "Pin message A\n");
25        std::thread::sleep(Duration::from_secs(1));
26        out.pin("b", "Pin message B\n");
27        std::thread::sleep(Duration::from_secs(1));
28        out.pin("a", "Pin message A (updated)\n");
29        std::thread::sleep(Duration::from_secs(1));
30        out.pin("b", "Pin message B (updated)\n");
31        std::thread::sleep(Duration::from_secs(1));
32        out.unpin("a");
33        std::thread::sleep(Duration::from_secs(1));
34        out.unpin("b");
35
36        std::thread::sleep(Duration::from_secs(1));
37        out.spin("a", "Loading .\n");
38        std::thread::sleep(Duration::from_secs(1));
39        out.spin("b", format!("Progress {:.1}% ..\n", 50));
40        std::thread::sleep(Duration::from_secs(1));
41        out.spin("a", "Loading ..\n");
42        std::thread::sleep(Duration::from_secs(1));
43        out.spin("b", format!("Progress {:.1}% ..\n", 100));
44        std::thread::sleep(Duration::from_secs(1));
45        out.unpin("a");
46        std::thread::sleep(Duration::from_secs(1));
47        out.unpin("b");
48        std::thread::sleep(Duration::from_secs(1));
49    }
50}
Source

pub fn unpin<I>(&mut self, id: I)
where I: Into<String>,

Removes a pinned message with the provided id. This method works for all pinned messages including animated spinners.

Examples found in repository?
examples/simulate.rs (line 32)
7fn main() {
8    let now = Instant::now();
9
10    let mut spinner = Spinner::default();
11    spinner.set_frames(vec!["|", "/", "-", "\\"]);
12    spinner.set_speed(50);
13    let mut out = Typout::default();
14    out.set_spinner(spinner);
15
16    loop {
17        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
18        out.write("Tick");
19        out.write(&format!(" started {:?} seconds ago\n", now.elapsed().as_secs()));
20        out.flush();
21
22        out.print("Running simulation:\n");
23
24        out.pin("a", "Pin message A\n");
25        std::thread::sleep(Duration::from_secs(1));
26        out.pin("b", "Pin message B\n");
27        std::thread::sleep(Duration::from_secs(1));
28        out.pin("a", "Pin message A (updated)\n");
29        std::thread::sleep(Duration::from_secs(1));
30        out.pin("b", "Pin message B (updated)\n");
31        std::thread::sleep(Duration::from_secs(1));
32        out.unpin("a");
33        std::thread::sleep(Duration::from_secs(1));
34        out.unpin("b");
35
36        std::thread::sleep(Duration::from_secs(1));
37        out.spin("a", "Loading .\n");
38        std::thread::sleep(Duration::from_secs(1));
39        out.spin("b", format!("Progress {:.1}% ..\n", 50));
40        std::thread::sleep(Duration::from_secs(1));
41        out.spin("a", "Loading ..\n");
42        std::thread::sleep(Duration::from_secs(1));
43        out.spin("b", format!("Progress {:.1}% ..\n", 100));
44        std::thread::sleep(Duration::from_secs(1));
45        out.unpin("a");
46        std::thread::sleep(Duration::from_secs(1));
47        out.unpin("b");
48        std::thread::sleep(Duration::from_secs(1));
49    }
50}

Trait Implementations§

Source§

impl Clone for Typout

Source§

fn clone(&self) -> Typout

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 Typout

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Typout

When the terminal starts, the output handler is spawned in a new thread. The messages and all other intents are sent the output through a channel.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Typout

For the application to gracefully exits, we make sure that all the messages are flushed and spawned threads exit before the object is dropped.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Typout

§

impl RefUnwindSafe for Typout

§

impl Send for Typout

§

impl Sync for Typout

§

impl Unpin for Typout

§

impl UnwindSafe for Typout

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.