Skip to main content

OutputConfig

Struct OutputConfig 

Source
pub struct OutputConfig {
    pub format: OutputFormat,
    pub quiet: bool,
    pub no_color: bool,
    pub use_color: bool,
    pub verbose: bool,
    pub raw: bool,
    pub no_interactive: bool,
}
Expand description

Output configuration threaded through command handlers.

OutputConfig is intentionally a pure data carrier — no I/O handles, no interior mutability. This keeps it Send + Sync + Clone, which is required for the planned async/concurrent ApiClient (see project_async_requirement).

use_color is the resolved color decision after combining --color, the NO_COLOR env var, and stderr’s TTY-ness. no_color is preserved as the negation (!use_color) for source-compatibility with existing call sites and tests that constructed OutputConfig { … } directly.

raw (from --raw) forces compact JSON (no pretty-printing) and strips ANSI styling from text output. Useful for pipelines that line-buffer.

§Example

use xurl::cli::ColorChoice;
use xurl::output::{OutputConfig, OutputFormat};

let cfg = OutputConfig::new(OutputFormat::Json, false, false, ColorChoice::Never);

let mut buf: Vec<u8> = Vec::new();
let payload = serde_json::json!({ "status": "ok" });
cfg.print_response(&mut buf, &payload);

let rendered = String::from_utf8(buf).unwrap();
assert!(rendered.contains("\"status\""));

Fields§

§format: OutputFormat

Resolved output format from --output / --json / --jsonl / XURL_OUTPUT.

§quiet: bool

Suppress non-essential human chatter (--quiet / XURL_QUIET).

§no_color: bool

Negation of Self::use_color, kept for source compatibility with call sites that pattern-match on the negative form.

§use_color: bool

Resolved color decision after combining --color, NO_COLOR, and stderr’s TTY-ness. The single source of truth for “should I emit ANSI escapes”.

§verbose: bool

Enable verbose request/response logging (--verbose / -v / XURL_VERBOSE).

§raw: bool

Emit unstyled, compact output (--raw / XURL_RAW). Strips ANSI in text mode and forces compact JSON in machine modes.

§no_interactive: bool

Set when the user passed --no-interactive (or XURL_NO_INTERACTIVE).

Routed into OutputConfig so dialoguer-gating call sites can ask Self::is_interactive_terminal without re-reading the parsed Cli struct. Constructors default this to false; the runner sets it via Self::with_no_interactive right after construction.

Implementations§

Source§

impl OutputConfig

Source

pub fn new( format: OutputFormat, quiet: bool, verbose: bool, color: ColorChoice, ) -> Self

Creates an OutputConfig from resolved CLI flags and environment.

