Skip to main content

Console

Struct Console 

Source
pub struct Console {
    pub file: Box<dyn Write + Send>,
    pub color_system: ColorSystem,
    pub theme: Theme,
    pub options: ConsoleOptions,
    pub quiet: bool,
    pub soft_wrap: bool,
    /* private fields */
}
Expand description

The main console for rendering rich output.

Fields§

§file: Box<dyn Write + Send>

The output writer.

§color_system: ColorSystem

Detected color system.

§theme: Theme

Current theme.

§options: ConsoleOptions

Default options.

§quiet: bool

If true, suppress all output.

§soft_wrap: bool

If true, text wraps at word boundaries.

Implementations§

Source§

impl Console

Source

pub fn new() -> Self

Create a new Console writing to stdout.

Source

pub fn with_file(file: Box<dyn Write + Send>) -> Self

Create a Console that writes to a file.

Source

pub fn set_width(&mut self, width: usize)

Set the console width (overrides auto-detected terminal width).

Source

pub fn set_height(&mut self, height: usize)

Set the console height.

Source

pub fn width(&self) -> usize

Get the effective width.

Source

pub fn height(&self) -> usize

Get the effective height.

Source

pub fn render_lines( &self, renderable: &dyn Renderable, options: &ConsoleOptions, style: Option<&Style>, _pad: bool, ) -> Vec<Vec<Segment>>

Render a renderable and return the segment lines.

Source

pub fn get_style(&self, name: &str, default: &str) -> Option<Style>

Look up a style by name from the theme.

Source

pub fn render_str(&self, text: &str, style: &str) -> Text

Render a string (with optional style).

Source

pub fn print(&mut self, objects: &[&dyn Renderable], sep: &str, end: &str)

Print one or more renderable objects, separated by sep, ending with end.

Source

pub fn println(&mut self, renderable: &dyn Renderable)

Print a single renderable followed by a newline.

Source

pub fn print_str(&mut self, text: &str)

Print a plain string (supports markup by default when markup is enabled).

Source

pub fn print_json(&mut self, data: &Value)

Print formatted JSON.

Source

pub fn clear(&mut self)

Clear the screen.

Source

pub fn show_cursor(&mut self)

Show the cursor.

Source

pub fn hide_cursor(&mut self)

Hide the cursor.

Source

pub fn set_window_title(&mut self, title: &str)

Set the terminal window title.

Source

pub fn color_ansi(&self, color: &Color) -> String

Get the ANSI escape string for a given color as this console supports.

Source

pub fn render( &self, renderable: &dyn Renderable, options: &ConsoleOptions, ) -> Vec<Segment>

Render a renderable by recursively flattening nested items into segments. This is equivalent to Python Rich’s Console.render(). It handles Group composition and any renderable that yields other renderables.

Source

pub fn measure( &self, renderable: &dyn Renderable, options: &ConsoleOptions, ) -> Measurement

Measure a renderable’s width constraints. Equivalent to Python Rich’s Measurement.get(console, options, renderable).

Source

pub fn rule( &mut self, title: impl Into<String>, characters: Option<&str>, style: Option<Style>, align: Option<AlignMethod>, )

Render a rule with the given title. Equivalent to Console.rule().

Source

pub fn bell(&mut self)

Output a bell character.

Source

pub fn line(&mut self, count: usize)

Output blank lines.

Source

pub fn log(&mut self, objects: &[&dyn Renderable])

Output a log entry with timestamp, caller info.

Source

pub fn push_theme(&mut self, theme: Theme)

Push a theme onto the stack.

Source

pub fn pop_theme(&mut self)

Pop the current theme, restoring the previous one.

Source

pub fn export_html(&self, renderable: &dyn Renderable) -> String

Export the current console output as an HTML document.

Renders the given renderable and wraps it in a styled HTML page.

Source

pub fn save_html( &self, path: impl AsRef<Path>, renderable: &dyn Renderable, ) -> Result<()>

Save rendered output as an HTML file.

Source

pub fn export_svg(&self, renderable: &dyn Renderable) -> String

Export the current console output as an SVG document.

Source

pub fn save_svg( &self, path: impl AsRef<Path>, renderable: &dyn Renderable, ) -> Result<()>

Save rendered output as an SVG file.

Source

pub fn export_text(&self, renderable: &dyn Renderable) -> String

Export the current console output as plain text (strips ANSI).

Source

pub fn save_text( &self, path: impl AsRef<Path>, renderable: &dyn Renderable, ) -> Result<()>

Save rendered output as a plain text file.

Source

pub fn begin_capture(&mut self)

Enter a capture context. Returns the Console back so it can be used inside a block. Call end_capture() to get the captured text.

Source

pub fn end_capture(&mut self) -> String

End capture and return captured text.

Source

pub fn set_quiet(&mut self, quiet: bool)

Set the quiet flag (suppress all output when true).

Source

pub fn quiet(self, quiet: bool) -> Self

Builder-style setter for quiet.

Source

pub fn set_soft_wrap(&mut self, soft_wrap: bool)

Set the soft-wrap flag (wrap text at word boundaries when true).

Source

pub fn soft_wrap(self, soft_wrap: bool) -> Self

Builder-style setter for soft_wrap.

Source

pub fn input(&mut self, prompt: &str, password: bool) -> String

Read a line of input from the user.

Writes prompt to the console, then reads a line from stdin. When password is true, the input is masked with * characters (using raw terminal mode via crossterm).

Source

pub fn screen(&mut self) -> ScreenContext

Create a ScreenContext that enters the alternate screen buffer. The context automatically exits the alternate screen when dropped.

Source

pub fn set_alt_screen(&mut self, enable: bool)

Enter or exit the alternate screen buffer by writing the corresponding escape sequences (\x1b[?1049h / \x1b[?1049l).

Source

pub fn is_terminal(&self) -> bool

Get whether the output is a terminal.

Source

pub fn set_size(&mut self, width: usize, height: usize)

Set the terminal size (overrides auto-detected dimensions).

Source

pub fn on_broken_pipe(&self)

Handle broken pipe errors gracefully.

In Rust, write() returns ErrorKind::BrokenPipe instead of raising SIGPIPE, so broken pipes are not fatal. The Console already uses let _ = write!(...) throughout, which silently discards all write errors including EPIPE. This method is provided for API compatibility with Python Rich and as a documentation point.

Trait Implementations§

Source§

impl Debug for Console

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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.