Browse Source

Support pastes and meta-backspace

v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
430a7f1bed
  1. 13
      lib/readline.js
  2. 20
      lib/tty_posix.js

13
lib/readline.js

@ -503,6 +503,7 @@ Interface.prototype._ttyWrite = function(s, key) {
break;
case 'd': // delete forward word
case 'delete':
if (this.cursor < this.line.length) {
next_word = this.line
.slice(this.cursor, this.line.length)
@ -525,6 +526,18 @@ Interface.prototype._ttyWrite = function(s, key) {
this._refreshLine();
}
break;
case 'backspace': // delete backwards to a word boundary
if (this.cursor !== 0) {
var leading = this.line.slice(0, this.cursor);
var match = leading.match(/\s?((\W+|\w+)\s*)$/);
leading = leading.slice(0, leading.length - match[1].length);
this.line = leading + this.line.slice(this.cursor, this.line.length);
this.cursor = leading.length;
this._refreshLine();
}
break;
}
} else {

20
lib/tty_posix.js

@ -122,27 +122,31 @@ ReadStream.prototype._emitKey = function(s) {
// tab
key.name = 'tab';
} else if (s === '\b' || s === '\x7f') {
} else if (s === '\b' || s === '\x7f' ||
s === '\x1b\x7f' || s === '\x1b\b') {
// backspace or ctrl+h
key.name = 'backspace';
key.meta = (s.charAt(0) === '\x1b');
} else if (s === '\x1b') {
} else if (s === '\x1b' || s === '\x1b\x1b') {
// escape key
key.name = 'escape';
key.meta = (s.length === 2);
} else if (s === ' ') {
} else if (s === ' ' || s === '\x1b ') {
key.name = 'space';
key.meta = (s.length === 2);
} else if (s <= '\x1a') {
// ctrl+letter
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.ctrl = true;
} else if (s >= 'a' && s <= 'z') {
} else if (s.length === 1 && s >= 'a' && s <= 'z') {
// lowercase letter
key.name = s;
} else if (s >= 'A' && s <= 'Z') {
} else if (s.length === 1 && s >= 'A' && s <= 'Z') {
// shift+letter
key.name = s.toLowerCase();
key.shift = true;
@ -254,7 +258,13 @@ ReadStream.prototype._emitKey = function(s) {
/* misc. */
case '[Z': key.name = 'tab'; key.shift = true; break;
}
} else if (s.length > 1 && s[0] !== '\x1b') {
// Got a longer-than-one string of characters.
// Probably a paste, since it wasn't a control sequence.
Array.prototype.forEach.call(s, this._emitKey, this);
return;
}
// Don't emit a key if no name was found

Loading…
Cancel
Save