From 4c070d489718b196d7950998ccfb54bcc50d9711 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 29 Apr 2017 17:05:04 -0700 Subject: readline: move escape codes into internal/readline Moves escape codes into internal/readline for easier management. PR-URL: https://github.com/nodejs/node/pull/12755 Reviewed-By: Anna Henningsen --- lib/internal/readline.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'lib/internal/readline.js') diff --git a/lib/internal/readline.js b/lib/internal/readline.js index 4474b02345..96da5cc82f 100644 --- a/lib/internal/readline.js +++ b/lib/internal/readline.js @@ -7,9 +7,27 @@ const ansi = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; +const kEscape = '\x1b'; + var getStringWidth; var isFullWidthCodePoint; +function CSI(strings, ...args) { + let ret = `${kEscape}[`; + for (var n = 0; n < strings.length; n++) { + ret += strings[n]; + if (n < args.length) + ret += args[n]; + } + return ret; +} + +CSI.kEscape = kEscape; +CSI.kClearToBeginning = CSI`1K`; +CSI.kClearToEnd = CSI`0K`; +CSI.kClearLine = CSI`2K`; +CSI.kClearScreenDown = CSI`0J`; + if (process.binding('config').hasIntl) { const icu = process.binding('icu'); getStringWidth = function getStringWidth(str, options) { @@ -151,11 +169,11 @@ function* emitKeys(stream) { shift: false }; - if (ch === '\x1b') { + if (ch === kEscape) { escaped = true; s += (ch = yield); - if (ch === '\x1b') { + if (ch === kEscape) { s += (ch = yield); } } @@ -370,7 +388,7 @@ function* emitKeys(stream) { // backspace or ctrl+h key.name = 'backspace'; key.meta = escaped; - } else if (ch === '\x1b') { + } else if (ch === kEscape) { // escape key key.name = 'escape'; key.meta = escaped; @@ -409,5 +427,6 @@ module.exports = { emitKeys, getStringWidth, isFullWidthCodePoint, - stripVTControlCharacters + stripVTControlCharacters, + CSI }; -- cgit v1.2.3