pub struct ThemeContext<'a> { /* private fields */ }Expand description
A theme in use on a Console until this guard drops. Returned by
Console::use_theme; upstream’s ThemeContext.
Methods from Deref<Target = Console>§
Sourcepub fn color_system(&self) -> Option<ColorSystem>
pub fn color_system(&self) -> Option<ColorSystem>
The active color system, or None when styles are not rendered at
all. Port of Console.color_system.
Like upstream, this is independent of no_color:
no-colour mode keeps the colour system and strips only the colours at
output time, so bold, italic, underline and the like still render.
Callers asking “will colour reach the terminal?” must check both.
Sourcepub fn no_color(&self) -> bool
pub fn no_color(&self) -> bool
Whether colour output is disabled. Port of Console.no_color: set by
the builder, or by a non-empty NO_COLOR environment variable.
Sourcepub fn get_time(&self) -> f64
pub fn get_time(&self) -> f64
The current time in seconds from this console’s clock. Port of
Console.get_time (default time.monotonic); override it with
ConsoleBuilder::get_time for deterministic animation.
Sourcepub fn height(&self) -> usize
pub fn height(&self) -> usize
The detected (or configured) height in rows. Used by height-aware
renderables such as Layout.
Sourcepub fn is_terminal(&self) -> bool
pub fn is_terminal(&self) -> bool
Whether output is going to a real terminal.
Sourcepub fn legacy_windows(&self) -> bool
pub fn legacy_windows(&self) -> bool
Whether output targets a legacy Windows console (drives box substitution).
Sourcepub fn safe_box(&self) -> bool
pub fn safe_box(&self) -> bool
Whether to substitute box glyphs for terminal-safe variants (default on).
Sourcepub fn ascii_only(&self) -> bool
pub fn ascii_only(&self) -> bool
Whether the terminal can only render ASCII (forces the ASCII box).
Sourcepub fn get_style(&self, style: &StyleType) -> Result<Style>
pub fn get_style(&self, style: &StyleType) -> Result<Style>
Resolve a style name (or pass a style through) against this console’s
theme. Port of Console.get_style.
Sourcepub fn push_theme(&mut self, theme: Theme, inherit: bool)
pub fn push_theme(&mut self, theme: Theme, inherit: bool)
Push a theme on to the top of the stack. Port of Console.push_theme.
With inherit the new top is the current top’s styles overridden by
theme’s; without it, the new top is exactly theme. Prefer
use_theme, which pops again automatically.
Sourcepub fn pop_theme(&mut self) -> Result<()>
pub fn pop_theme(&mut self) -> Result<()>
Remove the top theme, restoring the previous one. Port of
Console.pop_theme; popping the base theme is an error
(upstream’s ThemeStackError("Unable to pop base theme")).
Sourcepub fn use_theme(&mut self, theme: Theme) -> ThemeContext<'_>
pub fn use_theme(&mut self, theme: Theme) -> ThemeContext<'_>
Use a theme until the returned guard is dropped. Port of
Console.use_theme, Python’s context manager as an RAII guard.
The guard dereferences to the console, so print through the guard while it is alive; dropping it pops the theme, including during a panic unwind.
let mut console = Console::builder().width(20).build();
let mut theme = Theme::new();
theme.insert("warning", Style::parse("bold red").unwrap());
{
let themed = console.use_theme(theme);
assert!(themed.theme().get("warning").is_some());
}
assert!(console.theme().get("warning").is_none());Upstream’s use_theme also takes inherit, but its ThemeContext
never passes it on to push_theme, so a used theme always inherits
(verified against rich 15.0.0). This port keeps that behaviour and
omits the ignored parameter; call push_theme to
replace the styles outright.
Sourcepub fn base_style(&self) -> &Style
pub fn base_style(&self) -> &Style
The whole-output base style.
Sourcepub fn add_highlighter(&mut self, highlighter: Box<dyn Highlighter + Send>)
pub fn add_highlighter(&mut self, highlighter: Box<dyn Highlighter + Send>)
Sourcepub fn options(&self) -> ConsoleOptions
pub fn options(&self) -> ConsoleOptions
The default render options for this console (full width, no height).
Sourcepub fn size(&self) -> ConsoleDimensions
pub fn size(&self) -> ConsoleDimensions
The size of the console. Port of the Console.size property.
Sourcepub fn encoding(&self) -> &'static str
pub fn encoding(&self) -> &'static str
The output encoding: "ascii" for an ASCII-only
console, else "utf-8". Port of the Console.encoding property (which
reads the output file’s encoding; this port has no file to ask).
Sourcepub fn render_to_string(&self, renderable: &dyn Renderable) -> String
pub fn render_to_string(&self, renderable: &dyn Renderable) -> String
Render a value to an ANSI string (no trailing newline). Primarily for tests and inline rendering.
When no explicit justify is requested, the width is first shrunk to the renderable’s measured width (matching upstream’s measurement-fit for a bare top-level renderable).
Sourcepub fn render_segments_with(
&self,
renderable: &dyn Renderable,
options: &ConsoleOptions,
) -> Vec<Segment>
pub fn render_segments_with( &self, renderable: &dyn Renderable, options: &ConsoleOptions, ) -> Vec<Segment>
Render a renderable to segments with explicit render options, exactly
as print_with does before writing: a printed
Text goes through upstream’s Text.join, a renderable that opts in
is fitted to its measurement, and the result is cropped to the console
width (print(crop=True)). No trailing newline is added.
For upstream’s lower-level Console.render, see render.
Sourcepub fn render(
&self,
renderable: &dyn Renderable,
options: Option<&ConsoleOptions>,
) -> Vec<Segment>
pub fn render( &self, renderable: &dyn Renderable, options: Option<&ConsoleOptions>, ) -> Vec<Segment>
Render a renderable to segments. Port of Console.render: options
defaults to options, and nothing is rendered when
there is no width (max_width < 1).
Unlike the print path (render_segments_with)
the renderable is rendered as-is: no Text.join, no fitting and no
crop. Container renderables use this for their children.
As everywhere in this port, lines are separated by newline segments:
the final line has no trailing "\n" where upstream’s has one.
Sourcepub fn input(&self, prompt: &str) -> Result<Option<String>>
pub fn input(&self, prompt: &str) -> Result<Option<String>>
Display prompt and read a line of input from standard input. Port of
Console.input: the prompt is console markup, printed through the
console with end="" so it is captured and exported like any other
output. None means end of input.
Sourcepub fn input_from(
&self,
prompt: &dyn Renderable,
stream: &mut dyn InputSource,
) -> Result<Option<String>>
pub fn input_from( &self, prompt: &dyn Renderable, stream: &mut dyn InputSource, ) -> Result<Option<String>>
input with a renderable prompt (upstream accepts a
Text) and an explicit input source — upstream’s stream= argument.
Sourcepub fn render_lines(
&self,
renderable: &dyn Renderable,
options: &ConsoleOptions,
pad: bool,
) -> Vec<Vec<Segment>>
pub fn render_lines( &self, renderable: &dyn Renderable, options: &ConsoleOptions, pad: bool, ) -> Vec<Vec<Segment>>
Render a value into a list of lines, each a list of Segments.
Port of Console.render_lines. When pad is true, every line is padded
(or cropped) to options.max_width — this is what container renderables
such as Panel/Padding rely on to get uniform-width child rows.
Sourcepub fn render_lines_styled(
&self,
renderable: &dyn Renderable,
options: &ConsoleOptions,
style: Option<&Style>,
pad: bool,
) -> Vec<Vec<Segment>>
pub fn render_lines_styled( &self, renderable: &dyn Renderable, options: &ConsoleOptions, style: Option<&Style>, pad: bool, ) -> Vec<Vec<Segment>>
render_lines with upstream’s style= argument:
the style is applied under every rendered segment and to the padding
that fills each line, as Panel and Padding use it.
Sourcepub fn render_export(&self, renderable: &dyn Renderable) -> String
pub fn render_export(&self, renderable: &dyn Renderable) -> String
Render a value exactly as print would write it,
returning the string (including the single trailing newline). For tests
and export.
Sourcepub fn print(&self, renderable: &dyn Renderable)
pub fn print(&self, renderable: &dyn Renderable)
Render a value and write it to stdout, followed by a newline.
Sourcepub fn print_with(&self, renderable: &dyn Renderable, options: &ConsoleOptions)
pub fn print_with(&self, renderable: &dyn Renderable, options: &ConsoleOptions)
Print with explicit render options, the equivalent of upstream’s
Console.print(renderable, justify=…, overflow=…, no_wrap=…). Start
from options and set the fields to override. A
printed Text defers to these options, because upstream’s Text.join
drops the text’s own justify, overflow and no_wrap.
Sourcepub fn render_export_with(
&self,
renderable: &dyn Renderable,
options: &ConsoleOptions,
) -> String
pub fn render_export_with( &self, renderable: &dyn Renderable, options: &ConsoleOptions, ) -> String
Like render_export, with explicit render
options as for print_with.
Sourcepub fn control(&self, control: &Control)
pub fn control(&self, control: &Control)
Write a terminal control sequence to stdout.
Port of Console.control. Control codes are only written when output is
a real terminal (they are meaningless when redirected to a file).
Sourcepub fn is_alt_screen(&self) -> bool
pub fn is_alt_screen(&self) -> bool
Whether the alternate screen is enabled. Port of
Console.is_alt_screen.
Sourcepub fn set_alt_screen(&self, enable: bool) -> bool
pub fn set_alt_screen(&self, enable: bool) -> bool
Enable or disable the alternate screen. Port of
Console.set_alt_screen: only a terminal (not a legacy Windows
console) switches, and the result says whether it did.
Sourcepub fn update_screen(
&self,
renderable: &dyn Renderable,
region: Option<Region>,
options: Option<&ConsoleOptions>,
) -> Result<()>
pub fn update_screen( &self, renderable: &dyn Renderable, region: Option<Region>, options: Option<&ConsoleOptions>, ) -> Result<()>
Render renderable into region of the alternate screen (the whole
screen when None). Port of Console.update_screen.
Sourcepub fn update_screen_lines(
&self,
lines: &[Vec<Segment>],
x: usize,
y: usize,
) -> Result<()>
pub fn update_screen_lines( &self, lines: &[Vec<Segment>], x: usize, y: usize, ) -> Result<()>
Write rendered lines to the alternate screen at column x, row y.
Port of Console.update_screen_lines (through ScreenUpdate).
Sourcepub fn show_cursor(&self, show: bool)
pub fn show_cursor(&self, show: bool)
Show or hide the cursor. Port of Console.show_cursor.
Sourcepub fn capture(&self, f: impl FnOnce(&Console)) -> String
pub fn capture(&self, f: impl FnOnce(&Console)) -> String
Capture everything printed inside f instead of writing it to stdout,
returning it as a rendered (ANSI) string.
The Rust analogue of upstream’s with console.capture() as capture: —
the closure receives the same console, and captures nest correctly.
Equivalent to what would have been written to the terminal.
Sourcepub fn export_text(&self, f: impl FnOnce(&Console)) -> String
pub fn export_text(&self, f: impl FnOnce(&Console)) -> String
Like capture but with all styles stripped, returning
plain text. Port of Console.export_text(styles=False).
Sourcepub fn page(&self, styles: bool, f: impl FnOnce(&Console)) -> Result<()>
pub fn page(&self, styles: bool, f: impl FnOnce(&Console)) -> Result<()>
Buffer everything printed inside f and display it through the system
pager. The Rust analogue of upstream’s with console.pager(): block.
Styles are stripped unless styles is set, matching
Console.pager(styles=False). When there’s no terminal to page in (piped
output, TERM=dumb) or no pager can be started, the content is written
straight to stdout.
Sourcepub fn page_with(
&self,
pager: &dyn Pager,
styles: bool,
f: impl FnOnce(&Console),
) -> Result<()>
pub fn page_with( &self, pager: &dyn Pager, styles: bool, f: impl FnOnce(&Console), ) -> Result<()>
Sourcepub fn export_html(&self, f: impl FnOnce(&Console)) -> String
pub fn export_html(&self, f: impl FnOnce(&Console)) -> String
Capture output printed inside f and export it as a self-contained HTML
document (inline styles), using the default terminal theme. Port of
Console.export_html(inline_styles=True).
Sourcepub fn export_html_themed(
&self,
theme: &TerminalTheme,
f: impl FnOnce(&Console),
) -> String
pub fn export_html_themed( &self, theme: &TerminalTheme, f: impl FnOnce(&Console), ) -> String
Like export_html but with an explicit palette —
upstream’s export_html(theme=…). See terminal_theme for the
bundled presets.
Sourcepub fn export_html_classes(&self, f: impl FnOnce(&Console)) -> String
pub fn export_html_classes(&self, f: impl FnOnce(&Console)) -> String
Like export_html but with a generated CSS-class
stylesheet (.r1 {…}) instead of inline styles. Port of upstream’s
default Console.export_html(inline_styles=False).
Sourcepub fn export_html_classes_themed(
&self,
theme: &TerminalTheme,
f: impl FnOnce(&Console),
) -> String
pub fn export_html_classes_themed( &self, theme: &TerminalTheme, f: impl FnOnce(&Console), ) -> String
Like export_html_classes but with an
explicit palette — upstream’s export_html(theme=…, inline_styles=False).
Sourcepub fn export_svg(
&self,
title: &str,
unique_id: &str,
f: impl FnOnce(&Console),
) -> String
pub fn export_svg( &self, title: &str, unique_id: &str, f: impl FnOnce(&Console), ) -> String
Capture output printed inside f and export it as a self-contained SVG
image of a terminal window, using SVG_EXPORT_THEME. Port of
Console.export_svg.
unique_id prefixes every generated id/class. Upstream’s auto-computed
default hashes Python repr() output (not reproducible in Rust), so this
port takes an explicit id; output is byte-parity with
export_svg(title=…, unique_id=…) (see docs/DIVERGENCES.md #15).
Sourcepub fn export_svg_themed(
&self,
theme: &TerminalTheme,
title: &str,
unique_id: &str,
f: impl FnOnce(&Console),
) -> String
pub fn export_svg_themed( &self, theme: &TerminalTheme, title: &str, unique_id: &str, f: impl FnOnce(&Console), ) -> String
Like export_svg but with an explicit palette —
upstream’s export_svg(theme=…).
Sourcepub fn export_html_with(
&self,
theme: &TerminalTheme,
code_format: Option<&str>,
inline_styles: bool,
f: impl FnOnce(&Console),
) -> Result<String, ExportFormatError>
pub fn export_html_with( &self, theme: &TerminalTheme, code_format: Option<&str>, inline_styles: bool, f: impl FnOnce(&Console), ) -> Result<String, ExportFormatError>
Capture output printed inside f and export it as HTML with every
option of upstream’s Console.export_html: code_format (default
CONSOLE_HTML_FORMAT) and
inline_styles. See export::export_html_with.
Sourcepub fn export_svg_with(
&self,
theme: &TerminalTheme,
title: &str,
unique_id: &str,
code_format: Option<&str>,
font_aspect_ratio: f64,
f: impl FnOnce(&Console),
) -> Result<String, ExportFormatError>
pub fn export_svg_with( &self, theme: &TerminalTheme, title: &str, unique_id: &str, code_format: Option<&str>, font_aspect_ratio: f64, f: impl FnOnce(&Console), ) -> Result<String, ExportFormatError>
Capture output printed inside f and export it as SVG with every
option of upstream’s Console.export_svg: code_format (default
CONSOLE_SVG_FORMAT) and
font_aspect_ratio (upstream default 0.61). See
svg::export_svg_with.
Sourcepub fn record_output(&self, f: impl FnOnce(&Console)) -> Vec<Segment>
pub fn record_output(&self, f: impl FnOnce(&Console)) -> Vec<Segment>
Record everything f prints and hand back the raw segments, without
writing to the terminal.
This is the seam for producing several outputs from one render — the
terminal bytes and an HTML and an SVG file, say — which is what
rich --export-html … --export-svg … needs. Upstream reaches the same
place with Console(record=True) plus save_html(clear=False); here the
buffer is returned instead of being held on the console, so the caller
decides what to do with it and there is no hidden state to clear.
Pair with segments_to_string to get the
terminal form, export::export_html_classes
for HTML, and svg::export_svg for SVG.
Rendering twice instead would be wrong, not merely wasteful: a renderable reading standard input only yields its content once.
Sourcepub fn print_str(&self, content: &str)
pub fn print_str(&self, content: &str)
Parse content as console markup, apply registered highlighters, and
print it. This is the console.print("...") path.
Sourcepub fn render_str_to_string(&self, content: &str) -> String
pub fn render_str_to_string(&self, content: &str) -> String
Same as Console::print_str but returns the ANSI string.
Sourcepub fn build_text(&self, content: &str) -> Text
pub fn build_text(&self, content: &str) -> Text
Parse content as console markup (expanding emoji + applying the active
highlighters), returning the styled Text that print_str would print.
Exposed so callers can wrap the markup in another renderable.
Sourcepub fn try_build_text(&self, content: &str) -> Result<Text>
pub fn try_build_text(&self, content: &str) -> Result<Text>
As build_text, but returns
RichError::Markup for malformed
markup instead of falling back to the raw text — upstream’s behaviour.
Sourcepub fn render_str(&self, content: &str, highlight: Option<bool>) -> Text
pub fn render_str(&self, content: &str, highlight: Option<bool>) -> Text
Convert a plain string to Text the way a str renderable is
converted upstream: emoji codes expand (per the console), console
markup is parsed, and highlighting runs when highlight (or, when
None, the console default) enables it. Port of Console.render_str,
which Table cells, Tree labels and Columns items go through.
Malformed markup falls back to the literal text, as
build_text does (docs/DIVERGENCES.md §2).
Sourcepub fn try_print_str(&self, content: &str) -> Result<()>
pub fn try_print_str(&self, content: &str) -> Result<()>
As print_str, but reports malformed markup.
Sourcepub fn try_print_justified(&self, content: &str, justify: Justify) -> Result<()>
pub fn try_print_justified(&self, content: &str, justify: Justify) -> Result<()>
As print_justified, but reports malformed
markup.
Sourcepub fn render_str_with(
&self,
content: &str,
options: &RenderStrOptions<'_>,
) -> Result<Text>
pub fn render_str_with( &self, content: &str, options: &RenderStrOptions<'_>, ) -> Result<Text>
Convert a string to Text with every keyword upstream’s
Console.render_str takes. Port of Console.render_str, strict:
malformed markup is an error (MarkupError), not the literal text.
Emoji, markup and highlighting default to the console’s settings when
the option is None. As upstream, a highlighted result is a fresh
Text carrying only spans (the highlighter’s, then the markup’s): the
style, justify and overflow are dropped by copy_styles.
Sourcepub fn emoji(&self) -> bool
pub fn emoji(&self) -> bool
Whether :emoji: codes are replaced by default. Port of
Console(emoji=…).
Sourcepub fn highlight(&self) -> bool
pub fn highlight(&self) -> bool
Whether printed strings are highlighted by default. Port of
Console(highlight=…).
Sourcepub fn markup(&self) -> bool
pub fn markup(&self) -> bool
Whether printed strings are parsed as console markup by default. Port
of Console(markup=…).
Sourcepub fn emoji_variant(&self) -> Option<EmojiVariant>
pub fn emoji_variant(&self) -> Option<EmojiVariant>
The default emoji variant. Port of Console(emoji_variant=…).
Sourcepub fn tab_size(&self) -> usize
pub fn tab_size(&self) -> usize
The tab stop width Text expands tabs to. Port of Console.tab_size.
Sourcepub fn set_tab_size(&mut self, tab_size: usize)
pub fn set_tab_size(&mut self, tab_size: usize)
Change the tab stop width. Upstream’s Console.tab_size is a plain
attribute.
Sourcepub fn set_window_title(&self, title: &str) -> bool
pub fn set_window_title(&self, title: &str) -> bool
Set the terminal window title. Port of Console.set_window_title:
only a terminal is sent the code, and the return value says whether it
was.
Sourcepub fn print_justified(&self, content: &str, justify: Justify)
pub fn print_justified(&self, content: &str, justify: Justify)
Parse content as markup and print it justified to the console width.
This is the console.print("...", justify=...) path.
Sourcepub fn render_justified_to_string(
&self,
content: &str,
justify: Justify,
) -> String
pub fn render_justified_to_string( &self, content: &str, justify: Justify, ) -> String
Same as Console::print_justified but returns the ANSI string.
The justify is passed via options.justify, which — matching upstream —
disables the measurement-fit so the text pads to the full width.
Sourcepub fn segments_to_string(&self, segments: &[Segment]) -> String
pub fn segments_to_string(&self, segments: &[Segment]) -> String
Convert rendered segments into a terminal string, applying this console’s
colour system (and honouring no_color). Port of Console._render_buffer.