pub struct Json { /* private fields */ }Expand description
A parsed JSON document, rendered with syntax highlighting. Mirrors rich.json.JSON.
Like upstream, the document is re-serialized (json.dumps) with the
JsonOptions and highlighted with JSONHighlighter when it is built;
text is that highlighted Text.
Implementations§
Source§impl Json
impl Json
Sourcepub fn new(text: &str) -> Result<Self>
pub fn new(text: &str) -> Result<Self>
Parse text as JSON with upstream’s default options. Returns an error
if it is not valid JSON.
Sourcepub fn with_options(text: &str, options: &JsonOptions) -> Result<Self>
pub fn with_options(text: &str, options: &JsonOptions) -> Result<Self>
Parse text as JSON and format it with options. Port of
rich.json.JSON(json, indent=…, highlight=…, …).
Sourcepub fn text(&self) -> Text
pub fn text(&self) -> Text
The formatted, highlighted document. Port of the JSON.text
attribute (upstream’s __rich__ returns it).
Sourcepub fn no_wrap(self, no_wrap: bool) -> Self
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 — or with the no_wrap and
overflow that print was given, which reach it as
ConsoleOptions and which this renderable honours while the flag is
off.
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
impl Renderable for Json
Source§fn measure(&self, _console: &Console, _options: &ConsoleOptions) -> Measurement
fn measure(&self, _console: &Console, _options: &ConsoleOptions) -> Measurement
Upstream JSON.__rich__ returns its highlighted Text, so measuring a
JSON is Text.__rich_measure__ over the formatted document.
Source§fn rich_render(
&self,
console: &Console,
options: &ConsoleOptions,
) -> Vec<Segment>
fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>
Text.__rich_console__ of the document: each line wraps (or, with
no_wrap, is truncated) to the width with the options’ justify and
overflow — the document’s own overflow is None.
Source§fn fit_to_measurement(&self) -> bool
fn fit_to_measurement(&self) -> bool
Console::print shrinks this renderable to its
measured width. Upstream renders every top-level renderable at the full
console width, so the default is false; an extension may opt in.