Skip to main content

Style

Struct Style 

Source
pub struct Style {
    pub fg: Option<Color>,
    pub bg: Option<Color>,
    pub underline_color: Option<Color>,
    pub underline: UnderlineStyle,
    pub attrs: AttrFlags,
    pub link: Option<Arc<Link>>,
}
Expand description

A complete terminal text style.

Style is the central value used by cells, text painters, and renderers. It stores SGR state (foreground/background/underline colors, underline shape, and attributes) plus an optional OSC 8 hyperlink. Build styles with the provided value-taking builders, then render with the std::fmt::Display implementation: {style} writes the opener and {style:#} writes the matching closer.

Cloning is cheap for hyperlinks: the Link is reference-counted so a long span of identically-linked cells keeps a single shared allocation.

Fields§

§fg: Option<Color>

Foreground/text color.

None leaves the terminal’s current/default foreground unchanged when writing a full style; diffs use SGR 39 to clear a previous foreground.

§bg: Option<Color>

Background color.

None leaves the terminal’s current/default background unchanged when writing a full style; diffs use SGR 49 to clear a previous background.

§underline_color: Option<Color>

Underline color.

Encoded with SGR 58 when present. None means use the terminal’s default underline color; diffs use SGR 59 to clear a previous value.

§underline: UnderlineStyle

Underline shape.

§attrs: AttrFlags

Boolean SGR text attributes.

§link: Option<Arc<Link>>

Optional OSC 8 hyperlink target.

Implementations§

Source§

impl Style

Source

pub const EMPTY: Style

Style with no colors, attributes, underline, or hyperlink.

This is equivalent to Style::default(). Writing it as a style opener emits nothing, since the opener is additive; format any style with the alternate flag ({style:#}) to emit an explicit return to the terminal default.

Source

pub const fn new() -> Self

Create an empty style.

Equivalent to Style::default and Style::EMPTY: no colors, attributes, underline, or hyperlink. Chain the builder methods such as bold or fg to add settings.

Source

pub fn is_empty(&self) -> bool

Return whether this style is entirely empty.

Empty means no SGR-relevant fields and no OSC 8 hyperlink. This is equivalent to *self == Style::EMPTY.

Source

pub fn inherit(&self, base: impl Borrow<Style>) -> Style

Inherit from base, returning self with its unset fields filled in.

self takes precedence, like a child overriding inherited values: its foreground, background, underline color, underline shape, and hyperlink win wherever self sets them, and base only supplies a fallback for each field self leaves at its default. Attributes from both are combined. Inheriting from any base therefore never clears a field self already set, and inheriting from an empty base returns self unchanged.

base is borrowed, so either an owned Style or a &Style may be passed without cloning.

Source

pub fn bold(self) -> Self

Add bold intensity and return the updated style.

This sets AttrFlags::BOLD and leaves all other fields unchanged.

Source

pub fn faint(self) -> Self

Add faint intensity and return the updated style.

This sets AttrFlags::FAINT and leaves all other fields unchanged.

Source

pub fn italic(self) -> Self

Add italic text and return the updated style.

This sets AttrFlags::ITALIC and leaves all other fields unchanged.

Source

pub fn underline(self) -> Self

Use a single underline and return the updated style.

Equivalent to Style::underline_style with UnderlineStyle::Single.

Source

pub fn strikethrough(self) -> Self

Add strikethrough text and return the updated style.

This sets AttrFlags::STRIKETHROUGH and leaves all other fields unchanged.

Add slow blinking text and return the updated style.

This sets AttrFlags::SLOW_BLINK and leaves all other fields unchanged.

Add rapid blinking text and return the updated style.

This sets AttrFlags::RAPID_BLINK and leaves all other fields unchanged.

Source

pub fn reverse(self) -> Self

Reverse foreground and background and return the updated style.

This sets AttrFlags::REVERSE; it does not swap the stored fg and bg values.

Source

pub fn conceal(self) -> Self

Add concealed text and return the updated style.

This sets AttrFlags::CONCEAL and leaves all other fields unchanged.

Source

pub fn fg(self, color: impl Into<Option<Color>>) -> Self

Set or clear the foreground color and return the updated style.

Accepts any value convertible into Option<Color>, including a Color or None. Passing None clears any foreground color carried by the base style.

Source

pub fn bg(self, color: impl Into<Option<Color>>) -> Self

Set or clear the background color and return the updated style.

Accepts any value convertible into Option<Color>, including a Color or None. Passing None clears any background color carried by the base style.

Source

pub fn underline_color(self, color: impl Into<Option<Color>>) -> Self

Set or clear the underline color and return the updated style.

Accepts any value convertible into Option<Color>, including a Color or None. Passing None clears any underline color carried by the base style.

Source

pub fn underline_style(self, style: UnderlineStyle) -> Self

Set the underline shape and return the updated style.

Use UnderlineStyle::None to clear underlining.

Source

pub fn attrs(self, attrs: AttrFlags) -> Self

Replace the entire attribute flag set and return the updated style.

This is useful when applying a previously computed AttrFlags value. Use the convenience builders when adding a single flag.

Attach or clear an OSC 8 hyperlink and return the updated style.

A non-empty url stores a Link with the supplied params. An empty url clears any existing link and ignores params. Parameters are the raw OSC 8 parameter string, commonly empty or a terminal-supported value such as id=foo.

Trait Implementations§

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 Default for Style

Source§

fn default() -> Style

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

impl Display for Style

Render this style as ANSI escape sequences.

The default form ({style}) renders the opener: the SGR sequence (CSI … m) followed by an OSC 8 hyperlink start when this style carries a link. The opener is additive, so an empty style renders nothing.

The alternate form ({style:#}) renders the closer: the OSC 8 hyperlink terminator (when the style carries a link) followed by the SGR reset (CSI m, when it carries SGR state). The SGR reset clears all attributes to their defaults rather than restoring a previously active style.

Use the two together to wrap a span with a single style value:

use uncurses::color::Color;
use uncurses::style::Style;

let style = Style::default().bold().fg(Color::Green);
assert_eq!(format!("{style}hi{style:#}"), "\x1b[1;32mhi\x1b[m");
Source§

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

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

impl From<&Style> for Style

Source§

fn from(style: &Style) -> Self

Clone a borrowed style into an owned one.

This lets APIs that accept impl Into<Style> take a &Style without the caller writing .clone(). Owned styles convert for free via the blanket From<Style> for Style.

Source§

impl From<Option<Style>> for Style

Source§

fn from(style: Option<Style>) -> Self

Convert an optional style, mapping None to Style::EMPTY.

This lets APIs that accept impl Into<Style> take a bare None to mean “no styling”, e.g. surface.set_str(pos, text, None). Some(style) unwraps to the contained 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.
Source§

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

§

impl<T> ToCompactString for T
where T: Display,

§

fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>

Fallible version of [ToCompactString::to_compact_string()] Read more
§

fn to_compact_string(&self) -> CompactString

Converts the given value to a [CompactString]. Read more
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.
§

impl<T> ErasedDestructor for T
where T: 'static,