hello_world/
hello_world.rs

1use rustui::*;
2use std::{thread, time};
3
4const RENDERING_RATE: time::Duration = time::Duration::from_millis(16); // ms
5const INPUT_CAPTURING_RATE: time::Duration = time::Duration::from_millis(10); // ms
6
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let mut win = Window::new(false)?;
9    win.initialize(RENDERING_RATE)?; // Initialize the window and start the rendering thread
10    let input_rx = InputListener::new(INPUT_CAPTURING_RATE); // Create an input listener
11
12    loop {
13        // Check for key presses
14        if let Ok(InputEvent::Key(Key::Char('q'))) = input_rx.try_recv() {
15            break; // Exit the loop if 'q' is pressed
16        }
17
18        // Draw the frame
19        win.draw(|canvas| {
20            canvas.set_named_border(
21                "HELLO WORLD",
22                Align::Right,
23                Attr::NORMAL,
24                Color::White,
25                Color::default(),
26            ); // Set a named border for the canvas
27            canvas.set_str(
28                canvas.width / 2, // Center the text horizontally
29                canvas.height / 2,
30                "Hello, world! (Press 'q' to quit)",
31                Attr::NORMAL,     // Set text decoration
32                Color::Green,     // Set text color
33                Color::default(), // Set background color
34                Align::Center,    // Set text alignment to center
35            );
36        })?;
37
38        thread::sleep(time::Duration::from_millis(100)); // Sleep to prevent high CPU usage
39    }
40    Ok(())
41}