Fix cursor position bug

This commit is contained in:
Michaël Lemaire 2021-06-27 19:46:27 +02:00
parent 65a06d40d4
commit 5b7f4720f3
3 changed files with 31 additions and 5 deletions

View file

@ -17,7 +17,19 @@ describe(AnsiTerminalDisplay, () => {
{ 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$");
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");
});
});

21
ansi.ts
View file

@ -46,29 +46,42 @@ export class AnsiTerminalDisplay implements Display {
if (f != char.fg) {
f = char.fg;
await this.writer.write(this.palette_fg[f]);
const col = this.palette_fg[f];
if (col) {
await this.writer.write(col);
}
}
if (b != char.bg) {
b = char.bg;
await this.writer.write(this.palette_bg[b]);
const col = this.palette_bg[b];
if (col) {
await this.writer.write(col);
}
}
if (x != at.x || y != at.y) {
x = at.x;
y = at.y;
await this.writer.write(escape(`[${at.y};${at.x}H`));
await this.writer.write(escape(`[${y + 1};${x + 1}H`));
}
await this.writer.write(new TextEncoder().encode(char.ch));
x += 1;
if (x > this.width) {
if (x >= this.width) {
x = 0;
y += 1;
}
this.state = { x, y, f, b };
}
/**
* Force the display size for subsequent prints
*/
forceSize(size: BufferSize) {
this.width = size.w;
}
}
function escape(sequence: string): Uint8Array {

View file

@ -11,5 +11,6 @@ await ui.init([
{ r: 0, g: 1, b: 1 },
]);
ui.drawing.color(2, 0).text("hello", { x: 10, y: 3 });
ui.drawing.color(0, 1).text("world", { x: 10, y: 5 });
await ui.flush();
await new Promise((resolve) => setTimeout(resolve, 3000));