Files
shakespeare/src/lib/commands/tail.ts
T
Alex Gleason 376a982449 Overhaul shell commands for POSIX compliance and AI-agent ergonomics
Each built-in shell command has been audited and rewritten to support the
options AI agents (and humans) typically reach for, close critical behavior
gaps, and align with POSIX/GNU conventions:

- Shared argument parser (utils.ts) handles combined short flags, long
  options with '=VALUE' or space-separated values, the '--' sentinel,
  numeric shortcuts like 'head -5', and '-' for stdin.
- cat, ls, cp, mv, rm, mkdir, touch: added missing common flags; removed
  the legacy 'absolute paths are not supported' and 'reading from stdin
  is not supported' barriers where they made agent use awkward.
- mv now overwrites destinations by default (POSIX) and supports -n.
- touch updates mtime on existing files (by rewriting content) and
  accepts -c/-a/-m/-h/-r/-d/-t options.
- find: new expression engine with -iname, -path, -regex, -type,
  -empty, -size, -maxdepth, -mindepth, -not, -or, -print/-print0,
  -delete, -quit.
- grep: -v, -c, -l, -L, -w, -x, -o, -q, -E, -F, -G, -H, -h, -A/-B/-C
  context, --include/--exclude, multiple -e, -f scriptfile.
- head/tail: -c bytes, -NUM shorthand, negative/positive line counts,
  proper multi-file headers.
- sort: -k keys, -t separator, -o output, -f/-b/-d/-s, -h/-V/-M modes.
- uniq: -i, -f, -s, -w, -D, and the [input [output]] form.
- wc: -m characters and -L longest line; -c now counts bytes (UTF-8).
- cut: open-ended ranges (3-, -5), -s, --complement, --output-delimiter.
- tr: -c/-s/-t, [:class:] character classes, escape sequences.
- sed: real multi-command parser supporting ';', addresses (N, $,
  /re/, N,M), '!' negation, proper -n quiet mode, y///, and -i[SUFFIX].
- echo: -n, -e, -E with standard backslash escapes.
- pwd/cd: -L/-P, plus cd -, cd ~, cd $HOME with OLDPWD tracking.
- diff: proper LCS algorithm producing correct normal and unified
  output; adds -q, -s, -N, -i, -w, -b, -B.
- env: -i, -u, NAME=VAL assignments; reads the live shell environment.
- date: -u, -d, -R, -I, and a full GNU-subset format specifier set.

Tests for each command were updated to reflect the corrected behavior
(e.g. absolute paths and stdin are now accepted, POSIX error messages).
All 1334 tests pass; typecheck and production build succeed.
2026-04-26 23:58:13 -05:00

152 lines
5.3 KiB
TypeScript

