Skip to main content

StyledText

Struct StyledText 

Source
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: T

The 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: u8

A list of text formatting styles to be applied.

Implementations§

Source§

impl<T> StyledText<T>

Source

pub const fn new(text: T) -> Self

Creates a new StyledText instance wrapping the provided text.

Source

pub const fn color(self, color: Color) -> Self

Sets the foreground color to the specified Color.

Source

pub const fn bg(self, color: Color) -> Self

Sets the background color to the specified Color.

Source

pub const fn fixed(self, n: u8) -> Self

Sets the foreground color using an 8-bit (256-color) palette index.

Source

pub const fn rgb(self, r: u8, g: u8, b: u8) -> Self

Sets the foreground color using an RGB TrueColor value.

Source

pub const fn bg_fixed(self, n: u8) -> Self

Sets the background color using an 8-bit (256-color) palette index.

Source

pub const fn bg_rgb(self, r: u8, g: u8, b: u8) -> Self

Sets the background color using an RGB TrueColor value.

Source

pub const fn reset(self) -> Self

Clears all applied styles, foreground, and background colors.

Source

pub const fn black(self) -> Self

Sets the foreground color to black.

Source

pub const fn bright_black(self) -> Self

Sets the foreground color to bright black (gray).

Source

pub const fn bright_red(self) -> Self

Sets the foreground color to bright red.

Source

pub const fn bright_green(self) -> Self

Sets the foreground color to bright green.

Source

pub const fn bright_yellow(self) -> Self

Sets the foreground color to bright yellow.

Source

pub const fn bright_blue(self) -> Self

Sets the foreground color to bright blue.

Source

pub const fn bright_magenta(self) -> Self

Sets the foreground color to bright magenta.

Source

pub const fn bright_cyan(self) -> Self

Sets the foreground color to bright cyan.

Source

pub const fn bright_white(self) -> Self

Sets the foreground color to bright white.

Source

pub const fn red(self) -> Self

Sets the foreground color to red.

Source

pub const fn green(self) -> Self

Sets the foreground color to green.

Source

pub const fn yellow(self) -> Self

Sets the foreground color to yellow.

Source

pub const fn blue(self) -> Self

Sets the foreground color to blue.

Source

pub const fn magenta(self) -> Self

Sets the foreground color to magenta.

Source

pub const fn cyan(self) -> Self

Sets the foreground color to cyan.

Source

pub const fn white(self) -> Self

Sets the foreground color to white.

Source

pub const fn bg_black(self) -> Self

Sets the background color to black.

Source

pub const fn bg_bright_black(self) -> Self

Sets the background color to bright black (gray).

Source

pub const fn bg_bright_red(self) -> Self

Sets the background color to bright red.

Source

pub const fn bg_bright_green(self) -> Self

Sets the background color to bright green.

Source

pub const fn bg_bright_yellow(self) -> Self

Sets the background color to bright yellow.

Source

pub const fn bg_bright_blue(self) -> Self

Sets the background color to bright blue.

Source

pub const fn bg_bright_magenta(self) -> Self

Sets the background color to bright magenta.

Source

pub const fn bg_bright_cyan(self) -> Self

Sets the background color to bright cyan.

Source

pub const fn bg_bright_white(self) -> Self

Sets the background color to bright white.

Source

pub const fn bg_red(self) -> Self

Sets the background color to red.

Source

pub const fn bg_green(self) -> Self

Sets the background color to green.

Source

pub const fn bg_yellow(self) -> Self

Sets the background color to yellow.

Source

pub const fn bg_blue(self) -> Self

Sets the background color to blue.

Source

pub const fn bg_magenta(self) -> Self

Sets the background color to magenta.

Source

pub const fn bg_cyan(self) -> Self

Sets the background color to cyan.

Source

pub const fn bg_white(self) -> Self

Sets the background color to white.

Source

pub const fn underline(self) -> Self

Adds the underline style to the text.

Source

pub const fn dim(self) -> Self

Decreases the intensity of the text color.

Source

pub const fn italic(self) -> Self

Adds the italic style to the text.

Adds the blink style to the text.

Source

pub const fn reverse(self) -> Self

Reverses the foreground and background colors.

Source

pub const fn hidden(self) -> Self

Hides the text completely.

Source

pub const fn strikethrough(self) -> Self

Adds a strikethrough line to the text.

Source

pub const fn bold(self) -> Self

Adds the bold style to the text.

Examples found in repository?
examples/demo.rs (line 22)
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}

Trait Implementations§

Source§

impl<T: Debug> Debug for StyledText<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Display> Display for StyledText<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for StyledText<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for StyledText<T>
where T: RefUnwindSafe,

§

impl<T> Send for StyledText<T>
where T: Send,

§

impl<T> Sync for StyledText<T>
where T: Sync,

§

impl<T> Unpin for StyledText<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for StyledText<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for StyledText<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> StyleExt for T
where T: Display,

Source§

fn color(self, color: Color) -> StyledText<T>

Applies a foreground color to the text.
Source§

fn bg(self, color: Color) -> StyledText<T>

Applies a background color to the text.
Source§

fn black(self) -> StyledText<T>

Formats the text with a black foreground. Read more
Source§

fn red(self) -> StyledText<T>

Formats the text with a red foreground. Read more
Source§

fn green(self) -> StyledText<T>

Formats the text with a green foreground. Read more
Source§

fn yellow(self) -> StyledText<T>

Formats the text with a yellow foreground. Read more
Source§

fn blue(self) -> StyledText<T>

Formats the text with a blue foreground. Read more
Source§

fn magenta(self) -> StyledText<T>

Formats the text with a magenta foreground. Read more
Source§

fn cyan(self) -> StyledText<T>

Formats the text with a cyan foreground. Read more
Source§

fn white(self) -> StyledText<T>

Formats the text with a white foreground. Read more
Source§

fn rgb(self, r: u8, g: u8, b: u8) -> StyledText<T>

Formats the text with a red foreground. Read more
Source§

fn bold(self) -> StyledText<T>

Formats the text with a bold style. Read more
Source§

fn italic(self) -> StyledText<T>

Formats the text with a italic style. Read more
Source§

fn underline(self) -> StyledText<T>

Formats the text with a underline style. Read more
Formats the text with a blink style. Read more
Source§

fn strikethrough(self) -> StyledText<T>

Formats the text with a strikethrough style. Read more
Source§

fn dim(self) -> StyledText<T>

Formats the text with a dim style. Read more
Source§

fn reset(self) -> StyledText<T>

Resets the text styles. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.