textui/ansi.test.ts

41 lines
1.5 KiB
TypeScript

import { AnsiTerminalDisplay } from "./ansi.ts";
import { Buffer, describe, expect, it } from "./testing.ts";
describe(AnsiTerminalDisplay, () => {
it("clears the screen", async () => {
const stdout = new Buffer();
const display = new AnsiTerminalDisplay(stdout);
await display.clear();
checkSequence(stdout, "![2J");
});
it("writes colored characters", async () => {
const stdout = new Buffer();
const display = new AnsiTerminalDisplay(stdout);
await display.setupPalette([
{ r: 0.0, g: 0.0, b: 0.0 },
{ r: 0.5, g: 0.1, b: 1.0 },
]);
await display.setChar({ x: 0, y: 0 }, { ch: "$", fg: 1, bg: 0 });
checkSequence(stdout, "![38;2;128;26;255m![48;2;0;0;0m![1;1H$");
});
it("moves the cursor only when needed", async () => {
const stdout = new Buffer();
const display = new AnsiTerminalDisplay(stdout);
display.forceSize({ w: 4, h: 3 });
await display.setChar({ x: 0, y: 0 }, { ch: "a", fg: 0, bg: 0 });
await display.setChar({ x: 1, y: 0 }, { ch: "b", fg: 0, bg: 0 });
await display.setChar({ x: 3, y: 0 }, { ch: "c", fg: 0, bg: 0 });
await display.setChar({ x: 0, y: 1 }, { ch: "d", fg: 0, bg: 0 });
await display.setChar({ x: 0, y: 2 }, { ch: "e", fg: 0, bg: 0 });
checkSequence(stdout, "![1;1Hab![1;4Hcd![3;1He");
});
});
function checkSequence(buffer: Buffer, expected: string) {
const decoded = new TextDecoder().decode(
buffer.bytes().map((x) => x == 0x1B ? 0x21 : x),
);
expect(decoded).toEqual(expected);
}