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: ColorSystemDetected color system.
theme: ThemeCurrent theme.
options: ConsoleOptionsDefault options.
quiet: boolIf true, suppress all output.
soft_wrap: boolIf true, text wraps at word boundaries.
Implementations§
Source§impl Console
impl Console
Sourcepub fn set_width(&mut self, width: usize)
pub fn set_width(&mut self, width: usize)
Set the console width (overrides auto-detected terminal width).
Sourcepub fn set_height(&mut self, height: usize)
pub fn set_height(&mut self, height: usize)
Set the console height.
Sourcepub fn render_lines(
&self,
renderable: &dyn Renderable,
options: &ConsoleOptions,
style: Option<&Style>,
_pad: bool,
) -> Vec<Vec<Segment>>
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.
Sourcepub fn get_style(&self, name: &str, default: &str) -> Option<Style>
pub fn get_style(&self, name: &str, default: &str) -> Option<Style>
Look up a style by name from the theme.
Sourcepub fn render_str(&self, text: &str, style: &str) -> Text
pub fn render_str(&self, text: &str, style: &str) -> Text
Render a string (with optional style).
Sourcepub fn print(&mut self, objects: &[&dyn Renderable], sep: &str, end: &str)
pub fn print(&mut self, objects: &[&dyn Renderable], sep: &str, end: &str)
Print one or more renderable objects, separated by sep, ending with
end.
Sourcepub fn println(&mut self, renderable: &dyn Renderable)
pub fn println(&mut self, renderable: &dyn Renderable)
Print a single renderable followed by a newline.
Sourcepub fn print_str(&mut self, text: &str)
pub fn print_str(&mut self, text: &str)
Print a plain string (supports markup by default when markup is
enabled).
Sourcepub fn print_json(&mut self, data: &Value)
pub fn print_json(&mut self, data: &Value)
Print formatted JSON.
Sourcepub fn show_cursor(&mut self)
pub fn show_cursor(&mut self)
Show the cursor.
Sourcepub fn hide_cursor(&mut self)
pub fn hide_cursor(&mut self)
Hide the cursor.
Sourcepub fn set_window_title(&mut self, title: &str)
pub fn set_window_title(&mut self, title: &str)
Set the terminal window title.
Sourcepub fn color_ansi(&self, color: &Color) -> String
pub fn color_ansi(&self, color: &Color) -> String
Get the ANSI escape string for a given color as this console supports.
Sourcepub fn render(
&self,
renderable: &dyn Renderable,
options: &ConsoleOptions,
) -> Vec<Segment>
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.
Sourcepub fn measure(
&self,
renderable: &dyn Renderable,
options: &ConsoleOptions,
) -> Measurement
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).
Sourcepub fn rule(
&mut self,
title: impl Into<String>,
characters: Option<&str>,
style: Option<Style>,
align: Option<AlignMethod>,
)
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().
Sourcepub fn log(&mut self, objects: &[&dyn Renderable])
pub fn log(&mut self, objects: &[&dyn Renderable])
Output a log entry with timestamp, caller info.
Sourcepub fn push_theme(&mut self, theme: Theme)
pub fn push_theme(&mut self, theme: Theme)
Push a theme onto the stack.
Sourcepub fn export_html(&self, renderable: &dyn Renderable) -> String
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.
Sourcepub fn save_html(
&self,
path: impl AsRef<Path>,
renderable: &dyn Renderable,
) -> Result<()>
pub fn save_html( &self, path: impl AsRef<Path>, renderable: &dyn Renderable, ) -> Result<()>
Save rendered output as an HTML file.
Sourcepub fn export_svg(&self, renderable: &dyn Renderable) -> String
pub fn export_svg(&self, renderable: &dyn Renderable) -> String
Export the current console output as an SVG document.
Sourcepub fn save_svg(
&self,
path: impl AsRef<Path>,
renderable: &dyn Renderable,
) -> Result<()>
pub fn save_svg( &self, path: impl AsRef<Path>, renderable: &dyn Renderable, ) -> Result<()>
Save rendered output as an SVG file.
Sourcepub fn export_text(&self, renderable: &dyn Renderable) -> String
pub fn export_text(&self, renderable: &dyn Renderable) -> String
Export the current console output as plain text (strips ANSI).
Sourcepub fn save_text(
&self,
path: impl AsRef<Path>,
renderable: &dyn Renderable,
) -> Result<()>
pub fn save_text( &self, path: impl AsRef<Path>, renderable: &dyn Renderable, ) -> Result<()>
Save rendered output as a plain text file.
Sourcepub fn set_soft_wrap(&mut self, soft_wrap: bool)
pub fn set_soft_wrap(&mut self, soft_wrap: bool)
Set the soft-wrap flag (wrap text at word boundaries when true).
Sourcepub fn input(&mut self, prompt: &str, password: bool) -> String
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).
Sourcepub fn screen(&mut self) -> ScreenContext
pub fn screen(&mut self) -> ScreenContext
Create a ScreenContext that enters the
alternate screen buffer. The context automatically exits the alternate
screen when dropped.
Sourcepub fn set_alt_screen(&mut self, enable: bool)
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).
Sourcepub fn is_terminal(&self) -> bool
pub fn is_terminal(&self) -> bool
Get whether the output is a terminal.
Sourcepub fn set_size(&mut self, width: usize, height: usize)
pub fn set_size(&mut self, width: usize, height: usize)
Set the terminal size (overrides auto-detected dimensions).
Sourcepub fn on_broken_pipe(&self)
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.
Source§impl Console
impl Console
Sourcepub fn begin_capture(&mut self)
pub fn begin_capture(&mut self)
Start capturing all output. All subsequent writes to this console are
redirected to an internal buffer. Call end_capture
to stop capturing and retrieve the captured content.
Sourcepub fn end_capture(&mut self) -> Capture
pub fn end_capture(&mut self) -> Capture
End capture mode and return the Capture containing all output written
while capturing was active. The console’s output is restored to its
original destination.
Sourcepub fn capture<F: FnOnce(&mut Self)>(&mut self, f: F) -> String
pub fn capture<F: FnOnce(&mut Self)>(&mut self, f: F) -> String
Run the given closure with output captured, returning the captured text.
This is the most ergonomic way to capture output in Rust:
let mut console = Console::new();
let output = console.capture(|c| {
c.print_str("Hello, world!");
});
assert_eq!(output, "Hello, world!");Sourcepub fn pager(&mut self, styles: bool) -> PagerContext ⓘ
pub fn pager(&mut self, styles: bool) -> PagerContext ⓘ
Get a PagerContext. Content rendered while the context is alive is
collected and displayed through the system pager ($PAGER or less)
when the context is dropped.
styles controls whether ANSI styles are preserved when paging.
Sourcepub fn input_renderable(&mut self, prompt: &dyn Renderable) -> String
pub fn input_renderable(&mut self, prompt: &dyn Renderable) -> String
Display a Renderable prompt and read a line of input from stdin.
The prompt is rendered through the console’s current options and theme.
Sourcepub fn print_exception(&mut self, _width: Option<usize>, _extra_lines: usize)
pub fn print_exception(&mut self, _width: Option<usize>, _extra_lines: usize)
Print the current exception as a rich traceback.
In Rust, this is a best-effort rendering; it captures the current
thread’s panic info if available. width overrides the output width,
and extra_lines controls how many lines of source context to show
around each frame.
Sourcepub fn print_json_str(&mut self, json: &str)
pub fn print_json_str(&mut self, json: &str)
Pretty-print a JSON string. Parses the string and renders it with syntax highlighting.
Sourcepub fn render_to_lines(
&self,
renderable: &dyn Renderable,
options: &ConsoleOptions,
) -> Vec<Vec<Segment>>
pub fn render_to_lines( &self, renderable: &dyn Renderable, options: &ConsoleOptions, ) -> Vec<Vec<Segment>>
Render a renderable to a vector of segment lines.
This is the lower-level render entry point, returning raw lines instead
of an ANSI string. Compare with render which returns
flat segments.
Sourcepub fn render_ansi(&self, text: &str) -> String
pub fn render_ansi(&self, text: &str) -> String
Render a plain string to ANSI text, applying the current theme and styles. Returns the ANSI-formatted string.
Sourcepub fn export_svg_opts(&self, options: &ExportSvgOptions) -> String
pub fn export_svg_opts(&self, options: &ExportSvgOptions) -> String
Export the console output as an SVG document with explicit options.
This delegates to crate::export::export_svg with the given
ExportSvgOptions.
Sourcepub fn size(&self) -> ConsoleDimensions
pub fn size(&self) -> ConsoleDimensions
Get the terminal size as ConsoleDimensions.
Sourcepub fn is_dumb_terminal(&self) -> bool
pub fn is_dumb_terminal(&self) -> bool
Check if the terminal is a “dumb” terminal (no color support).
Sourcepub fn is_alt_screen(&self) -> bool
pub fn is_alt_screen(&self) -> bool
Check if the console is currently in the alternate screen buffer.
Sourcepub fn set_cursor_visible(&mut self, visible: bool)
pub fn set_cursor_visible(&mut self, visible: bool)
Show or hide the cursor based on the boolean parameter.
true shows the cursor, false hides it. Tracks the current state
so it can be queried via internal fields.
Sourcepub fn use_theme(&mut self, theme: Theme) -> ThemeContext
pub fn use_theme(&mut self, theme: Theme) -> ThemeContext
Temporarily switch to a different theme. Returns a ThemeContext
that restores the original theme when dropped.
§Example
let mut console = Console::new();
let custom = Theme::new();
{
let _ctx = console.use_theme(custom);
// console uses custom theme here
}
// original theme restored hereSourcepub fn clear_live(&mut self)
pub fn clear_live(&mut self)
Clear the live display region. When in alt-screen mode, this clears
the entire alternate screen. Otherwise, it’s equivalent to
clear.
Sourcepub fn set_live(&mut self, _live: &Live)
pub fn set_live(&mut self, _live: &Live)
Set the active live display. Stores a reference to the [Live]
renderer for integration with the console’s rendering pipeline.
Note: Live manages its own refresh cycle;
this method is primarily for API compatibility with Python Rich.
Sourcepub fn update_screen(
&mut self,
renderable: &dyn Renderable,
options: Option<&ConsoleOptions>,
)
pub fn update_screen( &mut self, renderable: &dyn Renderable, options: Option<&ConsoleOptions>, )
Update the full screen (enter alt-screen, render content, exit).
Clears the screen and renders the given renderable. If options is
None, the console’s current options are used.
Sourcepub fn update_screen_lines(
&mut self,
lines: &[Vec<Segment>],
options: Option<&ConsoleOptions>,
)
pub fn update_screen_lines( &mut self, lines: &[Vec<Segment>], options: Option<&ConsoleOptions>, )
Update the screen from pre-rendered lines of segments.
Takes already-rendered lines and displays them as the full screen content, clearing existing content first.
Sourcepub fn push_render_hook(&mut self, hook: RenderHook)
pub fn push_render_hook(&mut self, hook: RenderHook)
Add a RenderHook to the console. Hooks are applied in order and
can modify the rendered lines before they are displayed.
Sourcepub fn pop_render_hook(&mut self) -> Option<RenderHook>
pub fn pop_render_hook(&mut self) -> Option<RenderHook>
Remove and return the most recently added RenderHook, if any.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Console
impl !RefUnwindSafe for Console
impl Send for Console
impl !Sync for Console
impl Unpin for Console
impl UnsafeUnpin for Console
impl !UnwindSafe for Console
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more