Skip to main content

AnsiCodeTracker

Struct AnsiCodeTracker 

Source
pub struct AnsiCodeTracker {
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub fg_color: Option<String>,
    pub bg_color: Option<String>,
    pub hyperlink: Option<ActiveHyperlink>,
}
Expand description

Tracks active ANSI SGR and OSC 8 state across line breaks.

When wrapping styled text, styles must be closed at the end of each physical line and reopened at the start of the next. This struct records which attributes are currently active and can emit the corresponding escape sequences.

§Example

use photon_ui::utils::AnsiCodeTracker;

let mut tracker = AnsiCodeTracker::new();
tracker.process("\x1b[1m"); // bold on
tracker.process("\x1b[31m"); // red fg
assert_eq!(tracker.current_codes(), "\x1b[1;31m");

Fields§

§bold: bool

Bold (SGR 1) is active.

§italic: bool

Italic (SGR 3) is active.

§underline: bool

Underline (SGR 4) is active.

§fg_color: Option<String>

Active foreground color SGR parameter, e.g. "31" or "38;5;240".

§bg_color: Option<String>

Active background color SGR parameter, e.g. "41" or "48;5;240".

§hyperlink: Option<ActiveHyperlink>

Active OSC 8 hyperlink, if any.

Implementations§

Source§

impl AnsiCodeTracker

Source

pub fn new() -> Self

Create a tracker with no active codes.

Source

pub fn process(&mut self, seq: &str)

Process an ANSI escape sequence, updating internal state.

Supports:

  • OSC 8 hyperlink open / close (\x1b]8;;URL\x1b\\, \x1b]8;;\x1b\\)
  • SGR codes (\x1b[…m) for bold, italic, underline, and colors
Source

pub fn current_codes(&self) -> String

Return the escape sequences needed to restore all active codes.

This is used to reopen styles at the beginning of a continuation line.

Source

pub fn line_end_reset(&self) -> String

Return the escape sequences needed to close active codes at a line end.

Unlike a full SGR reset, this only closes attributes that would bleed into padding or subsequent lines (underline and hyperlinks). The caller is responsible for emitting \x1b[0m when a full SGR reset is needed.

Source

pub fn has_active_codes(&self) -> bool

Returns true if any SGR or OSC 8 code is currently active.

Trait Implementations§

Source§

impl Clone for AnsiCodeTracker

Source§

fn clone(&self) -> AnsiCodeTracker

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AnsiCodeTracker

Source§

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

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

impl Default for AnsiCodeTracker

Source§

fn default() -> AnsiCodeTracker

Returns the “default value” for a type. Read more
Source§

impl PartialEq for AnsiCodeTracker

Source§

fn eq(&self, other: &AnsiCodeTracker) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for AnsiCodeTracker

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.