Skip to content

Commit 4c070d4

Browse files
jasnelladdaleax
authored andcommitted
readline: move escape codes into internal/readline
Moves escape codes into internal/readline for easier management. PR-URL: #12755 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 4ac7a68 commit 4c070d4

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

lib/internal/readline.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,27 @@
77
const ansi =
88
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
99

10+
const kEscape = '\x1b';
11+
1012
var getStringWidth;
1113
var isFullWidthCodePoint;
1214

15+
function CSI(strings, ...args) {
16+
let ret = `${kEscape}[`;
17+
for (var n = 0; n < strings.length; n++) {
18+
ret += strings[n];
19+
if (n < args.length)
20+
ret += args[n];
21+
}
22+
return ret;
23+
}
24+
25+
CSI.kEscape = kEscape;
26+
CSI.kClearToBeginning = CSI`1K`;
27+
CSI.kClearToEnd = CSI`0K`;
28+
CSI.kClearLine = CSI`2K`;
29+
CSI.kClearScreenDown = CSI`0J`;
30+
1331
if (process.binding('config').hasIntl) {
1432
const icu = process.binding('icu');
1533
getStringWidth = function getStringWidth(str, options) {
@@ -151,11 +169,11 @@ function* emitKeys(stream) {
151169
shift: false
152170
};
153171

154-
if (ch === '\x1b') {
172+
if (ch === kEscape) {
155173
escaped = true;
156174
s += (ch = yield);
157175

158-
if (ch === '\x1b') {
176+
if (ch === kEscape) {
159177
s += (ch = yield);
160178
}
161179
}
@@ -370,7 +388,7 @@ function* emitKeys(stream) {
370388
// backspace or ctrl+h
371389
key.name = 'backspace';
372390
key.meta = escaped;
373-
} else if (ch === '\x1b') {
391+
} else if (ch === kEscape) {
374392
// escape key
375393
key.name = 'escape';
376394
key.meta = escaped;
@@ -409,5 +427,6 @@ module.exports = {
409427
emitKeys,
410428
getStringWidth,
411429
isFullWidthCodePoint,
412-
stripVTControlCharacters
430+
stripVTControlCharacters,
431+
CSI
413432
};

lib/readline.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,21 @@ const { debug, inherits } = require('util');
3131
const Buffer = require('buffer').Buffer;
3232
const EventEmitter = require('events');
3333
const {
34+
CSI,
3435
emitKeys,
3536
getStringWidth,
3637
isFullWidthCodePoint,
3738
stripVTControlCharacters
3839
} = require('internal/readline');
3940

41+
const {
42+
kEscape,
43+
kClearToBeginning,
44+
kClearToEnd,
45+
kClearLine,
46+
kClearScreenDown
47+
} = CSI;
48+
4049
const kHistorySize = 30;
4150
const kMincrlfDelay = 100;
4251
const kMaxcrlfDelay = 2000;
@@ -995,7 +1004,7 @@ function emitKeypressEvents(stream, iface) {
9951004
try {
9961005
stream[ESCAPE_DECODER].next(r[i]);
9971006
// Escape letter at the tail position
998-
if (r[i] === '\x1b' && i + 1 === r.length) {
1007+
if (r[i] === kEscape && i + 1 === r.length) {
9991008
timeoutId = setTimeout(escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
10001009
}
10011010
} catch (err) {
@@ -1047,9 +1056,9 @@ function cursorTo(stream, x, y) {
10471056
throw new Error('Can\'t set cursor row without also setting it\'s column');
10481057

10491058
if (typeof y !== 'number') {
1050-
stream.write('\x1b[' + (x + 1) + 'G');
1059+
stream.write(CSI`${x + 1}G`);
10511060
} else {
1052-
stream.write('\x1b[' + (y + 1) + ';' + (x + 1) + 'H');
1061+
stream.write(CSI`${y + 1};${x + 1}H`);
10531062
}
10541063
}
10551064

@@ -1062,15 +1071,15 @@ function moveCursor(stream, dx, dy) {
10621071
return;
10631072

10641073
if (dx < 0) {
1065-
stream.write('\x1b[' + (-dx) + 'D');
1074+
stream.write(CSI`${-dx}D`);
10661075
} else if (dx > 0) {
1067-
stream.write('\x1b[' + dx + 'C');
1076+
stream.write(CSI`${dx}C`);
10681077
}
10691078

10701079
if (dy < 0) {
1071-
stream.write('\x1b[' + (-dy) + 'A');
1080+
stream.write(CSI`${-dy}A`);
10721081
} else if (dy > 0) {
1073-
stream.write('\x1b[' + dy + 'B');
1082+
stream.write(CSI`${dy}B`);
10741083
}
10751084
}
10761085

@@ -1087,13 +1096,13 @@ function clearLine(stream, dir) {
10871096

10881097
if (dir < 0) {
10891098
// to the beginning
1090-
stream.write('\x1b[1K');
1099+
stream.write(kClearToBeginning);
10911100
} else if (dir > 0) {
10921101
// to the end
1093-
stream.write('\x1b[0K');
1102+
stream.write(kClearToEnd);
10941103
} else {
10951104
// entire line
1096-
stream.write('\x1b[2K');
1105+
stream.write(kClearLine);
10971106
}
10981107
}
10991108

@@ -1105,7 +1114,7 @@ function clearScreenDown(stream) {
11051114
if (stream === null || stream === undefined)
11061115
return;
11071116

1108-
stream.write('\x1b[0J');
1117+
stream.write(kClearScreenDown);
11091118
}
11101119

11111120
module.exports = {

0 commit comments

Comments
 (0)