export type Container = { id: string image: string command: string created: string status: string ports: string name: string } // Parse the fixed-width table output of `docker ps --format table` export function parseDockerPs(output: string): Container[] { const lines = output.trim().split('\n').filter(Boolean) if (lines.length < 2) return [] const header = lines[0] const COLS = ['CONTAINER ID', 'IMAGE', 'COMMAND', 'CREATED', 'STATUS', 'PORTS', 'NAMES'] as const const positions = COLS.map((col) => header.indexOf(col)) // Must find at least NAMES (last) to produce useful output if (positions[6] === -1) return [] const extract = (line: string, i: number): string => { const start = positions[i] if (start === -1) return '' const nextIdx = positions.slice(i + 1).find((p) => p !== -1) return line.slice(start, nextIdx).trim() } return lines.slice(1).map((line) => ({ id: extract(line, 0), image: extract(line, 1), command: extract(line, 2), created: extract(line, 3), status: extract(line, 4), ports: extract(line, 5), name: extract(line, 6), })) }