processes/
processes.rs

1use {
2    process_terminal::{
3        KeyCode, MessageSettings, ProcessSettings, ScrollSettings, add_process,
4        block_search_message, end_terminal, tprintln, utils::create_printing_process,
5    },
6    std::{thread::sleep, time::Duration},
7};
8
9fn main() {
10    // Create a process that prints messages and sleeps.
11    let process_foo = create_printing_process(["hello", "world", "foo", "bar"], 1.0, 30);
12
13    // Add the process to the terminal.
14    // The first time `add_process` or `tprintln!` is called, the terminal is automatically initialized.
15    add_process(
16        "Foo",
17        process_foo,
18        ProcessSettings::new_with_scroll(
19            // Show only the output messages.
20            MessageSettings::Output,
21            // Enable scrolling with the Left and Right keys.
22            // Up and Down keys are reserved for `Main` output.
23            ScrollSettings::enable(KeyCode::Left, KeyCode::Right),
24        ),
25    )
26    .unwrap();
27
28    sleep(Duration::from_secs(2));
29
30    // Create another process
31    let process_bar = create_printing_process(
32        ["hello", "Err: this is an error! >&2", "foo", "bar"],
33        0.1,
34        8,
35    );
36
37    add_process(
38        "Bar",
39        process_bar,
40        // Show both output and error messages.
41        ProcessSettings::new(MessageSettings::All),
42    )
43    .unwrap();
44
45    sleep(Duration::from_secs(2));
46
47    // Similar to print! and println!, but it prints messages in the `Main` section of the terminal.
48    // I don't find a way to capture the stdout (like println!) and display them into `Main` section.
49    tprintln!("searching_message");
50    // Block the current thread until the message is found.
51    let msg = block_search_message("Foo", "llo").unwrap();
52    tprintln!("msg found: {}", msg);
53    assert_eq!(msg, "hello");
54
55    tprintln!("searching_message");
56    let msg = block_search_message("Bar", "ar").unwrap();
57    tprintln!("msg found: {}", msg);
58    assert_eq!(msg, "bar");
59
60    sleep(Duration::from_secs(20));
61
62    // Restore the terminal to its original state.
63    end_terminal();
64}
65