From fa9857aedf66ea19c08108acb20895cea5e1b3c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Sat, 6 Jul 2019 16:47:48 +0200 Subject: [PATCH] simplify cursor movements --- src/InputHandler.ts | 69 ++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 6ae2cdeb..1ac02944 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -598,7 +598,11 @@ export class InputHandler extends Disposable implements IInputHandler { this._terminal.updateRange(this._terminal.buffer.y); } - // restrict cursor changes to addressable cols/rows + /** + * FIXME: + * - create Cursor class living on Buffer + * - move these private cursor methods to Cursor class as API + */ private _restrictCursor(): void { // cols if (this._terminal.buffer.x < 0) { @@ -614,6 +618,19 @@ export class InputHandler extends Disposable implements IInputHandler { } } + private _setCursor(x: number, y: number): void { + this._terminal.buffer.x = x; + this._terminal.buffer.y = y; + this._restrictCursor(); + } + + private _moveCursor(x: number, y: number): void { + // for relative changes we have to make sure we are within 0 .. cols/rows - 1 + // before calculating the new position + this._restrictCursor(); + this._setCursor(this._terminal.buffer.x + x, this._terminal.buffer.y + y); + } + /** * CSI Ps A * Cursor Up Ps Times (default = 1) (CUU). @@ -623,9 +640,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.y -= param; - this._restrictCursor(); + this._moveCursor(0, -param); } /** @@ -637,9 +652,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.y += param; - this._restrictCursor(); + this._moveCursor(0, param); } /** @@ -651,9 +664,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.x += param; - this._restrictCursor(); + this._moveCursor(param, 0); } /** @@ -665,9 +676,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.x -= param; - this._restrictCursor(); + this._moveCursor(-param, 0); } /** @@ -680,10 +689,8 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.y += param; + this._moveCursor(0, param); this._terminal.buffer.x = 0; - this._restrictCursor(); } @@ -697,10 +704,8 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.y -= param; + this._moveCursor(0, -param); this._terminal.buffer.x = 0; - this._restrictCursor(); } @@ -713,9 +718,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.x = param - 1; - this._restrictCursor(); + this._setCursor(param - 1, this._terminal.buffer.y); } /** @@ -731,11 +734,7 @@ export class InputHandler extends Disposable implements IInputHandler { } else { col = 0; } - - this._restrictCursor(); - this._terminal.buffer.x = col; - this._terminal.buffer.y = row; - this._restrictCursor(); + this._setCursor(col, row); } /** @@ -1018,9 +1017,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.x = param - 1; - this._restrictCursor(); + this._setCursor(param - 1, this._terminal.buffer.y); } /** @@ -1033,9 +1030,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.x += param; - this._restrictCursor(); + this._moveCursor(param, 0); } /** @@ -1148,9 +1143,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.y = param - 1; - this._restrictCursor(); + this._setCursor(this._terminal.buffer.x, param - 1); } /** @@ -1163,9 +1156,7 @@ export class InputHandler extends Disposable implements IInputHandler { if (param < 1) { param = 1; } - this._restrictCursor(); - this._terminal.buffer.y += param; - this._restrictCursor(); + this._moveCursor(0, param); } /**