pub struct Theme { /* private fields */ }Expand description
A named collection of styles. Mirrors rich.theme.Theme.
Implementations§
Source§impl Theme
impl Theme
pub fn new() -> Self
Sourcepub fn insert(&mut self, name: impl Into<String>, style: Style)
pub fn insert(&mut self, name: impl Into<String>, style: Style)
Insert or replace a named style.
Sourcepub fn extend_from(&mut self, other: &Theme)
pub fn extend_from(&mut self, other: &Theme)
Insert every style of other, replacing same-named ones: upstream’s
{**base, **other.styles} merge.
Sourcepub fn get_style(&self, style: &StyleType) -> Result<Style>
pub fn get_style(&self, style: &StyleType) -> Result<Style>
Resolve a StyleType against this theme. Port of Console.get_style.
An already-resolved style passes straight through. A name is looked up in
the theme first, and only then parsed as a style definition — the
order matters, because the default theme itself defines bare words like
none, bold and red, and a custom theme has to be able to shadow
them.
The lookup is case-sensitive while the parse fallback is not, which
reproduces an asymmetry upstream really has: "BOLD" misses the theme but
still parses to bold, whereas a theme key "Danger" is never found by a
span naming "danger".
Sourcepub fn get_style_or_null(&self, style: &StyleType) -> Style
pub fn get_style_or_null(&self, style: &StyleType) -> Style
As get_style, but an unresolvable name yields the
null style instead of an error. Port of upstream’s
get_style(..., default=Style.null()), which is what the render path
uses — an unknown name must not blow up a print.
Sourcepub fn names(&self) -> impl Iterator<Item = &str>
pub fn names(&self) -> impl Iterator<Item = &str>
The names of every style in this theme, in no particular order.
Sourcepub fn default_theme() -> Self
pub fn default_theme() -> Self
The complete upstream default theme — every entry of DEFAULT_STYLES.
Sourcepub fn from_styles<I, K, S>(styles: I, inherit: bool) -> Result<Self>
pub fn from_styles<I, K, S>(styles: I, inherit: bool) -> Result<Self>
Build a theme from (name, style) pairs. Port of
Theme(styles, inherit=True): with inherit the upstream default styles
are included first and the given styles override them; without it the
theme holds only the given styles.
Each style may be a definition to parse or an already-built Style,
as upstream accepts Union[str, Style]. A definition that does not parse
is an error, as upstream’s Style.parse raises.
Sourcepub fn config(&self) -> String
pub fn config(&self) -> String
The contents of a config file for this theme. Port of Theme.config:
a [styles] section with one name = style line per style, sorted by
name, each style written as its definition (str(Style)).
Sourcepub fn from_file(config: &str, inherit: bool) -> Result<Self>
pub fn from_file(config: &str, inherit: bool) -> Result<Self>
Load a theme from config-file text. Port of Theme.from_file, which
reads the [styles] section with Python’s configparser.
The configparser behaviour a theme file can observe is reproduced:
option names are lower-cased; = and : both separate name from value;
full-line # and ; comments are skipped; indented lines continue the
previous value; [DEFAULT] options apply to [styles]; %% is a
literal % and %(name)s interpolates. A missing [styles] section, a
duplicate option, a line with no value and a lone % are errors, as they
are upstream.
Sourcepub fn read(path: impl AsRef<Path>, inherit: bool) -> Result<Self>
pub fn read(path: impl AsRef<Path>, inherit: bool) -> Result<Self>
Read a theme from a config file on disk. Port of Theme.read.
The shared default theme, for callers that only need to resolve a name
(e.g. the built-in highlighters) and have no Console to hand.