-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.js
51 lines (41 loc) · 1023 Bytes
/
screen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict";
export class Screen {
constructor() {
this.width = 80;
this.height = 24;
this.buffer = [];
}
init() {
for (var y = 0; y < this.height; y++) {
this.buffer.push([]);
for (var x = 0; x < this.width; x++) {
this.buffer[y] += ' ';
}
}
}
push(y, x, text) {
// Arrays starts at 0
x--;
y--;
if (x+1 >= this.width) {
y++;
x = x - (this.width-1);
}
const currentLine = this.buffer[y];
const newLine = [currentLine.slice(0, x), text, currentLine.slice(x + text.length)].join('');
this.buffer[y] = newLine;
}
render() {
let i = 1;
this.buffer.forEach(line => {
let prefix = i;
if (prefix < 10) prefix = '0'+i;
prefix += ' ';
console.log(prefix + line);
i++;
});
}
clear() {
console.clear();
}
}