pub struct Style { /* private fields */ }Expand description
A terminal text style with colors, attributes, and links.
Implementations§
Source§impl Style
impl Style
Sourcepub 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>
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.
Sourcepub fn from_color(color: Option<Color>, bgcolor: Option<Color>) -> Self
pub fn from_color(color: Option<Color>, bgcolor: Option<Color>) -> Self
Creates a style from optional colors.
Sourcepub fn parse(definition: &str) -> Self
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());Sourcepub fn parse_strict(definition: &str) -> Result<Self, StyleError>
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
Sourcepub fn underline2(&self) -> Option<bool>
pub fn underline2(&self) -> Option<bool>
Returns the underline2 attribute.
Sourcepub fn underline_color(&self) -> Option<&Color>
pub fn underline_color(&self) -> Option<&Color>
Returns the underline color.
Sourcepub fn underline_style(&self) -> Option<UnderlineStyle>
pub fn underline_style(&self) -> Option<UnderlineStyle>
Returns the underline style.
Sourcepub fn set_italic(&mut self, value: Option<bool>)
pub fn set_italic(&mut self, value: Option<bool>)
Sets the italic attribute.
Sourcepub fn set_underline(&mut self, value: Option<bool>)
pub fn set_underline(&mut self, value: Option<bool>)
Sets the underline attribute.
Sourcepub fn set_reverse(&mut self, value: Option<bool>)
pub fn set_reverse(&mut self, value: Option<bool>)
Sets the reverse attribute.
Sourcepub fn set_conceal(&mut self, value: Option<bool>)
pub fn set_conceal(&mut self, value: Option<bool>)
Sets the conceal attribute.
Sourcepub fn set_strike(&mut self, value: Option<bool>)
pub fn set_strike(&mut self, value: Option<bool>)
Sets the strike attribute.
Sourcepub fn set_underline_color(&mut self, color: Option<Color>)
pub fn set_underline_color(&mut self, color: Option<Color>)
Sets the underline color.
Sourcepub fn set_underline_style(&mut self, style: Option<UnderlineStyle>)
pub fn set_underline_style(&mut self, style: Option<UnderlineStyle>)
Sets the underline style.
Sourcepub fn combine(styles: &[Style]) -> Style
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.
Sourcepub fn chain(styles: &[Style]) -> Style
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.
Sourcepub fn normalize(&self) -> String
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).
Sourcepub fn combine_refs<'a, I>(styles: I) -> Stylewhere
I: IntoIterator<Item = &'a Style>,
pub fn combine_refs<'a, I>(styles: I) -> Stylewhere
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.
Sourcepub fn render_no_link(
&self,
text: &str,
color_system: Option<ColorSystem>,
) -> String
pub fn render_no_link( &self, text: &str, color_system: Option<ColorSystem>, ) -> String
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.
pub fn render(&self, text: &str, color_system: Option<ColorSystem>) -> String
Sourcepub fn pick_first(candidates: &[Option<&Style>]) -> Style
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.
Sourcepub fn without_color(&self) -> Style
pub fn without_color(&self) -> Style
Returns a copy of this style without colors.
Sourcepub fn background_style(&self) -> Style
pub fn background_style(&self) -> Style
Returns a style with only the background color.
Sourcepub fn clear_meta_and_links(&self) -> Style
pub fn clear_meta_and_links(&self) -> Style
Returns a copy without metadata and links.
Sourcepub fn with_link(url: &str) -> Style
pub fn with_link(url: &str) -> Style
Returns a copy of this style with the given hyperlink URL.
Sourcepub fn update_link(&self, link: Option<&str>) -> Style
pub fn update_link(&self, link: Option<&str>) -> Style
Returns a copy with an updated link.
Sourcepub fn get_html_style(&self, theme: Option<&TerminalTheme>) -> String
pub fn get_html_style(&self, theme: Option<&TerminalTheme>) -> String
Returns a CSS style string for HTML rendering.
Trait Implementations§
impl Eq 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 UnsafeUnpin for Style
impl UnwindSafe for Style
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more