From 2d09ef854118692eefd6f04dbd8354811f8a78be Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sun, 12 Sep 2010 21:47:56 -0700 Subject: [PATCH] Fix style in readline --- lib/readline.js | 127 ++++++++++++++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 53 deletions(-) diff --git a/lib/readline.js b/lib/readline.js index 6a4652c22e..f6d6034983 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -364,82 +364,103 @@ Interface.prototype._ttyWrite = function (b) { case 27: /* escape sequence */ var next_word, next_non_word, previous_word, previous_non_word; - if (b[1] === 98 && this.cursor > 0) { // meta-b - backward word - previous_word = this.line.slice(0, this.cursor) - .split('').reverse().join('') - .search(/\w/); - if (previous_word !== -1) { - previous_non_word = this.line.slice(0, this.cursor - previous_word) - .split('').reverse().join('') - .search(/\W/); - if (previous_non_word !== -1) { - this.cursor -= previous_word + previous_non_word; - this._refreshLine(); - break; - } - } - this.cursor = 0; - this._refreshLine(); - } else if (b[1] === 102 && this.cursor < this.line.length) { // meta-f - forward word - next_word = this.line.slice(this.cursor, this.line.length) - .search(/\w/); - if (next_word !== -1) { - next_non_word = this.line.slice(this.cursor + next_word, this.line.length) - .search(/\W/); - if (next_non_word !== -1) { - this.cursor += next_word + next_non_word; - this._refreshLine(); - break; - } - } - this.cursor = this.line.length; - this._refreshLine(); - } else if (b[1] === 100 && this.cursor < this.line.length) { // meta-d delete forward word - next_word = this.line.slice(this.cursor, this.line.length) - .search(/\w/); - if (next_word !== -1) { - next_non_word = this.line.slice(this.cursor + next_word, this.line.length) - .search(/\W/); - if (next_non_word !== -1) { - this.line = this.line.slice(this.cursor + next_word + next_non_word); - this.cursor = 0; - this._refreshLine(); - break; - } - } - this.line = ''; - this.cursor = 0; - this._refreshLine(); - } else if (b[1] === 91 && b[2] === 68) { // left arrow + + if (b[1] === 98 && this.cursor > 0) { + // meta-b - backward word + previous_word = this.line.slice(0, this.cursor) + .split('').reverse().join('') + .search(/\w/); + if (previous_word !== -1) { + previous_non_word = this.line.slice(0, this.cursor - previous_word) + .split('').reverse().join('') + .search(/\W/); + if (previous_non_word !== -1) { + this.cursor -= previous_word + previous_non_word; + this._refreshLine(); + break; + } + } + this.cursor = 0; + this._refreshLine(); + + } else if (b[1] === 102 && this.cursor < this.line.length) { + // meta-f - forward word + next_word = this.line.slice(this.cursor, this.line.length).search(/\w/); + if (next_word !== -1) { + next_non_word = + this.line.slice(this.cursor + next_word, this.line.length) + .search(/\W/); + if (next_non_word !== -1) { + this.cursor += next_word + next_non_word; + this._refreshLine(); + break; + } + } + this.cursor = this.line.length; + this._refreshLine(); + + } else if (b[1] === 100 && this.cursor < this.line.length) { + // meta-d delete forward word + next_word = this.line.slice(this.cursor, this.line.length).search(/\w/); + if (next_word !== -1) { + next_non_word = + this.line.slice(this.cursor + next_word, this.line.length) + .search(/\W/); + if (next_non_word !== -1) { + this.line = + this.line.slice(this.cursor + next_word + next_non_word); + this.cursor = 0; + this._refreshLine(); + break; + } + } + this.line = ''; + this.cursor = 0; + this._refreshLine(); + + } else if (b[1] === 91 && b[2] === 68) { + // left arrow if (this.cursor > 0) { this.cursor--; this.output.write('\x1b[0D'); } - } else if (b[1] === 91 && b[2] === 67) { // right arrow + + } else if (b[1] === 91 && b[2] === 67) { + // right arrow if (this.cursor != this.line.length) { this.cursor++; this.output.write('\x1b[0C'); } + } else if ((b[1] === 91 && b[2] === 72) || (b[1] === 79 && b[2] === 72) || (b[1] === 91 && b[2] === 55) || - (b[1] === 91 && b[2] === 49 && (b[3] && b[3] === 126))) { // home + (b[1] === 91 && b[2] === 49 && (b[3] && b[3] === 126))) { + // home this.cursor = 0; this._refreshLine(); } else if ((b[1] === 91 && b[2] === 70) || (b[1] === 79 && b[2] === 70) || (b[1] === 91 && b[2] === 56) || - (b[1] === 91 && b[2] === 52 && (b[3] && b[3] === 126))) { // end + (b[1] === 91 && b[2] === 52 && (b[3] && b[3] === 126))) { + // end this.cursor = this.line.length; this._refreshLine(); - } else if (b[1] === 91 && b[2] === 65) { // up arrow + + } else if (b[1] === 91 && b[2] === 65) { + // up arrow this._historyPrev(); - } else if (b[1] === 91 && b[2] === 66) { // down arrow + + } else if (b[1] === 91 && b[2] === 66) { + // down arrow this._historyNext(); - } else if (b[1] === 91 && b[2] === 51 && this.cursor < this.line.length) { // delete right + + } else if (b[1] === 91 && b[2] === 51 && this.cursor < this.line.length) { + // delete right this.line = this.line.slice(0, this.cursor) + this.line.slice(this.cursor+1, this.line.length); this._refreshLine(); + } break;