@agentled/cli
Version:
CLI for Agentled — manage workflows, apps, and knowledge from the command line. Zero context-window cost for AI agents.
56 lines • 2.3 kB
JavaScript
/**
* Output formatting utilities.
*
* Default: JSON (optimized for AI agent parsing)
* --format table: Human-readable tables
* --format minimal: Compact single-line output for piping
*/
export function formatOutput(data, format = 'json') {
switch (format) {
case 'json':
return JSON.stringify(data, null, 2);
case 'minimal':
if (Array.isArray(data)) {
return data.map(item => {
if (typeof item === 'object' && item !== null) {
return item.id || item.name || JSON.stringify(item);
}
return String(item);
}).join('\n');
}
if (typeof data === 'object' && data !== null) {
return Object.entries(data)
.map(([k, v]) => `${k}=${typeof v === 'object' ? JSON.stringify(v) : v}`)
.join('\n');
}
return String(data);
case 'table':
if (Array.isArray(data) && data.length > 0) {
const keys = Object.keys(data[0]);
const widths = keys.map(k => Math.max(k.length, ...data.map(row => String(row[k] ?? '').length)));
// Cap column widths
const maxWidth = 40;
const cappedWidths = widths.map(w => Math.min(w, maxWidth));
const header = keys.map((k, i) => k.padEnd(cappedWidths[i])).join(' ');
const separator = cappedWidths.map(w => '-'.repeat(w)).join(' ');
const rows = data.map(row => keys.map((k, i) => {
const val = String(row[k] ?? '');
return val.length > maxWidth
? val.slice(0, maxWidth - 3) + '...'
: val.padEnd(cappedWidths[i]);
}).join(' '));
return [header, separator, ...rows].join('\n');
}
return formatOutput(data, 'json');
default:
return JSON.stringify(data, null, 2);
}
}
export function printOutput(data, format = 'json') {
console.log(formatOutput(data, format));
}
export function printError(message) {
console.error(`Error: ${message}`);
process.exit(1);
}
//# sourceMappingURL=output.js.map