Skip to main content

Module segment

Module segment 

Source
Expand description

Styled text unit with control codes — the smallest rendering primitive. Segment — styled text unit. Equivalent to Rich’s segment.py.

A Segment is the smallest unit of output: a piece of text with an associated Style and optional control code.

§Core Types

  • Segment — text + optional style + optional control code
  • Segments — a collection of segments with convenience methods
  • ControlType — 16 terminal control codes (bell, cursor movement, etc.)
  • ControlCode — a control type with optional parameters

§Utility Functions (v0.2)

FunctionDescription
Segments::simplifyCombine adjacent segments with identical styles
split_linesSplit segments into lines at newline boundaries
strip_stylesRemove all styling, returning plain text
strip_linksRemove link IDs and URLs from segment styles
align_top / align_middle / align_bottomVertical alignment helpers
divideSplit segments at given cell offsets
set_shapePad or truncate segments to exact width × height
filter_controlKeep only control segments (or only non-control)
get_line_lengthTotal cell width of a line of segments

§Example

use rusty_rich::{Segment, Segments, Style};

let segs = Segments::from(vec![
    Segment::styled("Hello ", Style::new().bold(true)),
    Segment::styled("World", Style::new().bold(true)),
]);

// Combine adjacent same-styled segments
let merged = segs.simplify();
assert_eq!(merged.segments.len(), 1);

Structs§

Segment
A piece of text with an associated style.
Segments
A collection of Segments, with convenience methods.

Enums§

ControlCode
ControlType
Non-printable control codes (equivalent to Rich’s ControlType).

Functions§

align_bottom
Align lines to the bottom of a region of given height.
align_middle
Align lines to the middle of a region of given height.
align_top
Align lines to the top of a region of given height.
divide
Divide segments at the given cell offsets.
filter_control
Filter segments, keeping only control codes if is_control is true, or only non-control segments if is_control is false.
get_line_length
Get the total cell length of a line of segments.
line
Helper: create a newline segment.
set_shape
Set segments to an exact width and height, padding/truncating as needed.
space
Helper: create a space segment.
split_lines
Split an iterable of segments into lines at newline boundaries.
strip_links
Remove link IDs and URLs from all segment styles.
strip_styles
Remove all styles from segments, returning plain text only.