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; break;
case 'd': // delete forward word case 'd': // delete forward word
case 'delete':
if (this.cursor < this.line.length) { if (this.cursor < this.line.length) {
next_word = this.line next_word = this.line
.slice(this.cursor, this.line.length) .slice(this.cursor, this.line.length)
@ -525,6 +526,18 @@ Interface.prototype._ttyWrite = function(s, key) {
this._refreshLine(); this._refreshLine();
} }
break; 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 { } else {

20
lib/tty_posix.js

@ -122,27 +122,31 @@ ReadStream.prototype._emitKey = function(s) {
// tab // tab
key.name = '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 // backspace or ctrl+h
key.name = 'backspace'; key.name = 'backspace';
key.meta = (s.charAt(0) === '\x1b');
} else if (s === '\x1b') { } else if (s === '\x1b' || s === '\x1b\x1b') {
// escape key // escape key
key.name = 'escape'; key.name = 'escape';
key.meta = (s.length === 2);
} else if (s === ' ') { } else if (s === ' ' || s === '\x1b ') {
key.name = 'space'; key.name = 'space';
key.meta = (s.length === 2);
} else if (s <= '\x1a') { } else if (s <= '\x1a') {
// ctrl+letter // ctrl+letter
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1); key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.ctrl = true; key.ctrl = true;
} else if (s >= 'a' && s <= 'z') { } else if (s.length === 1 && s >= 'a' && s <= 'z') {
// lowercase letter // lowercase letter
key.name = s; key.name = s;
} else if (s >= 'A' && s <= 'Z') { } else if (s.length === 1 && s >= 'A' && s <= 'Z') {
// shift+letter // shift+letter
key.name = s.toLowerCase(); key.name = s.toLowerCase();
key.shift = true; key.shift = true;
@ -254,7 +258,13 @@ ReadStream.prototype._emitKey = function(s) {
/* misc. */ /* misc. */
case '[Z': key.name = 'tab'; key.shift = true; break; 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 // Don't emit a key if no name was found

Loading…
Cancel
Save