UNPKG

@jawis/stdio-filter

Version:
77 lines (76 loc) 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeStdioInterpreter = void 0; const _jab_1 = require("^jab"); const internal_1 = require("./internal"); /** * Interpret stdio with ansi codes into lines. * * - onStdio callback is called with individual lines, making it easy to handle the stdio stream. * - newlines are not included in the lines given to onStdio. * - ANSI codes are interpreted, so only styling is emitted in the lines. * - ANSI codes can result in updates to lines. In that case the full line is emitted again. * - Carriage return is filtered out, because it has special meaning in the console. * - Lines are zero index'ed. * * todo * drop lines, when buffer becomes too big. * */ const makeStdioInterpreter = (onLine) => { let indexX = 0; let indexY = 0; const buffer = []; const parser = (0, internal_1.makeAnsiParser)({ onPrint: (str) => { var _a; (0, _jab_1.assert)(str.indexOf("\n") === -1); const prev = (_a = buffer[indexY]) !== null && _a !== void 0 ? _a : ""; buffer[indexY] = prev.slice(0, indexX) + str + prev.slice(indexX + str.length); indexX += str.length; onLine(indexY, buffer[indexY], buffer); }, onAction: (action, _count) => { switch (action) { case "up": case "down": case "right": case "left": case "up-beginning": throw new Error("not impl"); case "down-beginning": indexX = 0; indexY++; if (buffer[indexY] === undefined) { buffer[indexY] = ""; } onLine(indexY, buffer[indexY], buffer); break; default: return (0, _jab_1.assertNever)(action); } }, onAction2: (action) => { switch (action) { case "erase-to-beginning": buffer[indexY] = buffer[indexY].slice(indexX); //todo: + || - 1 ?? indexX = 0; onLine(indexY, buffer[indexY], buffer); break; default: return (0, _jab_1.assertNever)(action); } }, setPosition: (cursorX, cursorY) => { indexX = cursorX - 1; if (cursorY !== undefined) { indexY = cursorY - 1; } }, }); return (str) => { parser.parse(str); }; }; exports.makeStdioInterpreter = makeStdioInterpreter;