Available on crate feature
view only.Expand description
Pretty, colourless, and JSON output from one typed model.
A command result implements Present: it serializes for JSON and builds
a Document for people. A View holds the output format, the colour
mode, and the layout policy, and writes the result with View::show or
returns it unwritten with View::capture.
Success goes to stdout and failure to stderr, except JSON, which always goes to stdout. JSON never contains ANSI whatever the colour mode says.
use ctl_core::color::ColorMode;
use ctl_core::document::{Document, Fields};
use ctl_core::format::OutputFormat;
use ctl_core::view::{Destination, JsonLayout, Present, View};
use serde::Serialize;
#[derive(Serialize)]
struct Status {
pending: usize,
}
impl Present for Status {
fn present(&self) -> Document {
Document::new().fields(Fields::new().row("pending", self.pending.to_string()))
}
}
let status = Status { pending: 2 };
let json = View::new(OutputFormat::Json, ColorMode::Never)
.json_layout(JsonLayout::Pretty)
.capture(&status)?;
assert_eq!(json.stream(), Destination::Stdout);
assert!(json.text().contains("\"pending\": 2"));
let pretty = View::new(OutputFormat::Pretty, ColorMode::Never)
.width(40)
.capture(&status)?;
assert!(pretty.text().contains("pending"));Structs§
- Captured
- A rendered result that has not been written: its text, the stream it belongs on, and its exit code.
- View
- How to present a
Presentresult: the format, the colour mode, and the layout policy. JSON never contains ANSI.
Enums§
- Destination
- Where a view sends rendered output.
- Json
Layout - How JSON output is broken into lines. No layout adds ANSI.
- Outcome
- Whether a
Presentresult succeeded. It picks the stream pretty output goes to and the default exit code.
Constants§
- DEFAULT_
JSON_ LAYOUT_ ENV - The environment variable that sets the JSON layout when the CLI has not:
CTL_CORE_JSON_LAYOUT. Its values are theJsonLayoutnamespretty,compact, andpretty-on-terminal. - DEFAULT_
JSON_ LAYOUT_ ENVS - The ordered list of variables
View::resolved_json_layoutreads, holding onlyDEFAULT_JSON_LAYOUT_ENV.View::json_layout_envsreplaces it.
Traits§
- Present
- A command result that serializes for machines and presents itself to people.