import type { Command, CommandOptions, CommandResult } from '../types'; import { resolveCount, gatherInputs, toLines } from './_textlines'; export const tailCommand: Command = { name: 'tail', description: 'Output the last N lines of each file (default 10)', usage: 'tail [-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 lines = toLines(inputs[i].content); const picked = count <= 0 ? [] : lines.slice(-count); // slice(-0) would return everything 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(`tail: ${error instanceof Error ? error.message : String(error)}\n`); return { exitCode: 1 }; } }, };