Skip to main content

Text

Struct Text 

Source
pub struct Text(/* private fields */);
Expand description

Sanitized, display-safe text.

Constructing a Text always goes through Text::sanitize (directly, or indirectly via Deserialize), which strips control characters and terminal escape sequences, resolves backspace-overstrike, expands tabs, collapses whitespace runs, normalizes newlines (preserving paragraph breaks), and truncates to MAX_TEXT_CHARS. Widgets and other consumers may assume a Text is safe to place directly into a rendering surface.

Implementations§

Source§

impl Text

Source

pub fn sanitize(raw: &str) -> Text

The only way to build a Text from raw, untrusted input.

Pipeline (see spec §4.1 and §13.3 for the adversarial cases this must survive):

  1. Strip ANSI/OSC/other terminal escape sequences.
  2. Resolve backspace-overstrike (_\bX, X\bX, and any stray \b).
  3. Strip remaining C0 control characters and DEL.
  4. Expand tabs to spaces at 8-column stops.
  5. Normalize line endings to \n.
  6. Unwrap hard-wrapped paragraphs: a single \n inside a paragraph joins to a space (so a later re-wrap at the pane’s actual width produces clean lines instead of re-wrapping already-short, pre-broken lines raggedly); \n\n stays a paragraph break; indented/code-like lines and list items (- , * , 1. ) are never joined to a neighbor, preserving block structure.
  7. Collapse runs of horizontal whitespace to a single space.
  8. Trim leading/trailing whitespace.
  9. Truncate to MAX_TEXT_CHARS characters, at a char boundary.
Source

pub fn sanitize_markdown(raw: &str) -> Text

Like Text::sanitize, but for text known to originate as markdown-flavored prose (carapace-spec’s description/ documentation fields, which use [label](uri) links — including custom schemes like man:// and cmd:// — plus inline code, **bold**, and *em*/_em_ markers).

This is a conservative, targeted normalizer, not a general markdown parser: it recognizes exactly those four constructs and leaves anything else untouched. In particular it does not touch [value] usage-string brackets (no following (...)), and it requires non-word characters immediately outside *em*/_em_ delimiters so it doesn’t misfire on identifiers like GIT_DIR or globs. Recognized markup is replaced with its inner text; the surrounding URI/delimiters are discarded (plain-text fallback, since the detail pane doesn’t yet render hyperlinks).

Source

pub fn sanitize_preserving_layout(raw: &str) -> Text

Like Text::sanitize, but for the raw-help display path (the verbatim pane, t), whose entire job is showing a tool’s own bytes as they arrived — not turning them into IR prose. Text::sanitize is the wrong gate there: its steps 6-8 (unwrap hard-wrapped paragraphs, collapse whitespace runs, trim leading/trailing whitespace) are exactly what destroy column alignment, and column alignment is the one thing a side-by-side “does this match the raw pane’s ground truth” review depends on.

This still neutralizes terminal control sequences — the one thing the raw pane cannot safely pass through, since ANSI/OSC/DCS escapes, stray carriage returns, and other C0 controls could scramble the reader’s terminal or misrepresent what arrived — and nothing else:

  1. Strip ANSI/OSC/DCS escape sequences (shares [strip_escapes] with Text::sanitize — same hazard, same fix).
  2. Strip remaining C0 control characters and DEL, including a stray \r — callers pass one already-line-split string at a time (see below), so any \r still present did not terminate a line and is exactly the “carriage return that lies about what’s on screen” hazard, not useful structure.
  3. Expand tabs to spaces at 8-column stops. This is a neutralization too, not a formatting choice: ratatui does not interpret \t as a tab stop the way a real terminal does (unicode-width gives it zero display width), so a raw tab left in would misalign columns in the pane relative to what the reader’s own terminal shows for the same bytes — the opposite of this function’s purpose.
  4. Truncate to MAX_TEXT_CHARS, the same bound Text::sanitize applies, so a pathological single line cannot blow up the pane.

Deliberately not applied: unwrapping, whitespace-collapsing, trimming, or paragraph-break normalization — indentation and internal column alignment are preserved exactly as fetched, and blank lines are whatever the caller’s own line-splitting already produced.

Only [mandible-extract’s help_text::raw_help* functions] call this; every other consumer of a --help probe keeps going through Text::sanitize unchanged — this is an additional path for display, not a redefinition of the existing one.

Source

pub fn as_str(&self) -> &str

Borrow the sanitized string.

Source

pub fn is_empty(&self) -> bool

True if the sanitized text is empty.

Source

pub fn single_line(&self) -> String

Collapse to a single display line (paragraph breaks and internal newlines become a single space), for contexts like tree rows that have no room for multi-line text. The tree pane is expected to call this at render time rather than store a second copy of the text.

Trait Implementations§

Source§

impl Clone for Text

Source§

fn clone(&self) -> Text

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 Text

Source§

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

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

impl Default for Text

Source§

fn default() -> Text

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

impl<'de> Deserialize<'de> for Text

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialization re-runs Text::sanitize rather than trusting the stored bytes verbatim. This keeps the invariant airtight even when a Text is round-tripped through the on-disk cache (spec §11): a tampered or corrupted cache file cannot smuggle unsanitized bytes back into the IR. sanitize is idempotent, so this costs nothing extra for cache entries that were already clean.

Source§

impl Display for Text

Source§

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

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

impl Eq for Text

Source§

impl Hash for Text

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Text

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for Text

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Text

Auto Trait Implementations§

§

impl Freeze for Text

§

impl RefUnwindSafe for Text

§

impl Send for Text

§

impl Sync for Text

§

impl Unpin for Text

§

impl UnsafeUnpin for Text

§

impl UnwindSafe for Text

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> 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.