From a974892eaefde3b24d397b23e3f6dea29a43aaf1 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Tue, 10 Jul 2018 18:41:45 +0100 Subject: [PATCH] feat: support ctrl+l to clear And refactor the stdin handling --- lib/nodemon.js | 57 +++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/nodemon.js b/lib/nodemon.js index 13961b8b..836e387b 100644 --- a/lib/nodemon.js +++ b/lib/nodemon.js @@ -96,44 +96,53 @@ function nodemon(settings) { }); // echo out notices about running state - if (config.options.stdin && config.options.restartable) { - // allow nodemon to restart when the use types 'rs\n' - process.stdin.resume(); - process.stdin.setEncoding('utf8'); - process.stdin.on('data', function (data) { - data = (data + '').trim().toLowerCase(); - - // if the keys entered match the restartable value, then restart! - if (data === config.options.restartable) { - bus.emit('restart'); - } - }); - } else if (config.options.stdin) { - // if 'restartable' is disabled (via a nodemon.json) - // then it's possible we're being used with a REPL + if (config.options.stdin) { // so let's make sure we don't eat the key presses // but also, since we're wrapping, watch out for - // special keys, like ctrl+c x 2 or '.exit' or ctrl+d + // special keys, like ctrl+c x 2 or '.exit' or ctrl+d or ctrl+l var ctrlC = false; var buffer = ''; + const rs = config.options.restartable; + process.stdin.setEncoding('utf8'); process.stdin.on('data', function (data) { - buffer += data; data = data.toString(); - var chr = data.charCodeAt(0); + buffer += data; + const chr = data.charCodeAt(0); + + // if restartable, echo back + if (rs) { + if (chr === 13) { + process.stdout.write('\n'); + } + // this prevents cursor keys from working. + process.stdout.write(String.fromCharCode(chr)); + } + if (chr === 3) { - if (ctrlC) { - process.exit(); + // if restartable, assume ctrl+c will break immediately + if (ctrlC || rs) { + process.exit(rs ? 1 : 0); } ctrlC = true; - } else if (buffer === '.exit' || chr === 4) { + return; + } else if (buffer === '.exit' || chr === 4) { // ctrl+d process.exit(); - } else if (ctrlC || chr === 10) { - ctrlC = false; + } else if (chr === 13 || chr === 10) { // enter / carriage return + const input = buffer.toString().trim().toLowerCase(); + if (input === rs) { + bus.emit('restart'); + } + buffer = ''; + } else if (chr === 12) { // ctrl+l + console.clear(); buffer = ''; } + ctrlC = false; }); - process.stdin.setRawMode(true); + if (process.stdin.setRawMode) { + process.stdin.setRawMode(true); + } } if (config.options.restartable) {