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
impl Output
Sourcepub fn new<W: Write + Send + 'static>(
format: OutputFormat,
writer: W,
no_color: bool,
) -> Self
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 usewriter- 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);Sourcepub fn format(&self) -> OutputFormat
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);Sourcepub fn no_color(&self) -> bool
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());Sourcepub fn success(&self, message: &str) -> Result<()>
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();Sourcepub fn error(&self, message: &str) -> Result<()>
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();Sourcepub fn warning(&self, message: &str) -> Result<()>
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();Sourcepub fn info(&self, message: &str) -> Result<()>
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();Sourcepub fn plain(&self, message: &str) -> Result<()>
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();Sourcepub fn json<T: Serialize>(&self, data: &T) -> Result<()>
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 indentationJsonCompact: Single line, no whitespaceHuman/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();Sourcepub fn table(&self, table: &mut Table) -> Result<()>
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();Sourcepub fn write_raw(&self, data: &[u8]) -> Result<()>
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();Sourcepub fn blank_line(&self) -> Result<()>
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();Sourcepub fn flush(&self) -> Result<()>
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();