pub struct StyledText<T> {
pub text: T,
pub fg: Option<Color>,
pub bg: Option<Color>,
pub style_mask: u8,
}Expand description
A deferred evaluation builder for ANSI terminal styles.
Instead of allocating strings immediately, StyledText stores the
requested styles and injects the ANSI escape codes directly into the
buffer when std::fmt::Display is invoked. This ensures maximum
performance and prevents duplicate reset codes.
Fields§
§text: TThe underlying text or data to be styled.
fg: Option<Color>The foreground color to be applied, if any.
bg: Option<Color>The background color to be applied, if any.
style_mask: u8A list of text formatting styles to be applied.
Implementations§
Source§impl<T> StyledText<T>
impl<T> StyledText<T>
Sourcepub const fn new(text: T) -> Self
pub const fn new(text: T) -> Self
Creates a new StyledText instance wrapping the provided text.
Sourcepub const fn color(self, color: Color) -> Self
pub const fn color(self, color: Color) -> Self
Sets the foreground color to the specified Color.
Sourcepub const fn fixed(self, n: u8) -> Self
pub const fn fixed(self, n: u8) -> Self
Sets the foreground color using an 8-bit (256-color) palette index.
Sourcepub const fn rgb(self, r: u8, g: u8, b: u8) -> Self
pub const fn rgb(self, r: u8, g: u8, b: u8) -> Self
Sets the foreground color using an RGB TrueColor value.
Sourcepub const fn bg_fixed(self, n: u8) -> Self
pub const fn bg_fixed(self, n: u8) -> Self
Sets the background color using an 8-bit (256-color) palette index.
Sourcepub const fn bg_rgb(self, r: u8, g: u8, b: u8) -> Self
pub const fn bg_rgb(self, r: u8, g: u8, b: u8) -> Self
Sets the background color using an RGB TrueColor value.
Sourcepub const fn bright_black(self) -> Self
pub const fn bright_black(self) -> Self
Sets the foreground color to bright black (gray).
Sourcepub const fn bright_red(self) -> Self
pub const fn bright_red(self) -> Self
Sets the foreground color to bright red.
Sourcepub const fn bright_green(self) -> Self
pub const fn bright_green(self) -> Self
Sets the foreground color to bright green.
Sourcepub const fn bright_yellow(self) -> Self
pub const fn bright_yellow(self) -> Self
Sets the foreground color to bright yellow.
Sourcepub const fn bright_blue(self) -> Self
pub const fn bright_blue(self) -> Self
Sets the foreground color to bright blue.
Sourcepub const fn bright_magenta(self) -> Self
pub const fn bright_magenta(self) -> Self
Sets the foreground color to bright magenta.
Sourcepub const fn bright_cyan(self) -> Self
pub const fn bright_cyan(self) -> Self
Sets the foreground color to bright cyan.
Sourcepub const fn bright_white(self) -> Self
pub const fn bright_white(self) -> Self
Sets the foreground color to bright white.
Sourcepub const fn bg_bright_black(self) -> Self
pub const fn bg_bright_black(self) -> Self
Sets the background color to bright black (gray).
Sourcepub const fn bg_bright_red(self) -> Self
pub const fn bg_bright_red(self) -> Self
Sets the background color to bright red.
Sourcepub const fn bg_bright_green(self) -> Self
pub const fn bg_bright_green(self) -> Self
Sets the background color to bright green.
Sourcepub const fn bg_bright_yellow(self) -> Self
pub const fn bg_bright_yellow(self) -> Self
Sets the background color to bright yellow.
Sourcepub const fn bg_bright_blue(self) -> Self
pub const fn bg_bright_blue(self) -> Self
Sets the background color to bright blue.
Sourcepub const fn bg_bright_magenta(self) -> Self
pub const fn bg_bright_magenta(self) -> Self
Sets the background color to bright magenta.
Sourcepub const fn bg_bright_cyan(self) -> Self
pub const fn bg_bright_cyan(self) -> Self
Sets the background color to bright cyan.
Sourcepub const fn bg_bright_white(self) -> Self
pub const fn bg_bright_white(self) -> Self
Sets the background color to bright white.
Sourcepub const fn bg_magenta(self) -> Self
pub const fn bg_magenta(self) -> Self
Sets the background color to magenta.
Hides the text completely.
Sourcepub const fn strikethrough(self) -> Self
pub const fn strikethrough(self) -> Self
Adds a strikethrough line to the text.
Sourcepub const fn bold(self) -> Self
pub const fn bold(self) -> Self
Adds the bold style to the text.
Examples found in repository?
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}