pub struct Console { /* private fields */ }Expand description
The high-level interface for rendering to a terminal. Mirrors
rich.console.Console.
Implementations§
Source§impl Console
impl Console
Sourcepub fn builder() -> ConsoleBuilder
pub fn builder() -> ConsoleBuilder
Start configuring a console explicitly (used by tests and rich-ext).
Sourcepub fn color_system(&self) -> Option<ColorSystem>
pub fn color_system(&self) -> Option<ColorSystem>
The active color system, or None when color is disabled.
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 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 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_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_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 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 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 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 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 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).