import type { Command, CommandOptions, CommandResult } from '../types'; import { resolveCount, gatherInputs, toLines } from './_textlines'; export const headCommand: Command = { name: 'head', description: 'Output the first N lines of each file (default 10)', usage: 'head [-n N | -N] [file...]', async execute(args: string[], options: CommandOptions): Promise { const { stdout } = options; const { count, files } = resolveCount(options.flags, args); try { const inputs = await gatherInputs(files, options); const multi = inputs.length > 1; for (let i = 0; i < inputs.length; i++) { const picked = toLines(inputs[i].content).slice(0, count); if (multi) await stdout.write(`${i ? '\n' : ''}==> ${inputs[i].name} <==\n`); if (picked.length) await stdout.write(picked.join('\n') + '\n'); } return { exitCode: 0 }; } catch (error) { await stdout.write(`head: ${error instanceof Error ? error.message : String(error)}\n`); return { exitCode: 1 }; } }, };