-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.js
209 lines (158 loc) · 3.35 KB
/
terminal.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const proc = require('process');
const std = require('std');
const KEYCODES = require('./keycodes.js');
const escapeCodes = require('ansi-escape-codes');
const STTY_DEFAULT = proc.exec('stty -g < /dev/tty');
const STTY_MODES = {
KEY: 1,
LINE: 2
};
var savedPosY = [];
var savedPosX = [];
var maxY = 1;
var posY = 1;
var maxX = 1;
var posX = 1;
var mode = -1;
setMode(STTY_MODES.KEY);
function setMode(newMode) {
if (mode != STTY_MODES.KEY && newMode == STTY_MODES.KEY) {
proc.exec('stty -icanon min 1 < /dev/tty');
proc.exec('stty -echo < /dev/tty');
write(escapeCodes.hideCursor());
}
if (mode != STTY_MODES.LINE && newMode == STTY_MODES.LINE) {
proc.exec('stty sane < /dev/tty');
proc.exec('stty "' + STTY_DEFAULT + '" < /dev/tty');
write(escapeCodes.showCursor());
}
mode = newMode;
return mode;
}
function readKey() {
setMode(STTY_MODES.KEY);
return std.readKey();
}
function readLine() {
setMode(STTY_MODES.LINE);
return std.readLine();
}
function write(msg) {
var values = msg.split(/\n/);
posY += values.length - 1;
maxY = Math.max(maxY, posY);
if (values.length > 1) {
posX = 1;
}
posX += escapeCodes.stripAnsi(values[values.length - 1]).length;
values.forEach(function(row) {
maxX = Math.max(maxX, escapeCodes.stripAnsi(row).length);
});
std.print(msg);
}
function writeln(msg) {
write(msg + "\n");
}
function gotoY(y) {
if (y < 0) {
return;
}
var offset = posY - y;
posY -= offset;
if (posY <= 1) {
posY = 1;
}
if (offset < 0) {
write(escapeCodes.moveDown(Math.abs(offset)));
} else {
write(escapeCodes.moveUp(offset));
}
}
function gotoX(x) {
if (x < 0) {
return;
}
var offset = posX - x;
posX -= offset;
if (posX <= 1) {
posX = 1;
}
if (offset < 0) {
write(escapeCodes.moveRight(Math.abs(offset)));
} else {
write(escapeCodes.moveLeft(offset));
}
}
function saveX() {
savedPosX.push(posX);
}
function clearX() {
savedPosX = [];
}
function restoreX() {
if (!savedPosX.length) {
return;
}
var newPosX = savedPosX.pop();
gotoY(newPosX);
}
function saveY() {
savedPosY.push(posY);
}
function clearY() {
savedPosY = [];
}
function restoreY() {
if (!savedPosY.length) {
return;
}
var newPosY = savedPosY.pop();
gotoY(newPosY);
}
function restore() {
setMode(STTY_MODES.LINE);
}
function saveXY() {
saveX();
saveY();
}
function restoreXY() {
restoreY();
restoreX();
}
function clear() {
maxY = posY;
write(escapeCodes.clearLine());
}
exports = {
readKey: readKey,
readLine: readLine,
write: write,
writeln: writeln,
restore: restore,
gotoY: gotoY,
getY: function() { return posY; },
saveY: saveY,
clearY: clearY,
restoreY: restoreY,
gotoX: gotoX,
getX: function() { return posX; },
saveX: saveX,
clearX: clearX,
restoreX: restoreX,
saveXY: saveXY,
restoreXY: restoreXY,
clear: clear
};
// writeln('1x');
// writeln('2xx');
// writeln('3xxx');
// writeln('4xxxx');
// writeln('5xxxxx');
// gotoY(2);
// gotoX(2);
// write('xxxxxxxxxxxxxxxxxx');
// readKey();
// clear();
// // write(escapeCodes.clearLine());
// readKey();