bktide
Version:
Command-line interface for Buildkite CI/CD workflows with rich shell completions (Fish, Bash, Zsh) and Alfred workflow integration for macOS power users
93 lines • 3 kB
JavaScript
/**
* Shared formatting utilities for formatters
*
* These utilities provide consistent formatting for status, dates, durations,
* sizes, and text truncation across all formatters.
*/
import { SEMANTIC_COLORS } from '../ui/theme.js';
/**
* Format a build/job status with icon and color
*/
export function formatStatus(state) {
if (!state)
return SEMANTIC_COLORS.dim('unknown');
const stateUpper = state.toUpperCase();
switch (stateUpper) {
case 'PASSED':
return SEMANTIC_COLORS.success('✓ passed');
case 'FAILED':
return SEMANTIC_COLORS.error('✖ failed');
case 'RUNNING':
return SEMANTIC_COLORS.info('↻ running');
case 'BLOCKED':
return SEMANTIC_COLORS.warning('⚠ blocked');
case 'CANCELED':
case 'CANCELLED':
return SEMANTIC_COLORS.dim('− canceled');
case 'SKIPPED':
return SEMANTIC_COLORS.dim('− skipped');
default:
return SEMANTIC_COLORS.dim(`− ${state.toLowerCase()}`);
}
}
/**
* Format a date as a relative time string
* Examples: "just now", "5m ago", "3h ago", "2d ago"
*/
export function formatRelativeDate(dateStr) {
const date = new Date(dateStr);
const now = new Date();
const diff = now.getTime() - date.getTime();
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (days > 0)
return `${days}d ago`;
if (hours > 0)
return `${hours}h ago`;
if (minutes > 0)
return `${minutes}m ago`;
return 'just now';
}
/**
* Format duration between two dates
* Examples: "45s", "5m 30s"
*/
export function formatDuration(startStr, endStr) {
const start = new Date(startStr);
const end = new Date(endStr);
const diff = end.getTime() - start.getTime();
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
if (minutes > 0) {
return `${minutes}m ${remainingSeconds}s`;
}
return `${seconds}s`;
}
/**
* Format byte size as human-readable string
* Examples: "500 B", "4.9 KB", "4.8 MB"
*/
export function formatSize(bytes) {
if (bytes < 1024)
return `${bytes} B`;
if (bytes < 1024 * 1024)
return `${(bytes / 1024).toFixed(1)} KB`;
if (bytes < 1024 * 1024 * 1024)
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
}
/**
* Truncate a string to a maximum length with ellipsis
* Also replaces newlines with spaces
*/
export function truncate(str, length) {
// Replace newlines with spaces first
const singleLine = str.replace(/\n+/g, ' ').trim();
if (singleLine.length <= length)
return singleLine;
return singleLine.slice(0, length - 3) + '...';
}
//# sourceMappingURL=formatUtils.js.map