diff options
| author | Lily <[email protected]> | 2026-07-28 16:12:21 +1000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-07-28 16:12:21 +1000 |
| commit | c11e17573334dfb487a8e1cc25fa84fa9e138f5c (patch) | |
| tree | e91dbd19b524f9432da6bcb86b1fc193e0d02e26 /src/commands/tty.js | |
| parent | 6a2f098e72e71437154ec00506e8e736aa8e1026 (diff) | |
| download | bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.tar.gz bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.zip | |
Refactor coreutils into single-call command modules (#1)
* Refactor to single call
Diffstat (limited to 'src/commands/tty.js')
| -rw-r--r-- | src/commands/tty.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/commands/tty.js b/src/commands/tty.js new file mode 100644 index 0000000..6df436c --- /dev/null +++ b/src/commands/tty.js @@ -0,0 +1,68 @@ +#!/usr/bin/env bun + +import { localeQuotedEscapedDiagnostic, parseOptions } from "../shared/common.js"; +import { UsageError, fail, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const TTY_LONG_OPTIONS = ["silent", "quiet", "help", "version"]; + +export function ttyMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (!arg.startsWith("--")) { + for (const ch of arg.slice(1)) { + if (ch !== "s") return null; + } + continue; + } + const option = normalizeTtyLongOption(arg); + if (option.includes("=") || !TTY_LONG_OPTIONS.includes(option.slice(2))) return null; + if (option === "--help" || option === "--version") return option; + } + return null; +} + +export async function ttyCmd(args) { + args = normalizeTtyLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { s: false }, long: { silent: false, quiet: false, help: false, version: false } }); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + const isTty = Boolean(process.stdin.isTTY); + if (!(opts.s || opts.silent || opts.quiet)) { + try { + stdout(isTty ? "/dev/tty\n" : "not a tty\n"); + } catch (error) { + return fail("tty", error.message || String(error), 3); + } + } + return isTty ? 0 : 1; +} + +export function normalizeTtyLongOptions(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(normalizeTtyLongOption(arg)); + } + return out; +} + +export function normalizeTtyLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const match = TTY_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +const singleCall = defineCommand("tty", ttyCmd, ttyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); |
