// Purpose: JavaScript host effects for run.bend: Sys.isatty, Sys.columns and // Sys.has_env (ยง7.3), for -o x.js builds and `bend x.bend`. // Expected output: none; it registers effects. // Run: bend main.bend -o build/pkg.js // Only a character device can be a terminal: testing that first spares // loading tty, and process.stdout's setup, about 3.5 ms under Bun, when // output goes to a file or pipe. function sys_tty(fd) { let chr = false; try { chr = require("fs").fstatSync(fd).isCharacterDevice(); } catch (_) { } return chr && require("tty").isatty(fd); } function sys_isatty(fd) { return sys_tty(fd) ? 1 : 0; } // 0 when fd is not a terminal or reports no size. function sys_columns(fd) { const stream = !sys_tty(fd) ? null : fd === 1 ? process.stdout : fd === 2 ? process.stderr : null; return (stream && stream.columns) || 0; } // 1 if the variable is set; a name IO.get_env rejects, with a NUL or an // =, is not. function sys_has_env(name) { name = str_out(name); return !name.includes("\0") && !name.includes("=") && Object.hasOwn(process.env, name) ? 1 : 0; } io_eff(CID(Sys.isatty), sys_isatty); io_eff(CID(Sys.columns), sys_columns); io_eff(CID(Sys.has_env), sys_has_env);