quorum_core/startup.rs
1//! Server startup printing initiliation
2
3//! Provides public functions to be used for neat and pretty output during server startup.
4//! Including the ASCII banner, step-by-step progress logging, and timing information.
5
6use colored::*;
7use std::thread;
8use std::time::{Duration, Instant};
9
10/// Display the ASCII art banner
11///
12/// Prints a centered, coloured ASCII art banner.
13/// The banner is used at the very start of initializing the server to give it a unique and identifiable look in the terminal.
14/// The ASCII art is generated from https://patorjk.com/software/taag/#p=display
15///
16/// # Example
17/// ```rust
18/// print_banner();
19/// ```
20pub fn print_banner() {
21 println!("{}", "═".repeat(75).cyan());
22 //ASCII art generated with https://patorjk.com/software/taag/#p=display
23 println!(
24 r#"
25 ██████
26 ███░░░░███
27 ███ ░░███ █████ ████ ██████ ████████ █████ ████ █████████████
28 ░███ ░███░░███ ░███ ███░░███░░███░░███░░███ ░███ ░░███░░███░░███
29 ░███ ██░███ ░███ ░███ ░███ ░███ ░███ ░░░ ░███ ░███ ░███ ░███ ░███
30 ░░███ ░░████ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███
31 ░░░██████░██ ░░████████░░██████ █████ ░░████████ █████░███ █████
32 ░░░░░░ ░░ ░░░░░░░░ ░░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░
33 "#
34 );
35 println!("{}", "═".repeat(75).cyan());
36
37 if rand::random::<u8>().is_multiple_of(100) {
38 println!();
39 println!("{}", "Greetings, Professor Falken.".cyan().bold());
40 println!();
41 }
42}
43
44/// Display the "Initializing..." startup message
45///
46/// Displays the text in bold yellow text waiting 300ms before continuing.
47///
48/// # Example
49/// ```rust
50/// print_initializing();
51/// ```
52pub fn print_initializing() {
53 println!("\n{}", "Initializing...".yellow().bold());
54 thread::sleep(Duration::from_millis(300));
55}
56
57/// Display a single initialization step with status and timing
58///
59/// Prints a formatted line showing a step name, success/failure status,
60/// and elapsed tim in milliseconds.
61///
62/// # Arguments
63/// * `step` - A string describing the initialization step being performed.
64/// * `success` - A boolean indicating whether the step succeeded (true) or failed (false).
65/// * `duration_ms` - The time taken to complete the step, in milliseconds.
66///
67/// # Example
68/// ```rust
69/// let timer = create_timer();
70/// print_step("Initializing database", true, elapsed_ms(timer));
71/// ```
72pub fn print_step(step: &str, success: bool, duration: Duration) {
73 let status = if success { "✓".green() } else { "✗".red() };
74 let timing = format!("({:?})", duration).dimmed();
75 println!("{}{} {} {}", " ├─ ".blue(), step.white(), status, timing);
76 thread::sleep(Duration::from_millis(200));
77}
78
79/// Display the final initialization step with status and timing
80///
81/// Similar to `print_step` but uses a different end tree character.
82///
83/// # Arguments
84/// * `step` - A string describing the final initialization step being performed.
85/// * `success` - A boolean indicating whether the step succeeded (true) or failed (false).
86/// * `duration_ms` - The time taken to complete the step, in milliseconds.
87///
88/// # Example
89/// ```rust
90/// let timer = create_timer();
91/// print_final_step("Starting server", true, elapsed_ms(timer));
92/// ```
93pub fn print_final_step(step: &str, success: bool, duration: Duration) {
94 let status = if success { "✓".green() } else { "✗".red() };
95 let timing = format!("({:?})", duration).dimmed();
96 println!("{}{} {} {}", " └─ ".blue(), step.white(), status, timing);
97 thread::sleep(Duration::from_millis(200));
98}
99
100/// Display the final "Server ready" message with the server URL
101///
102/// Prints a formatted message indicating that the server is ready, including the URL and port number.
103/// The URL is displayed in green and bold for emphasis, and a note about stopping the server is shown in dimmed text.
104///
105/// # Arguments
106/// * `port` - The port number on which the server is running.
107///
108/// # Example
109/// ```rust
110/// print_ready(8080);
111/// ```
112pub fn print_ready(port: u16) {
113 println!(
114 "\n{}",
115 format!("Server ready at http://127.0.0.1:{}", port)
116 .green()
117 .bold()
118 );
119 println!(
120 "{}",
121 "Use command \"server:login\" to get authentication to restricted commands.".dimmed()
122 );
123}
124
125/// Create a timer for measuring elapsed time
126///
127/// # Returns
128/// An `Instant` representing the current time, which can be used to measure elapsed time for initialization steps.
129///
130/// # Example
131/// ```rust
132/// let timer = create_timer();
133/// let elapsed = elapsed_ms(timer);
134/// println!("Step completed in {} ms", elapsed);
135/// ```
136pub fn create_timer() -> Instant {
137 Instant::now()
138}
139
140/// Calculate elapsed time as a `Duration` from a given timer
141///
142/// # Arguments
143/// * `timer` - An `Instant` representing the start time of an operation.
144///
145/// # Returns
146/// The elapsed time as a `Duration`.
147///
148/// # Example
149/// ```rust
150/// let timer = create_timer();
151/// let elapsed = elapsed(timer);
152/// println!("Operation completed in {:?}", elapsed);
153/// ```
154pub fn elapsed(timer: Instant) -> Duration {
155 timer.elapsed()
156}