import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
import { createSuccessResult, createErrorResult } from "./ShellCommand";
import { classifyFsError, parseOptions, resolvePath } from "./utils";
/**
* Implementation of the 'tail' command.
*
* Supported options:
* -n NUM, --lines=NUM Print last NUM lines (default 10). `-n +K` prints
* starting from line K (1-indexed).
* -c NUM, --bytes=NUM Print last NUM bytes. `-c +K` starts from byte K.
* -q, --quiet, --silent Suppress headers when multiple files
* -v, --verbose Always print headers
* -NUM Shorthand for -n NUM
* -- End of options
* - Read from stdin
*/
export class TailCommand implements ShellCommand {
name = 'tail';
description = 'Display the last lines of files';
usage = 'tail [-n NUM] [-c NUM] [-qv] [--] [file...]';
private fs: JSRuntimeFS;
constructor(fs: JSRuntimeFS) {
this.fs = fs;
}
async execute(args: string[], cwd: string, input?: string): Promise<ShellCommandResult> {
const parsed = parseOptions(args, {
booleanShort: ['q', 'v'],
valueShort: ['n', 'c'],
booleanLong: ['quiet', 'silent', 'verbose'],
valueLong: ['lines', 'bytes'],
longToShort: {
quiet: 'q', silent: 'q', verbose: 'v',
lines: 'n', bytes: 'c',
},
}, { enableNumericShortcut: true });
if (parsed.unknown.length > 0) {
return createErrorResult(`${this.name}: invalid option -- '${parsed.unknown[0].replace(/^-+/, '')}'`);
}
const linesArg = parsed.values.get('n');
const bytesArg = parsed.values.get('c');
let lineCount = 10;
let linesFromStart = false; // -n +K: start from line K
if (linesArg !== undefined) {
const parsedLines = parseCountArg(linesArg);
if (!parsedLines) return createErrorResult(`${this.name}: invalid number of lines: '${linesArg}'`);
lineCount = parsedLines.n;
linesFromStart = parsedLines.fromStart;
}
let byteCount: number | undefined;
let bytesFromStart = false;
if (bytesArg !== undefined) {
const parsedBytes = parseCountArg(bytesArg);
if (!parsedBytes) return createErrorResult(`${this.name}: invalid number of bytes: '${bytesArg}'`);
byteCount = parsedBytes.n;
bytesFromStart = parsedBytes.fromStart;
}
const quiet = parsed.flags.has('q');
const verbose = parsed.flags.has('v');
const files = parsed.operands.length > 0 ? parsed.operands : ['-'];
const outputs: string[] = [];
const showHeaders = verbose || (!quiet && files.length > 1);
for (let idx = 0; idx < files.length; idx++) {
const filePath = files[idx];
let content: string;
if (filePath === '-') {
content = input ?? '';
} else {
try {
const absolutePath = resolvePath(filePath, cwd);
const stats = await this.fs.stat(absolutePath);
if (stats.isDirectory()) {
return createErrorResult(`${this.name}: error reading '${filePath}': Is a directory`);
}
content = await this.fs.readFile(absolutePath, 'utf8');
} catch (error) {
const { kind } = classifyFsError(error);
if (kind === 'ENOENT') {
return createErrorResult(`${this.name}: cannot open '${filePath}' for reading: No such file or directory`);
}
if (kind === 'EACCES') {
return createErrorResult(`${this.name}: cannot open '${filePath}' for reading: Permission denied`);
}
return createErrorResult(`${this.name}: ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
if (showHeaders) {
if (idx > 0) outputs.push('');
outputs.push(`==> ${filePath} <==`);
}
if (byteCount !== undefined) {
const result = bytesFromStart
? content.slice(Math.max(0, byteCount - 1))
: content.slice(Math.max(0, content.length - byteCount));
outputs.push(result);
} else {
const lines = content.split('\n');
// If file ends with newline, split produces a trailing empty string.
if (lines.length > 0 && lines[lines.length - 1] === '' && content.endsWith('\n')) {
lines.pop();
}
const selected = linesFromStart
? lines.slice(Math.max(0, lineCount - 1))
: lines.slice(-lineCount);
outputs.push(selected.join('\n') + (selected.length > 0 ? '\n' : ''));
}
}
return createSuccessResult(normalizeOutput(outputs));
}
}
/** Parse `-n NUM` or `-n +NUM` / `-n -NUM` arguments. */
function parseCountArg(s: string): { n: number; fromStart: boolean } | undefined {
if (s.startsWith('+')) {
if (!/^\+\d+$/.test(s)) return undefined;
return { n: parseInt(s.slice(1), 10), fromStart: true };
}
if (!/^-?\d+$/.test(s)) return undefined;
const raw = parseInt(s, 10);
return { n: Math.abs(raw), fromStart: false };
}
function normalizeOutput(chunks: string[]): string {
let out = '';
for (let i = 0; i < chunks.length; i++) {
const c = chunks[i];
if (c === '' && i > 0) {
if (!out.endsWith('\n')) out += '\n';
out += '\n';
continue;
}
if (i > 0 && !out.endsWith('\n')) out += '\n';
out += c;
}
return out;
}