use_color is computed from color together with NO_COLOR and std::io::stderr().is_terminal():

  • NO_COLOR is absolute (per https://no-color.org/): when set, color is disabled regardless of --color.
  • --color always overrides the TTY check (still loses to NO_COLOR).
  • --color never disables color unconditionally.
  • --color auto enables color only when stderr is a TTY.

raw forces use_color = false and switches JSON output to compact form (no pretty-printing).

Source

pub fn new_with_raw( format: OutputFormat, quiet: bool, verbose: bool, color: ColorChoice, raw: bool, ) -> Self

Like new, with an explicit raw flag.

Source

pub fn with_no_interactive(self, no_interactive: bool) -> Self

Returns a copy of this config with the no_interactive field set.

Used by the runner immediately after construction to thread the parsed --no-interactive (or XURL_NO_INTERACTIVE) flag into Self::is_interactive_terminal.

Source

pub fn is_interactive_terminal(&self) -> bool

Returns true when the active session can drive interactive prompts.

True only when:

  • --no-interactive is NOT set,
  • stdin is a TTY,
  • stderr is a TTY (dialoguer renders prompts on stderr).

Call sites that drive dialoguer::Select / dialoguer::Confirm MUST gate on this — auto-engaging a prompt under a non-TTY session leaves the dialoguer state machine waiting on /dev/null.

Source

pub fn print_error_envelope( &self, err: &mut dyn Write, reason: &str, exit_code: i32, message: &str, )

Emits a canonical error envelope with an explicit kebab-case reason.

Mirrors Self::print_error but lets the caller pin the reason (e.g. "no-tty") rather than reading it from XurlError::kind(). Under text mode falls back to a plain “Error: …” line.

Source

pub fn info(&self, err: &mut dyn Write, msg: &str)

Prints an informational message (suppressed by –quiet or any structured --output mode).

The runner passes a stderr writer here in the binary path; tests pass a Vec<u8>.

Source

pub fn status(&self, err: &mut dyn Write, msg: &str)

Prints a success/status message with optional color.

The runner passes a stderr writer here in the binary path; tests pass a Vec<u8>.

Source

pub fn print_response(&self, out: &mut dyn Write, value: &Value)

Prints an API response according to the configured output format.

I/O errors are intentionally swallowed (best-effort posture) so a closed downstream pipe doesn’t abort the program — the SIGPIPE restoration in main handles the more general case.

Under --raw, JSON output is emitted compactly (one line, no whitespace) rather than pretty-printed.

Source

pub fn print_stream_line(&self, out: &mut dyn Write, line: &str)

Prints a streaming line according to the configured output format.

Source

pub fn print_error( &self, err: &mut dyn Write, error: &XurlError, exit_code: i32, )

Formats and prints an error to the supplied stderr writer. Under any structured --output, emits the canonical envelope shape: {"status":"error","reason":<kind>,"exit_code":<code>,"message":<display>}. Json/Jsonl/Ndjson emit one JSON line; Yaml emits a YAML document; Csv/Tsv emit a JSON line carrying the envelope (delimited formats are not a good fit for nested error metadata).

Source

pub fn print_success(&self, out: &mut dyn Write, payload: &Value)

Emits a canonical success envelope under structured modes.

Wraps payload (treated as a JSON object whose keys flatten in at the top level) with {"status":"ok", ...payload}. Under text mode the payload is passed through to Self::print_response so existing formatters keep their shape.

Source

pub fn print_dry_run( &self, out: &mut dyn Write, would_succeed: bool, exit_code: i32, ctx: &Value, )

Emits a canonical dry-run envelope under structured modes.

Shape: {"status":"dry_run","would_succeed":<bool>,"exit_code":<int>, ...ctx}. Under text mode, falls back to a pass-through of ctx.

Source

pub fn print_confirmation_required( &self, err: &mut dyn Write, ctx: &Value, exit_code: i32, )

Prints a canonical confirmation-required error envelope (U7).

Emitted when a destructive op was invoked under --no-interactive without --force. ctx carries verb-context fields; the helper folds status: "error", reason: "confirmation-required", and exit_code into the same object on stderr.

Source

pub fn verbose(&self, err: &mut dyn Write, msg: &str)

Emits a verbose diagnostic line to err when verbose is on, quiet is off, and the format is text.

Under --output json or --output jsonl, agents parsing structured output must not encounter interleaved human text on stderr, so verbose is suppressed (per the agent-native semantic-fields-over- stderr-warnings principle). Mirrors the diag! macro pattern from the bird CLI without the macro.

Source

pub fn warning(&self, err: &mut dyn Write, msg: &str)

Emits a warning to err. Always goes to stderr in text mode; under JSON modes the line is suppressed (the canonical envelope is the channel for structured warnings — see plan U8’s deferred warnings: [] envelope promotion).

Suppressed entirely under --quiet combined with JSON modes; under --quiet text mode, warnings still surface (errors and warnings are the load-bearing signals the operator must see).

Source

pub fn progress(&self, err: &mut dyn Write, msg: &str)

Emits a progress / status line to err when the format is text and stderr is a TTY. Quiet suppresses progress unconditionally.

Source

pub fn print_message(&self, out: &mut dyn Write, msg: &str)

Prints a simple text message (e.g. version, auth status) to the supplied writer. Respects –output json by wrapping in a JSON object.

Trait Implementations§

Source§

impl Clone for OutputConfig

Source§

fn clone(&self) -> OutputConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OutputConfig

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for OutputConfig

Source§

fn default() -> Self

Library-friendly default: text format, color-auto, no verbose, no quiet, no raw. Matches what an interactive operator gets without flags. Used when an ApiClient is constructed before the runner has resolved the real OutputConfig.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more