Skip to main content

Json

Struct Json 

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

A parsed JSON document, rendered with syntax highlighting. Mirrors rich.json.JSON.

Implementations§

Source§

impl Json

Source

pub fn new(text: &str) -> Result<Self>

Parse text as JSON. Returns an error if it is not valid JSON.

Source

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

Keep rich.json.JSON’s self.text.no_wrap = True, which crops each line at the available width instead of wrapping it.

Defaults to false, because that is what a top-level Console.print(JSON(...)) produces and that is how this renderable is normally reached. Upstream’s flag really is set, but Console.print never renders the Text it is set on: _collect_renderables sees a Text, buffers it, and check_text hands back Text(sep, justify=…, end=…).join(buffered) — and Text.join starts from self.blank_copy(), i.e. from the separator’s metadata. The separator has no_wrap=None, so the copy that actually renders wraps with the default fold overflow.

Turn it on whenever the document is nested inside another renderable — a Panel, Padding, Styled, Constrain, or rich-cli’s ForceWidth. Those are ConsoleRenderables, so _collect_renderables appends them untouched and the inner Text reaches Text.__rich_console__ with the flag intact; Text.wrap then skips divide_line and only truncates to the width.

The two are not interchangeable. Wrapping keeps every character; cropping discards what does not fit, which for JSON means the printed document no longer parses — so the choice has to follow upstream’s, not taste.

Trait Implementations§

Source§

impl Renderable for Json

Source§

fn rich_render( &self, _console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>

Source§

fn measure(&self, _console: &Console, options: &ConsoleOptions) -> Measurement

The (minimum, maximum) cell width this renderable wants. The default assumes the renderable fills the available width (e.g. Panel, Table); Text overrides it with its content width so the top-level print path can shrink to fit. Port of __rich_measure__ / Measurement.get.

Auto Trait Implementations§

§

impl Freeze for Json

§

impl RefUnwindSafe for Json

§

impl Send for Json

§

impl Sync for Json

§

impl Unpin for Json

§

impl UnsafeUnpin for Json

§

impl UnwindSafe for Json

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.