textui/ansi.test.ts

42 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-05-11 21:20:33 +00:00
import { AnsiTerminalDisplay } from "./ansi.ts";
2021-06-24 22:41:34 +00:00
import { Buffer, describe, expect, it } from "./testing.ts";
2021-05-11 21:20:33 +00:00
describe(AnsiTerminalDisplay, () => {
it("clears the screen", async () => {
const stdout = new Buffer();
const display = new AnsiTerminalDisplay(stdout);
await display.clear();
2021-06-24 22:41:34 +00:00
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 });
2021-06-27 17:46:27 +00:00
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");
2021-05-11 21:20:33 +00:00
});
});
2021-06-24 22:41:34 +00:00
function checkSequence(buffer: Buffer, expected: string) {
const decoded = new TextDecoder().decode(
buffer.bytes().map((x) => x == 0x1B ? 0x21 : x),
);
expect(decoded).toEqual(expected);
}