Output

Struct Output 

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

Main output handler for CLI commands.

Provides consistent formatting across all commands with support for multiple output formats (human-readable, JSON, quiet). All output goes to stdout, while logs go to stderr independently.

§Examples

Basic usage:

use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.success("Operation completed").unwrap();
output.info("Processing 3 files").unwrap();
output.warning("Deprecated option used").unwrap();

JSON output:

use sublime_cli_tools::output::{Output, OutputFormat, JsonResponse};
use std::io;
use serde::Serialize;

#[derive(Serialize)]
struct Result {
    count: usize,
}

let output = Output::new(OutputFormat::Json, io::stdout(), false);
let response = JsonResponse::success(Result { count: 42 });
output.json(&response).unwrap();

Implementations§

Source§

impl Output

Source

pub fn new<W: Write + Send + 'static>( format: OutputFormat, writer: W, no_color: bool, ) -> Self

Creates a new Output instance.

§Arguments
  • format - The output format to use
  • writer - The writer to output to (typically stdout)
  • no_color - Whether to disable color output
§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
Source

pub fn format(&self) -> OutputFormat

Returns the current output format.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Json, io::stdout(), false);
assert_eq!(output.format(), OutputFormat::Json);
Source

pub fn no_color(&self) -> bool

Returns whether color output is disabled.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), true);
assert!(output.no_color());
Source

pub fn success(&self, message: &str) -> Result<()>

Outputs a success message.

In human mode, this is displayed with a green checkmark. In JSON mode, this is ignored (use json() instead). In quiet mode, this is suppressed.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.success("Configuration saved successfully").unwrap();
Source

pub fn error(&self, message: &str) -> Result<()>

Outputs an error message.

In human mode, this is displayed with a red X. In JSON mode, this is ignored (use json() with error response). In quiet mode, this is still displayed as errors are critical.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.error("Failed to read configuration file").unwrap();
Source

pub fn warning(&self, message: &str) -> Result<()>

Outputs a warning message.

In human mode, this is displayed with a yellow warning symbol. In JSON mode, this is ignored (use json() instead). In quiet mode, this is suppressed.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.warning("Deprecated configuration option detected").unwrap();
Source

pub fn info(&self, message: &str) -> Result<()>

Outputs an informational message.

In human mode, this is displayed with a blue info symbol. In JSON mode, this is ignored (use json() instead). In quiet mode, this is suppressed.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.info("Found 3 packages in workspace").unwrap();
Source

pub fn plain(&self, message: &str) -> Result<()>

Outputs plain text without formatting.

This is useful for outputting raw data or content that should not be modified. In JSON mode, this is ignored.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.plain("Package: @org/core").unwrap();
output.plain("Version: 1.2.3").unwrap();
Source

pub fn json<T: Serialize>(&self, data: &T) -> Result<()>

Outputs data as JSON.

This is the primary method for outputting structured data in JSON mode. The data is serialized according to the current output format:

  • Json: Pretty-printed with indentation
  • JsonCompact: Single line, no whitespace
  • Human/Quiet: Ignored (use other methods for human output)
§Examples
use sublime_cli_tools::output::{Output, OutputFormat, JsonResponse};
use std::io;
use serde::Serialize;

#[derive(Serialize)]
struct MyData {
    packages: Vec<String>,
    count: usize,
}

let output = Output::new(OutputFormat::Json, io::stdout(), false);
let data = MyData {
    packages: vec!["@org/core".to_string()],
    count: 1,
};
let response = JsonResponse::success(data);
output.json(&response).unwrap();
Source

pub fn table(&self, table: &mut Table) -> Result<()>

Renders and outputs a table.

In human mode, displays a formatted table. In JSON mode, this is ignored (use json() with structured data instead). In quiet mode, this is suppressed.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat, table::TableBuilder};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
let mut table = TableBuilder::new()
    .columns(&["Package", "Version"])
    .build();
table.add_row(&["typescript", "5.3.3"]);
output.table(&mut table).unwrap();
Source

pub fn write_raw(&self, data: &[u8]) -> Result<()>

Writes raw bytes to the output stream.

This is a low-level method that bypasses all formatting. Use with caution and prefer the higher-level methods when possible.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.write_raw(b"Raw data\n").unwrap();
Source

pub fn blank_line(&self) -> Result<()>

Outputs a blank line.

In human mode, outputs a blank line for spacing. In JSON mode, this is ignored. In quiet mode, this is suppressed.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.info("First section").unwrap();
output.blank_line().unwrap();
output.info("Second section").unwrap();
Source

pub fn flush(&self) -> Result<()>

Flushes the output buffer.

Ensures all buffered output is written to the underlying stream.

§Examples
use sublime_cli_tools::output::{Output, OutputFormat};
use std::io;

let output = Output::new(OutputFormat::Human, io::stdout(), false);
output.info("Processing...").unwrap();
output.flush().unwrap();

Auto Trait Implementations§

§

impl !Freeze for Output

§

impl !RefUnwindSafe for Output

§

impl Send for Output

§

impl !Sync for Output

§

impl Unpin for Output

§

impl !UnwindSafe for Output

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> 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: 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: 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, 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