Skip to content

Commit

Permalink
feat: support ctrl+l to clear
Browse files Browse the repository at this point in the history
And refactor the stdin handling
  • Loading branch information
remy committed Jul 10, 2018
1 parent d48a482 commit a974892
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions lib/nodemon.js
Expand Up @@ -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) {
Expand Down

0 comments on commit a974892

Please sign in to comment.