textui/ansi.test.ts

30 lines
937 B
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 });
checkSequence(stdout, "![38;2;128;26;255m![48;2;0;0;0m![0;0H$");
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);
}