|
| 1 | +// Terminal ASCII table — no external dependencies |
| 2 | + |
| 3 | +let noColor = false; |
| 4 | +export function setNoColor(v) { noColor = v; } |
| 5 | + |
| 6 | +const SEV_COLOR = { |
| 7 | + critical: '\x1b[35m', // magenta |
| 8 | + high: '\x1b[31m', // red |
| 9 | + medium: '\x1b[33m', // yellow |
| 10 | + low: '\x1b[34m', // blue |
| 11 | + info: '\x1b[37m', // white |
| 12 | + unknown: '\x1b[90m', // dark gray |
| 13 | +}; |
| 14 | +const RESET = '\x1b[0m'; |
| 15 | + |
| 16 | +function color(text, code) { |
| 17 | + if (noColor || !process.stdout.isTTY) return text; |
| 18 | + return `${code}${text}${RESET}`; |
| 19 | +} |
| 20 | + |
| 21 | +function pad(str, len) { |
| 22 | + const s = String(str ?? ''); |
| 23 | + return s.length >= len ? s.slice(0, len) : s + ' '.repeat(len - s.length); |
| 24 | +} |
| 25 | + |
| 26 | +const COLS = [ |
| 27 | + { key: 'severity', label: 'SEVERITY', width: 10 }, |
| 28 | + { key: 'category', label: 'CATEGORY', width: 17 }, |
| 29 | + { key: 'file_path', label: 'FILE', width: 40 }, |
| 30 | + { key: 'line_number', label: 'LINE', width: 6 }, |
| 31 | + { key: 'check_id', label: 'CHECK', width: 20 }, |
| 32 | + { key: 'message', label: 'MESSAGE', width: 60 }, |
| 33 | +]; |
| 34 | + |
| 35 | +export default { |
| 36 | + name: 'table', |
| 37 | + mime: 'text/plain', |
| 38 | + extension: '.txt', |
| 39 | + render(envelope) { |
| 40 | + const { findings = [], summary, errors = [] } = envelope; |
| 41 | + const lines = []; |
| 42 | + |
| 43 | + // Header |
| 44 | + const sep = COLS.map((c) => '-'.repeat(c.width)).join('-+-'); |
| 45 | + const header = COLS.map((c) => pad(c.label, c.width)).join(' | '); |
| 46 | + lines.push(sep); |
| 47 | + lines.push(header); |
| 48 | + lines.push(sep); |
| 49 | + |
| 50 | + for (const f of findings) { |
| 51 | + const sev = f.severity; |
| 52 | + const cols = COLS.map((c) => { |
| 53 | + let val = String(f[c.key] ?? ''); |
| 54 | + if (c.key === 'file_path' && val.length > c.width) { |
| 55 | + val = '…' + val.slice(-(c.width - 1)); |
| 56 | + } |
| 57 | + val = pad(val, c.width); |
| 58 | + if (c.key === 'severity') { |
| 59 | + val = color(val, SEV_COLOR[sev] ?? ''); |
| 60 | + } |
| 61 | + return val; |
| 62 | + }); |
| 63 | + lines.push(cols.join(' | ')); |
| 64 | + } |
| 65 | + |
| 66 | + lines.push(sep); |
| 67 | + lines.push(`Total: ${summary?.total ?? findings.length} ` + |
| 68 | + Object.entries(summary?.by_severity ?? {}) |
| 69 | + .filter(([, n]) => n > 0) |
| 70 | + .map(([s, n]) => color(`${s}:${n}`, SEV_COLOR[s] ?? '')) |
| 71 | + .join(' ') |
| 72 | + ); |
| 73 | + |
| 74 | + if (errors.length > 0) { |
| 75 | + lines.push(''); |
| 76 | + for (const e of errors) { |
| 77 | + lines.push(color(`[error] ${e.category}: ${e.error}`, '\x1b[31m')); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return lines.join('\n'); |
| 82 | + }, |
| 83 | +}; |
0 commit comments