quorum_core/utility/std.rs
1//! This file includes some basic std::io functionality
2//! including outputting text as a typewriter effect and waiting for user input to continue.
3
4use crate::startup;
5use std::{
6 io::{self, Write},
7 thread,
8 time::Duration,
9};
10
11/// Waits for the user to press Enter to continue. Optionally clears the screen and reprints the banner.
12///
13/// # Arguments
14/// * `clear_screen` - If true, clears the terminal screen after pressing Enter.
15/// * `reprint_banner` - If true, reprints the banner after clearing the screen.
16///
17/// # Example
18/// ```
19/// let clear_screen = true;
20/// let reprint_banner = true;
21/// press_enter_to_continue(clear_screen, reprint_banner);
22/// ```
23pub fn press_enter_to_continue(clear_screen: bool, reprint_banner: bool) {
24 use std::io::{self, Write};
25 print!("Press Enter to continue...");
26 io::stdout().flush().unwrap();
27 let mut input = String::new();
28 io::stdin().read_line(&mut input).unwrap();
29
30 if clear_screen {
31 print!("\x1B[2J\x1B[1;1H");
32 }
33
34 if reprint_banner {
35 startup::print_banner();
36 }
37}
38
39/// Prints the given text to the console with a typewriter effect, where each character is printed with a delay.
40///
41/// # Arguments
42/// * `text` - The text to be printed with the typewriter effect.
43///
44/// # Returns
45/// * `io::Result<()>` - Returns an `io::Result` indicating success or failure of the printing operation.
46///
47/// # Example
48/// ```
49/// typewriter_println(&format!("{}","Hello, World!"))
50/// ```
51pub fn typewriter_println(text: &str) -> io::Result<()> {
52 let mut stdout = io::stdout();
53 let delay_ms: u64 = 35;
54
55 for ch in text.chars() {
56 print!("{ch}");
57 stdout.flush()?;
58 thread::sleep(Duration::from_millis(delay_ms));
59 }
60
61 println!();
62 Ok(())
63}