This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (85 loc) · 2.2 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Import Internal Dependencies
import Vector2 from "./src/Vector2.js";
// Vars
const stdout = process.stdout;
export class VirtualTTY {
#currLen = 0;
#contentByLines = [];
#maxLenBehavior = "overwriteStart";
#maxStrLength = 0;
static alignLeft(vTTY, heigh = vTTY.position.y, padding = 1) {
return new Vector2(vTTY.size.x + padding, heigh);
}
constructor(pos, size, options = Object.create(null)) {
this.position = Vector2.toVector2(pos);
this.size = Vector2.toVector2(size);
this.#maxStrLength = this.size.y * this.size.x;
// console.log('Terminal size: ' + process.stdout.columns + 'x' + process.stdout.rows);
}
getLocalPosition() {
const x = this.#currLen % this.size.x;
const y = Math.floor(this.#currLen / this.size.x);
return new Vector2(x, y);
}
getFullScreenContent() {
for (let y = 0; y < this.size.y; y++) {
this.cursorTo(new Vector2(0, y));
stdout.write(" ".repeat(this.size.x));
}
}
cursorTo(vector) {
if (!VirtualTTY.DEBUG) {
stdout.cursorTo(this.position.x + vector.x, this.position.y + vector.y);
}
}
triggerMaxLenBehavior() {
switch (this.#maxLenBehavior) {
case "overwriteStart": {
this.#currLen = 0;
break;
}
case "overwriteEnd": {
this.#currLen -= str.length;
break;
}
case "stream": {
break;
}
}
return this.getLocalPosition();
}
rawWrite(str) {
if (!VirtualTTY.DEBUG) {
stdout.write(str);
}
this.#currLen += str.length;
}
write(str) {
let local = this.getLocalPosition();
if (local.y >= this.size.y) {
local = this.triggerMaxLenBehavior();
}
// if (local.x === 0) {
// str = str.trimStart();
// }
this.cursorTo(local);
const div = this.size.x - local.x;
// console.log(local, div);
if (div >= str.length) {
this.rawWrite(str);
}
else {
this.rawWrite(str.slice(0, div));
this.write(str.slice(div));
}
}
clear() {
for (let y = 0; y < this.size.y; y++) {
this.cursorTo(new Vector2(0, y));
stdout.write(" ".repeat(this.size.x));
}
this.#currLen = 0;
}
}
VirtualTTY.DEBUG = false;
export { Vector2 };