Skip to main content

Style

Struct Style 

Source
pub struct Style { /* private fields */ }
Expand description

A terminal text style with colors, attributes, and links.

Implementations§

Source§

impl Style

Source

pub fn new( color: Option<&str>, bgcolor: Option<&str>, bold: Option<bool>, dim: Option<bool>, italic: Option<bool>, underline: Option<bool>, blink: Option<bool>, blink2: Option<bool>, reverse: Option<bool>, conceal: Option<bool>, strike: Option<bool>, underline2: Option<bool>, frame: Option<bool>, encircle: Option<bool>, overline: Option<bool>, link: Option<&str>, ) -> Result<Self, StyleError>

Creates a new style with specified attributes.

Source

pub fn null() -> Self

Creates an empty null style with no attributes set.

Source

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

Creates a style from optional colors.

Source

pub fn parse(definition: &str) -> Self

Parses a style definition string. Lossy: returns Style::null on any parse error.

This is the recommended entry point for the common case of literal style strings ("bold red", "on blue", …) where a parse failure indicates a programming bug, not a recoverable runtime condition. For static literals there is nothing meaningful to do with an error at the callsite — the lossy form removes the boilerplate unwrap_or_else(|_| Style::null()) that wrapped almost every previous use.

§When to use parse_strict instead
  • You’re parsing user-supplied style strings (config files, CLI flags) and want to surface a syntax error.
  • You want to write a unit test that verifies a particular input fails to parse.
§Examples
use gilt::style::Style;

let bold_red = Style::parse("bold red");          // no `?`, no `.unwrap()`
let same     = Style::parse("BOLD RED");          // case-insensitive keywords
let null     = Style::parse("not a real style");  // returns Style::null(), no panic
assert!(null.is_null());
Source

pub fn parse_strict(definition: &str) -> Result<Self, StyleError>

Parses a style definition string. Strict: returns the parse error if the input is malformed.

Prefer parse for the common case where a parse failure indicates a programming bug, not a recoverable runtime condition.

§Grammar
  • Words are split by whitespace
  • "on" keyword: next word is background color
  • "not" keyword: next word is attribute name, set to false
  • "link" keyword: next word is URL
  • Known attribute names with aliases
  • Anything else: try as foreground color
Source

pub fn bold(&self) -> Option<bool>

Returns the bold attribute.

Source

pub fn dim(&self) -> Option<bool>

Returns the dim attribute.

Source

pub fn italic(&self) -> Option<bool>

Returns the italic attribute.

Source

pub fn underline(&self) -> Option<bool>

Returns the underline attribute.

Returns the blink attribute.

Source

pub fn blink2(&self) -> Option<bool>

Returns the blink2 attribute.

Source

pub fn reverse(&self) -> Option<bool>

Returns the reverse attribute.

Source

pub fn conceal(&self) -> Option<bool>

Returns the conceal attribute.

Source

pub fn strike(&self) -> Option<bool>

Returns the strike attribute.

Source

pub fn underline2(&self) -> Option<bool>

Returns the underline2 attribute.

Source

pub fn frame(&self) -> Option<bool>

Returns the frame attribute.

Source

pub fn encircle(&self) -> Option<bool>

Returns the encircle attribute.

Source

pub fn overline(&self) -> Option<bool>

Returns the overline attribute.

Source

pub fn color(&self) -> Option<&Color>

Returns the foreground color.

Source

pub fn bgcolor(&self) -> Option<&Color>

Returns the background color.

Returns the link URL.

Source

pub fn underline_color(&self) -> Option<&Color>

Returns the underline color.

Source

pub fn underline_style(&self) -> Option<UnderlineStyle>

Returns the underline style.

Source

pub fn set_bold(&mut self, value: Option<bool>)

Sets the bold attribute.

Source

pub fn set_dim(&mut self, value: Option<bool>)

Sets the dim attribute.

Source

pub fn set_italic(&mut self, value: Option<bool>)

Sets the italic attribute.

Source

pub fn set_underline(&mut self, value: Option<bool>)

Sets the underline attribute.

Sets the blink attribute.

Source

pub fn set_reverse(&mut self, value: Option<bool>)

Sets the reverse attribute.

Source

pub fn set_conceal(&mut self, value: Option<bool>)

Sets the conceal attribute.

Source

pub fn set_strike(&mut self, value: Option<bool>)

Sets the strike attribute.

Source

pub fn set_underline_color(&mut self, color: Option<Color>)

Sets the underline color.

Source

pub fn set_underline_style(&mut self, style: Option<UnderlineStyle>)

Sets the underline style.

Source

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

Combines multiple styles into one (left-to-right merge).

Prefer Style::combine_refs in hot paths — combine clones each input on the way through Add<Style>. The reference-based variant avoids the per-step clone.

Source

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

Alias for Style::combine matching Python rich’s Style.chain.

Applies each style left-to-right; later styles override earlier ones for conflicting attributes.

Source

pub fn normalize(&self) -> String

Returns the canonical string form of this style.

Equivalent to formatting and re-parsing: Style::parse(&format!("{}", self)). Useful for normalising user-supplied style strings (config, CLI flags) to a canonical representation.

Mirrors Python rich’s Style.normalize(definition).

Source

pub fn combine_refs<'a, I>(styles: I) -> Style
where I: IntoIterator<Item = &'a Style>,

Like Style::combine but iterates over references — avoids the per-step Style.clone() that the by-value Add<Style> impl forces.

Used by the per-segment span-rendering loop in Text where the same active span set is consulted thousands of times per render.

Renders text with this style as ANSI escape sequences. Render text with this style’s SGR codes (color, bold, etc.) but without wrapping in an OSC 8 hyperlink even if self.link is set.

Used by Console::render_buffer when it has decided to coalesce a run of consecutive same-link segments under a single OSC 8 wrapper.

Returns plain text when color_system is None or text is empty.

Source

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

Source

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

Pick the first non-null style from the candidate list, or Style::null if every candidate is null/None.

Mirrors rich’s Style.pick_first(*candidates) — useful in render pipelines that select among theme / row / column / cell style overrides.

Source

pub fn is_null(&self) -> bool

Returns true if this is a null style (nothing set).

Source

pub fn without_color(&self) -> Style

Returns a copy of this style without colors.

Source

pub fn background_style(&self) -> Style

Returns a style with only the background color.

Returns a copy without metadata and links.

Returns a copy of this style with the given hyperlink URL.

Returns a copy with an updated link.

Source

pub fn get_html_style(&self, theme: Option<&TerminalTheme>) -> String

Returns a CSS style string for HTML rendering.

Trait Implementations§

Source§

impl Add for Style

Source§

type Output = Style

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Style) -> Style

Performs the + operation. Read more
Source§

impl Add<Option<Style>> for Style

Source§

type Output = Style

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Option<Style>) -> Style

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 (const: unstable) · 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 Display for Style

Source§

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

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

impl Eq for Style

Source§

impl Hash for Style

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 Style

Source§

fn eq(&self, other: &Self) -> 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.

Auto Trait Implementations§

§

impl Freeze for Style

§

impl RefUnwindSafe for Style

§

impl Send for Style

§

impl Sync for Style

§

impl Unpin for Style

§

impl UnsafeUnpin 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

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> ToCompactString for T
where T: Display,

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.