sandlot/src/fmt.ts

38 lines
1.3 KiB
TypeScript

// ── ANSI escape codes ───────────────────────────────────────────────
export const reset = "\x1b[0m"
export const dim = "\x1b[2m"
export const green = "\x1b[32m"
export const yellow = "\x1b[33m"
export const cyan = "\x1b[36m"
// ── Formatted output ────────────────────────────────────────────────
export function die(message: string): never {
process.stderr.write(`${message}\n`)
process.exit(1)
}
export function success(message: string) {
process.stderr.write(`${message}\n`)
}
export function info(message: string) {
process.stderr.write(`${message}\n`)
}
// ── Pager ───────────────────────────────────────────────────────────
export async function pager(content: string): Promise<void> {
const lines = content.split("\n").length
const termHeight = process.stdout.rows || 24
if (lines > termHeight) {
const p = Bun.spawn(["less", "-R"], { stdin: "pipe", stdout: "inherit", stderr: "inherit" })
p.stdin.write(content)
p.stdin.end()
await p.exited
} else {
process.stdout.write(content)
}
}