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: UnderlineStyleUnderline shape.
attrs: AttrFlagsBoolean SGR text attributes.
link: Option<Arc<Link>>Optional OSC 8 hyperlink target.
Implementations§
Source§impl Style
impl Style
Sourcepub const EMPTY: Style
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.
Sourcepub const fn new() -> Self
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.
Sourcepub fn is_empty(&self) -> bool
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.
Sourcepub fn inherit(&self, base: impl Borrow<Style>) -> Style
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.
Sourcepub fn bold(self) -> Self
pub fn bold(self) -> Self
Add bold intensity and return the updated style.
This sets AttrFlags::BOLD and leaves all other fields unchanged.
Sourcepub fn faint(self) -> Self
pub fn faint(self) -> Self
Add faint intensity and return the updated style.
This sets AttrFlags::FAINT and leaves all other fields unchanged.
Sourcepub fn italic(self) -> Self
pub fn italic(self) -> Self
Add italic text and return the updated style.
This sets AttrFlags::ITALIC and leaves all other fields unchanged.
Sourcepub fn underline(self) -> Self
pub fn underline(self) -> Self
Use a single underline and return the updated style.
Equivalent to Style::underline_style with
UnderlineStyle::Single.
Sourcepub fn strikethrough(self) -> Self
pub fn strikethrough(self) -> Self
Add strikethrough text and return the updated style.
This sets AttrFlags::STRIKETHROUGH and leaves all other fields
unchanged.
Sourcepub fn blink(self) -> Self
pub fn blink(self) -> Self
Add slow blinking text and return the updated style.
This sets AttrFlags::SLOW_BLINK and leaves all other fields
unchanged.
Sourcepub fn rapid_blink(self) -> Self
pub fn rapid_blink(self) -> Self
Add rapid blinking text and return the updated style.
This sets AttrFlags::RAPID_BLINK and leaves all other fields
unchanged.
Sourcepub fn reverse(self) -> Self
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.
Sourcepub fn conceal(self) -> Self
pub fn conceal(self) -> Self
Add concealed text and return the updated style.
This sets AttrFlags::CONCEAL and leaves all other fields unchanged.
Sourcepub fn fg(self, color: impl Into<Option<Color>>) -> Self
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.
Sourcepub fn bg(self, color: impl Into<Option<Color>>) -> Self
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.
Sourcepub fn underline_color(self, color: impl Into<Option<Color>>) -> Self
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.
Sourcepub fn underline_style(self, style: UnderlineStyle) -> Self
pub fn underline_style(self, style: UnderlineStyle) -> Self
Set the underline shape and return the updated style.
Use UnderlineStyle::None to clear underlining.
Sourcepub fn attrs(self, attrs: AttrFlags) -> Self
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.
Sourcepub fn link(self, url: impl Into<String>, params: impl Into<String>) -> Self
pub fn link(self, url: impl Into<String>, params: impl Into<String>) -> Self
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 Display for Style
Render this style as ANSI escape sequences.
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");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,
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()] Read more§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString]. Read more