Skip to main content

demo/
demo.rs

1//! # Example Usage: `cirious_codex_term`
2//!
3//! This example demonstrates how to initialize the library, manipulate the cursor,
4//! clear the screen, and apply styles using the fluid, performant builder API.
5//!
6//! ## Requirements
7//! Ensure your `Cargo.toml` is configured with the necessary dependencies.
8
9use cirious_codex_term::control::{traits::StyleExt, Cursor};
10use std::io::{stdout, Write};
11
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13  // 1. Terminal Initialization
14  // Mandatory for enabling ANSI support on Windows.
15  // On Linux/macOS, this is a no-op (native support).
16  cirious_codex_term::init_term();
17
18  // 2. Fluid Styling
19  // The `StyleExt` trait allows chaining styles directly onto any type that
20  // implements `Display`. This avoids unnecessary allocations and improves
21  // code readability.
22  println!("{}", "Hello, Codex Term!".cyan().bold());
23
24  // 3. Cursor Manipulation
25  // We use a handle to `stdout` to ensure escape sequences are written
26  // directly to the system buffer.
27  let mut handle = stdout();
28
29  Cursor::down(&mut handle, 2)?;
30  Cursor::right(&mut handle, 5)?;
31
32  // 4. Explicitly write and flush to ensure the text appears at the
33  // exact position before further operations.
34  write!(handle, "I am here!")?;
35  handle.flush()?;
36
37  // 5. Cursor Visibility
38  // Hiding the cursor is essential for TUI (Terminal User Interface)
39  // applications to prevent flickering and visual clutter.
40  Cursor::hide(&mut handle)?;
41  println!("\nCursor hidden for 1 second...");
42  std::thread::sleep(std::time::Duration::from_secs(1));
43
44  Cursor::show(&mut handle)?;
45  println!("Cursor restored.");
46
47  Ok(())
48}