Skip to main content

Style

Struct Style 

Source
pub struct Style {
Show 15 fields pub color: Option<SimpleColor>, pub bgcolor: Option<SimpleColor>, pub bold: Option<bool>, pub dim: Option<bool>, pub italic: Option<bool>, pub underline: Option<bool>, pub blink: Option<bool>, pub blink2: Option<bool>, pub reverse: Option<bool>, pub conceal: Option<bool>, pub strike: Option<bool>, pub underline2: Option<bool>, pub frame: Option<bool>, pub encircle: Option<bool>, pub overline: Option<bool>,
}
Expand description

Text style with color and attributes.

Uses Option<bool> for attributes to support three states:

  • None: inherit from parent
  • Some(true): enable
  • Some(false): disable

Fields§

§color: Option<SimpleColor>

Foreground color.

§bgcolor: Option<SimpleColor>

Background color.

§bold: Option<bool>

Bold text.

§dim: Option<bool>

Dim/faint text.

§italic: Option<bool>

Italic text.

§underline: Option<bool>

Underlined text.

§blink: Option<bool>

Blinking text.

§blink2: Option<bool>

Rapid blinking text (SGR 6).

§reverse: Option<bool>

Reverse video (swap fg/bg).

§conceal: Option<bool>

Concealed text (SGR 8).

§strike: Option<bool>

Strikethrough text.

§underline2: Option<bool>

Double underline (SGR 21).

§frame: Option<bool>

Framed text (SGR 51).

§encircle: Option<bool>

Encircled text (SGR 52).

§overline: Option<bool>

Overlined text (SGR 53).

Implementations§

Source§

impl Style

Source

pub fn new() -> Self

Create a new empty style.

Source

pub fn color(color: Color) -> Self

Create a style with a foreground color.

Source

pub fn bgcolor(color: Color) -> Self

Create a style with a background color.

Source

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

Builder: set foreground color.

Source

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

Builder: set background color.

Source

pub fn with_bold(self, bold: bool) -> Self

Builder: set bold.

Source

pub fn with_dim(self, dim: bool) -> Self

Builder: set dim.

Source

pub fn with_italic(self, italic: bool) -> Self

Builder: set italic.

Source

pub fn with_underline(self, underline: bool) -> Self

Builder: set underline.

Source

pub fn with_reverse(self, reverse: bool) -> Self

Builder: set reverse video.

Source

pub fn with_strike(self, strike: bool) -> Self

Builder: set strike.

Source

pub fn with_blink2(self, blink2: bool) -> Self

Builder: set blink2 (rapid blink).

Source

pub fn with_conceal(self, conceal: bool) -> Self

Builder: set conceal.

Source

pub fn with_underline2(self, underline2: bool) -> Self

Builder: set underline2 (double underline).

Source

pub fn with_frame(self, frame: bool) -> Self

Builder: set frame.

Source

pub fn with_encircle(self, encircle: bool) -> Self

Builder: set encircle.

Source

pub fn with_overline(self, overline: bool) -> Self

Builder: set overline.

Source

pub fn combine(&self, other: &Style) -> Self

Combine this style with another, with other taking precedence.

Values from other override values from self only if they are Some.

Source

pub fn parse(s: &str) -> Option<Self>

Parse a style from a string.

Supports space-separated style definitions like:

  • “bold red on blue”
  • “italic #ff0000”
  • “bold underline”
  • “not bold” (negation)
Source

pub fn is_null(&self) -> bool

Check if this is a null style (all attributes are None).

Source

pub fn render(&self, text: &str, color_system: ColorSystem) -> String

Render text with this style using ANSI escape codes.

§Arguments
  • text - The text to style.
  • color_system - The color system to render to.
§Returns

A string containing the text wrapped in ANSI escape codes. If text is empty, returns empty string.

§Examples
use rich_rs::{Style, ColorSystem, SimpleColor};

let style = Style::new().with_bold(true).with_color(SimpleColor::Standard(1));
let rendered = style.render("Hello", ColorSystem::TrueColor);
assert!(rendered.contains("\x1b["));
assert!(rendered.contains("Hello"));
assert!(rendered.ends_with("\x1b[0m"));
Source

pub fn render_open(&self, text: &str, color_system: ColorSystem) -> String

Render styled text WITHOUT a trailing reset.

This is used for streaming output where we want to minimize SGR resets to avoid visual artifacts (like black hairlines between colored lines). The caller is responsible for emitting a reset at the end.

Source

pub fn get_html_style(&self) -> String

Get a CSS style string for this style.

§Returns

A semicolon-separated CSS string suitable for use in a style attribute.

§Examples
use rich_rs::{Style, SimpleColor};

let style = Style::new()
    .with_bold(true)
    .with_color(SimpleColor::Rgb { r: 255, g: 0, b: 0 });
let css = style.get_html_style();
assert!(css.contains("font-weight: bold"));
assert!(css.contains("color:"));
Source

pub fn chain(styles: &[Style]) -> Self

Chain multiple styles together. Like combine but applied left-to-right.

This is equivalent to Python Rich’s Style.chain(*styles).

Source

pub fn from_color(color: Option<Color>, bgcolor: Option<Color>) -> Self

Create a style with only foreground/background colors.

This is equivalent to Python Rich’s Style.from_color.

Source

pub fn on<I, K>( meta: Option<BTreeMap<String, MetaValue>>, handlers: I, ) -> StyleMeta
where I: IntoIterator<Item = (K, MetaValue)>, K: Into<String>,

Create blank style metadata with optional event handlers.

In Python Rich, Style.on(...) returns a Style with metadata attached. In this crate, metadata is represented separately as StyleMeta.

Source

pub fn normalize(style: &str) -> String

Normalize a style definition to a canonical representation.

This is equivalent to Python Rich’s Style.normalize. Returns lowercased/trimmed input for syntactically invalid definitions.

Source

pub fn pick_first(values: &[Option<Style>]) -> Style

Return the first non-None style from a sequence.

Panics if all styles are None, matching Python Rich’s ValueError behavior.

Source

pub fn test(&self, text: &str, color_system: ColorSystem) -> String

Apply the style to text and return the ANSI string.

This is equivalent to Python Rich’s Style.test().

Source

pub fn background_style(&self) -> Self

Return a new Style with only the background color.

This is equivalent to Python Rich’s Style.background_style.

Source

pub fn without_color(&self) -> Self

Return a new Style with colors stripped but attributes preserved.

This is equivalent to Python Rich’s Style.without_color.

Source

pub fn has_transparent_background(&self) -> bool

Check if the style has a transparent (unset or default) background.

This is equivalent to Python Rich’s Style.transparent_background.

Source

pub fn to_markup_string(&self) -> String

Convert this style to its markup-compatible string representation.

Returns a string like "bold red on blue" that can be used in Rich markup tags. This is the canonical way to serialize a Style for markup — all consumers (Text::to_markup, Theme serialization, etc.) should use this method.

Trait Implementations§

Source§

impl Add for Style

Source§

type Output = Style

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl Clone for Style

Source§

fn clone(&self) -> Style

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Style

Source§

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

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

impl Default for Style

Source§

fn default() -> Style

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

impl PartialEq for Style

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Copy for Style

Source§

impl Eq for Style

Source§

impl StructuralPartialEq for Style

Auto Trait Implementations§

§

impl Freeze for Style

§

impl RefUnwindSafe for Style

§

impl Send for Style

§

impl Sync for Style

§

impl Unpin for Style

§

impl UnwindSafe for Style

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<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, 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.