Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 8e08116

Browse files
committed
Remove Cursor.
1 parent 6469659 commit 8e08116

File tree

3 files changed

+30
-78
lines changed

3 files changed

+30
-78
lines changed

src/ANSIParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export class ANSIParser {
206206
/* tslint:disable:max-line-length */
207207
long = "Move the active position to the same horizontal position on the preceding lin If the active position is at the top margin, a scroll down is performed.";
208208

209-
if (this.screenBuffer.cursor.row === this.screenBuffer.marginTop) {
209+
if (this.screenBuffer.cursorPosition.row === this.screenBuffer.marginTop) {
210210
this.screenBuffer.scrollDown(1);
211211
} else {
212212
this.screenBuffer.moveCursorRelative({vertical: -1});
@@ -432,7 +432,7 @@ export class ANSIParser {
432432
url = "http://www.vt100.net/docs/vt510-rm/DL";
433433
short = "Deletes one or more lines in the scrolling region, starting with the line that has the cursor. (DL)";
434434

435-
this.screenBuffer.scrollUp(param || 1, this.screenBuffer.cursor.row);
435+
this.screenBuffer.scrollUp(param || 1, this.screenBuffer.cursorPosition.row);
436436
break;
437437
case "P":
438438
url = "http://www.vt100.net/docs/vt510-rm/DCH.html";
@@ -566,6 +566,6 @@ function or1(value: number | undefined) {
566566

567567
// TODO: Move to
568568
function logPosition(buffer: ScreenBuffer) {
569-
const position = buffer.cursor.getPosition();
569+
const position = buffer.cursorPosition;
570570
debug(`%crow: ${position.row}\tcolumn: ${position.column}\t value: ${buffer.at(position)}`, "color: green");
571571
}

src/Cursor.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/ScreenBuffer.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as events from "events";
22
import {Char, attributesFlyweight, defaultAttributes} from "./Char";
3-
import {Cursor} from "./Cursor";
43
import * as i from "./Interfaces";
54
import * as e from "./Enums";
65
import {List} from "immutable";
@@ -13,7 +12,9 @@ interface SavedState {
1312

1413
export class ScreenBuffer extends events.EventEmitter {
1514
public static hugeOutputThreshold = 300;
16-
public cursor: Cursor = new Cursor();
15+
public cursorPosition: RowColumn = {row: 0, column: 0};
16+
public _showCursor = true;
17+
public _blinkCursor = true;
1718
public activeScreenBufferType = e.ScreenBufferType.Standard;
1819
private storage = List<List<Char>>();
1920
private _attributes: i.Attributes = {...defaultAttributes, color: e.Color.White, weight: e.Weight.Normal};
@@ -43,10 +44,10 @@ export class ScreenBuffer extends events.EventEmitter {
4344
this.moveCursorRelative({horizontal: -1});
4445
break;
4546
case e.KeyCode.Tab:
46-
this.moveCursorAbsolute({column: Math.floor((this.cursor.column + 8) / 8) * 8});
47+
this.moveCursorAbsolute({column: Math.floor((this.cursorPosition.column + 8) / 8) * 8});
4748
break;
4849
case e.KeyCode.NewLine:
49-
if (this.cursor.row === this._margins.bottom) {
50+
if (this.cursorPosition.row === this._margins.bottom) {
5051
this.scrollUp(1);
5152
} else {
5253
this.moveCursorRelative({vertical: 1});
@@ -68,7 +69,7 @@ export class ScreenBuffer extends events.EventEmitter {
6869

6970
scrollDown(count: number) {
7071
this.storage = this.storage.splice((this._margins.bottom || 0) - count + 1, count).toList();
71-
times(count, () => this.storage = this.storage.splice(this.cursor.row, 0, undefined).toList());
72+
times(count, () => this.storage = this.storage.splice(this.cursorPosition.row, 0, undefined).toList());
7273
}
7374

7475
scrollUp(count: number, deletedLine = this._margins.top) {
@@ -91,7 +92,7 @@ export class ScreenBuffer extends events.EventEmitter {
9192
toRenderable(status: e.Status, fromStorage = this.storage): List<List<Char>> {
9293
let storage = fromStorage;
9394

94-
if (status === e.Status.InProgress && (this.cursor.show || this.cursor.blink)) {
95+
if (status === e.Status.InProgress && (this._showCursor || this._blinkCursor)) {
9596
const cursorRow = this.cursorPosition.row - (this.storage.size - fromStorage.size);
9697
const cursorColumn = this.cursorPosition.column;
9798

@@ -141,28 +142,39 @@ export class ScreenBuffer extends events.EventEmitter {
141142
}
142143

143144
showCursor(state: boolean): void {
144-
this.ensureRowExists(this.cursor.row);
145-
this.cursor.show = state;
145+
this.ensureRowExists(this.cursorPosition.row);
146+
this._showCursor = state;
146147
this.emitData();
147148
}
148149

149150
blinkCursor(state: boolean): void {
150-
this.ensureRowExists(this.cursor.row);
151-
this.cursor.blink = state;
151+
this.ensureRowExists(this.cursorPosition.row);
152+
this._blinkCursor = state;
152153
this.emitData();
153154
}
154155

155-
moveCursorRelative(position: Advancement): this {
156-
this.cursor.moveRelative(position);
157-
this.ensureRowExists(this.cursor.row);
156+
moveCursorRelative(advancement: Advancement): this {
157+
const row = Math.max(0, this.cursorPosition.row + (advancement.vertical || 0));
158+
const column = Math.max(0, this.cursorPosition.column + (advancement.horizontal || 0));
159+
160+
this.moveCursorAbsolute({ row: row, column: column });
161+
162+
this.ensureRowExists(this.cursorPosition.row);
158163
this.emitData();
159164

160165
return this;
161166
}
162167

163168
moveCursorAbsolute(position: Partial<RowColumn>): this {
164-
this.cursor.moveAbsolute(position, this.homePosition);
165-
this.ensureRowExists(this.cursor.row);
169+
if (typeof position.column === "number") {
170+
this.cursorPosition.column = Math.max(position.column, 0) + this.homePosition.column;
171+
}
172+
173+
if (typeof position.row === "number") {
174+
this.cursorPosition.row = Math.max(position.row, 0) + this.homePosition.row;
175+
}
176+
177+
this.ensureRowExists(this.cursorPosition.row);
166178
this.emitData();
167179

168180
return this;
@@ -254,10 +266,6 @@ export class ScreenBuffer extends events.EventEmitter {
254266
return this.storage.size;
255267
}
256268

257-
get cursorPosition(): RowColumn {
258-
return this.cursor.getPosition();
259-
}
260-
261269
isEmpty(): boolean {
262270
return this.storage.size === 0;
263271
}

0 commit comments

Comments
 (0)