system_pause/lib.rs
1/// Macro: `pause!`
2///
3/// Pauses the program execution and prompts the user with a default message
4/// or a custom message provided as an argument. The program will wait until
5/// the user presses the Enter key.
6///
7/// # Usage
8/// - `pause!();` - Displays the default message: "Press Enter to continue...".
9/// - `pause!("Your custom message");` - Displays a custom message.
10///
11/// # Arguments
12/// * Optional: `$msg` - The message to display before pausing.
13///
14/// # Example
15/// ```norust
16/// pause!(); // Default message
17/// pause!("Custom message..."); // Custom message
18/// ```
19#[macro_export]
20macro_rules! pause {
21 () => {
22 {
23 pause!("Press Enter to continue..."); // Calls the `pause_with_message!` macro with a default message.
24 }
25 };
26
27 ($msg:expr) => {
28 {
29 println!($msg); // Prints the custom pause message.
30 let mut input = String::new();
31 std::io::stdin().read_line(&mut input).unwrap(); // Waits for user input (Enter key) to continue.
32 }
33 };
34}
35
36/// Macro: `pause_with_message!`
37///
38/// Pauses the program execution and displays a custom message to the user.
39/// The program will wait until the user presses the Enter key.
40///
41/// # Arguments
42/// * `$msg`: The custom message to display before pausing.
43///
44/// # Deprecated
45/// This macro has been deprecated since version `0.1.1`. Use `pause!` instead.
46///
47/// # Example
48/// ```norust
49/// pause_with_message!("Custom pause message...");
50/// ```
51#[deprecated(since = "0.1.1", note = "Use `pause!` instead.")]
52#[macro_export]
53macro_rules! pause_with_message {
54 ($msg:expr) => {
55 {
56 println!($msg); // Prints the custom pause message.
57 let mut input = String::new();
58 std::io::stdin().read_line(&mut input).unwrap(); // Waits for user input (Enter key) to continue.
59 }
60 };
61}
62
63/// Macro: `pause_for_time!`
64///
65/// Pauses the program execution for a specified number of seconds and displays a countdown.
66/// During the pause, the remaining time is updated on the same console line.
67///
68/// # Usage
69/// - `pause_for_time!(seconds);` - Displays a countdown with the default message.
70/// - `pause_for_time!(seconds, "Custom message {}s");` - Displays a countdown with a custom message.
71///
72/// # Arguments
73/// * `$seconds`: The total number of seconds to wait.
74/// * Optional: `$message`: A custom message to display with a placeholder for the countdown.
75///
76/// # Environment Variables
77/// The behavior can be influenced by the `SYSTEM_PAUSE` environment variable:
78/// - `CLEAR_TIMER_LINE=true`: Clears the countdown line after completion.
79///
80/// # Example
81/// ```norust
82/// pause_for_time!(5); // Pauses for 5 seconds with a default message.
83/// pause_for_time!(5, "Custom pause for {} seconds"); // Custom message.
84/// ```
85#[macro_export]
86macro_rules! pause_for_time {
87 // Pattern for just seconds parameter
88 ($seconds:expr) => {
89 pause_for_time!($seconds, "Wait {}s to continue...") // Default message
90 };
91
92 // Pattern for both seconds and message parameters
93 ($seconds:expr, $message:expr) => {{
94 use std::io::Write;
95 let message_length: usize = $message.len();
96 let mut time_remaining = $seconds;
97 let mut clear_line = false;
98 while time_remaining >= 0 {
99 print!(
100 "\r{}\r{}",
101 " ".repeat(message_length + 5),
102 format!($message, time_remaining)
103 );
104 std::io::stdout().flush().unwrap();
105 std::thread::sleep(std::time::Duration::from_secs(1));
106 time_remaining -= 1;
107 }
108
109 let system_pause_options = std::env::var("SYSTEM_PAUSE");
110 if let Ok(options_list) = system_pause_options {
111 let options = options_list.split(',');
112 for option in options {
113 let key_value: Vec<&str> = option.split('=').collect();
114 let key = key_value[0];
115 let value = key_value[1];
116 if key == "CLEAR_TIMER_LINE" && value == "true" {
117 clear_line = true;
118 }
119 }
120 }
121 if clear_line {
122 print!("\r{}\r", " ".repeat(message_length + 5));
123 std::io::stdout().flush().unwrap();
124 } else {
125 println!();
126 }
127 }};
128}