Output
Handlers receive out instead of console. The output channel adapts to context automatically.
Basic Output
ts
import { command } from '@kjanat/dreamcli';
command('deploy').action(({ out }) => {
out.log('Informational message');
out.warn('Warning message');
out.error('Error message');
});JSON Output
ts
import { createOutput } from '@kjanat/dreamcli';
const out = createOutput();
out.json({ status: 'ok', count: 42 });When the CLI is invoked with --json, structured payloads stay on stdout while plain text (log, info, warn, error) routes to stderr.
Tables
ts
import { createOutput } from '@kjanat/dreamcli';
type Row = { name: string; status: string; uptime: number };
const rows = [
{ name: 'web-1', status: 'running', uptime: 72 },
{ name: 'worker-1', status: 'degraded', uptime: 18 },
];
const out = createOutput();
out.table<Row>(rows, [
{ key: 'name', header: 'Name' },
{ key: 'status', header: 'Status' },
{ key: 'uptime', header: 'Uptime (h)' },
]);Use a type alias (not interface) for table rows.
TypeScript's structural typing requires Record<string, unknown> compatibility.
Spinners
ts
import { createOutput } from '@kjanat/dreamcli';
const deploy = async () => {};
const out = createOutput();
const spinner = out.spinner('Deploying...');
await deploy();
spinner.succeed('Done');Spinners auto-disable when stdout is not a TTY (CI, piped output). In --json mode, spinners are suppressed entirely.
Progress Bars
ts
import { createOutput } from '@kjanat/dreamcli';
const tick = async () => {};
const out = createOutput();
const progress = out.progress({
label: 'Uploading',
total: 100,
});
for (let i = 0; i <= 100; i++) {
progress.update(i);
await tick();
}
progress.done('Upload complete');Output Modes
The output channel automatically adjusts behavior:
| Context | Behavior |
|---|---|
| TTY | Pretty formatting, spinners animate, colors |
| Piped | Minimal stable output, spinners suppressed |
--json | Structured JSON to stdout, everything else to stderr |
One code path, correct output everywhere.
What's Next?
- Related examples: JSON mode, Spinner and progress
- Errors — structured error handling
- Testing — capturing output in tests