Skip to main content

Console

Struct Console 

Source
pub struct Console { /* private fields */ }
Expand description

The high-level interface for rendering to a terminal. Mirrors rich.console.Console.

Implementations§

Source§

impl Console

Source

pub fn new() -> Self

Auto-detect terminal capabilities from the environment.

Source

pub fn builder() -> ConsoleBuilder

Start configuring a console explicitly (used by tests and rich-ext).

Source

pub fn color_system(&self) -> Option<ColorSystem>

The active color system, or None when color is disabled.

Source

pub fn width(&self) -> usize

The detected (or configured) width in cells.

Source

pub fn height(&self) -> usize

The detected (or configured) height in rows. Used by height-aware renderables such as Layout.

Source

pub fn is_terminal(&self) -> bool

Whether output is going to a real terminal.

Source

pub fn legacy_windows(&self) -> bool

Whether output targets a legacy Windows console (drives box substitution).

Source

pub fn safe_box(&self) -> bool

Whether to substitute box glyphs for terminal-safe variants (default on).

Source

pub fn ascii_only(&self) -> bool

Whether the terminal can only render ASCII (forces the ASCII box).

Source

pub fn theme(&self) -> &Theme

The active theme.

Source

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.

Source

pub fn base_style(&self) -> &Style

The whole-output base style.

Source

pub fn add_highlighter(&mut self, highlighter: Box<dyn Highlighter + Send>)

Register a highlighter. The core plugin seam — see docs/PLUGINS.md. The highlighter must be Send so a Console can move to a background thread (e.g. an auto-refreshing Live).

Source

pub fn options(&self) -> ConsoleOptions

The default render options for this console (full width, no height).

Source

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

Source

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.

Source

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.

Source

pub fn print(&self, renderable: &dyn Renderable)

Render a value and write it to stdout, followed by a newline.

Source

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

Source

pub fn show_cursor(&self, show: bool)

Show or hide the cursor. Port of Console.show_cursor.

Source

pub fn clear(&self)

Clear the screen. Port of Console.clear.

Source

pub fn bell(&self)

Ring the terminal bell. Port of Console.bell.

Source

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.

Source

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

Source

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.

Source

pub fn page_with( &self, pager: &dyn Pager, styles: bool, f: impl FnOnce(&Console), ) -> Result<()>

Like page but with an explicit Pager — the seam upstream exposes as Console.pager(pager=…).

Source

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

Source

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.

Source

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

Source

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

Source

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

Source

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=…).

Source

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.

Source

pub fn print_str(&self, content: &str)

Parse content as console markup, apply registered highlighters, and print it. This is the console.print("...") path.

Source

pub fn render_str_to_string(&self, content: &str) -> String

Same as Console::print_str but returns the ANSI string.

Source

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.

Source

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.

Source

pub fn try_print_str(&self, content: &str) -> Result<()>

As print_str, but reports malformed markup.

Source

pub fn try_print_justified(&self, content: &str, justify: Justify) -> Result<()>

As print_justified, but reports malformed markup.

Source

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.

Source

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.

Source

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

Trait Implementations§

Source§

impl Default for Console

Source§

fn default() -> Self

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

Auto Trait Implementations§

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

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.