import { Buffer } from "../buffer.ts";import { clearLine, clearScreenDown, cursorTo, moveCursor,} from "../internal/readline/callbacks.mjs";import { Duplex, Readable, Writable } from "../stream.ts";import { stdio } from "./stdio.mjs";import { fs as fsConstants } from "../internal_binding/constants.ts";
function createWritableStdioStream(writer, name) { const stream = new Writable({ write(buf, enc, cb) { if (!writer) { this.destroy( new Error(`Deno.${name} is not available in this environment`), ); return; } writer.writeSync(buf instanceof Uint8Array ? buf : Buffer.from(buf, enc)); cb(); }, destroy(err, cb) { cb(err); this._undestroy(); if (!this._writableState.emitClose) { nextTick(() => this.emit("close")); } }, }); stream.fd = writer?.rid ?? -1; stream.destroySoon = stream.destroy; stream._isStdio = true; stream.once("close", () => writer?.close()); Object.defineProperties(stream, { columns: { enumerable: true, configurable: true, get: () => Deno.isatty?.(writer?.rid) ? Deno.consoleSize?.().columns : undefined, }, rows: { enumerable: true, configurable: true, get: () => Deno.isatty?.(writer?.rid) ? Deno.consoleSize?.().rows : undefined, }, isTTY: { enumerable: true, configurable: true, get: () => Deno.isatty?.(writer?.rid), }, getWindowSize: { enumerable: true, configurable: true, value: () => Deno.isatty?.(writer?.rid) ? Object.values(Deno.consoleSize?.()) : undefined, }, });
if (Deno.isatty?.(writer?.rid)) { stream.cursorTo = function (x, y, callback) { return cursorTo(this, x, y, callback); };
stream.moveCursor = function (dx, dy, callback) { return moveCursor(this, dx, dy, callback); };
stream.clearLine = function (dir, callback) { return clearLine(this, dir, callback); };
stream.clearScreenDown = function (callback) { return clearScreenDown(this, callback); }; }
return stream;}
export const stderr = stdio.stderr = createWritableStdioStream( Deno.stderr, "stderr",);
export const stdout = stdio.stdout = createWritableStdioStream( Deno.stdout, "stdout",);
function _guessStdinType(fd) { if (typeof fd !== "number" || fd < 0) return "UNKNOWN"; if (Deno.isatty?.(fd)) return "TTY";
try { const fileInfo = Deno.fstatSync?.(fd);
if (Deno.build.os !== "windows") { switch (fileInfo.mode & fsConstants.S_IFMT) { case fsConstants.S_IFREG: case fsConstants.S_IFCHR: return "FILE"; case fsConstants.S_IFIFO: return "PIPE"; case fsConstants.S_IFSOCK: return "TCP"; default: return "UNKNOWN"; } }
if (fileInfo.isFile) { if (fileInfo.birthtime.valueOf() === 11644473600000) return "PIPE"; return "FILE"; } } catch (e) { if (Deno.build.os === "windows" && e.code === "EISDIR") return "FILE"; }
return "UNKNOWN";}
const _read = function (size) { const p = Buffer.alloc(size || 16 * 1024); Deno.stdin?.read(p).then((length) => { this.push(length === null ? null : p.slice(0, length)); }, (error) => { this.destroy(error); });};
export const stdin = stdio.stdin = (() => { const fd = Deno.stdin?.rid; let _stdin; const stdinType = _guessStdinType(fd);
switch (stdinType) { case "FILE": { _stdin = new Readable({ highWaterMark: 64 * 1024, autoDestroy: false, read: _read, }); break; } case "TTY": case "PIPE": case "TCP": {
_stdin = new Duplex({ readable: stdinType === "TTY" ? undefined : true, writable: stdinType === "TTY" ? undefined : false, readableHighWaterMark: stdinType === "TTY" ? 0 : undefined, allowHalfOpen: false, emitClose: false, autoDestroy: true, decodeStrings: false, read: _read, });
if (stdinType !== "TTY") { _stdin._writableState.ended = true; } break; } default: { _stdin = new Readable({ read() {} }); _stdin.push(null); } }
return _stdin;})();stdin.on("close", () => Deno.stdin?.close());stdin.fd = Deno.stdin?.rid ?? -1;Object.defineProperty(stdin, "isTTY", { enumerable: true, configurable: true, get() { return Deno.isatty?.(Deno.stdin.rid); },});stdin._isRawMode = false;stdin.setRawMode = (enable) => { Deno.stdin?.setRaw?.(enable); stdin._isRawMode = enable; return stdin;};Object.defineProperty(stdin, "isRaw", { enumerable: true, configurable: true, get() { return stdin._isRawMode; },});