From 5b7f4720f3c9af14314e63d1dcd1799adad5cb5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 27 Jun 2021 19:46:27 +0200 Subject: [PATCH] Fix cursor position bug --- ansi.test.ts | 14 +++++++++++++- ansi.ts | 21 +++++++++++++++++---- demo.ts | 1 + 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/ansi.test.ts b/ansi.test.ts index 0b003de..1ffd1a7 100644 --- a/ansi.test.ts +++ b/ansi.test.ts @@ -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"); }); }); diff --git a/ansi.ts b/ansi.ts index b847a85..b17226d 100644 --- a/ansi.ts +++ b/ansi.ts @@ -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 { diff --git a/demo.ts b/demo.ts index 041dbbd..e40a307 100755 --- a/demo.ts +++ b/demo.ts @@ -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));