From d452b72dd8984be73611fe8d28aa55f8b1d89c21 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 11 Jul 2013 23:00:03 -0500 Subject: [PATCH] optimize updateCursor with CUU, CUD, CUF, and CUB. --- lib/program.js | 19 +++++++++++++++++ lib/widget.js | 56 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/lib/program.js b/lib/program.js index 8bbd941..1be5b8a 100644 --- a/lib/program.js +++ b/lib/program.js @@ -49,6 +49,14 @@ function Program(options) { this.listen(); + if (options.zero) { + this.ti = 0; + this.ni = 1; + } else { + this.ti = -1; + this.ni = 0; + } + //if (!Program.global) { // Program._write = process.stdout.write; // process.stdout.write = function() {}; @@ -750,10 +758,13 @@ Program.prototype.repeat = function(ch, i) { * Normal */ +//Program.prototype.pad = Program.prototype.nul = function() { + //if (this.tput) return this.put.nul(); return this.write('\0'); }; +Program.prototype.bel = Program.prototype.bell = function() { if (this.tput) return this.put.bel(); return this.write('\x07'); @@ -768,11 +779,14 @@ Program.prototype.form = function() { return this.write('\x0c'); }; +Program.prototype.kbs = Program.prototype.backspace = function() { this.x--; + if (this.tput) return this.put.kbs(); return this.write('\x08'); }; +Program.prototype.ht = Program.prototype.tab = function() { this.x += 8; if (this.tput) return this.put.ht(); // or `tab` @@ -794,9 +808,12 @@ Program.prototype.return = function() { return this.write('\r'); }; +Program.prototype.nel = +Program.prototype.newline = Program.prototype.feed = function() { this.x = 1; this.y++; + if (this.tput) return this.put.nel(); return this.write('\n'); }; @@ -809,6 +826,7 @@ Program.prototype.esc = function(code) { }; // ESC D Index (IND is 0x84). +Program.prototype.ind = Program.prototype.index = function() { this.y++; if (this.tput) return this.put.ind(); @@ -816,6 +834,7 @@ Program.prototype.index = function() { }; // ESC M Reverse Index (RI is 0x8d). +Program.prototype.ri = Program.prototype.reverse = Program.prototype.reverseIndex = function() { this.y--; diff --git a/lib/widget.js b/lib/widget.js index ef855da..8487c65 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -2503,6 +2503,9 @@ function Textarea(options) { this.value = options.value || ''; + this.cx = -1; + this.cy = -1; + this.__updateCursor = this.updateCursor.bind(this); this.on('resize', this.__updateCursor); this.on('move', this.__updateCursor); @@ -2512,12 +2515,16 @@ Textarea.prototype.__proto__ = ScrollableText.prototype; Textarea.prototype.type = 'textarea'; -// TODO: Optimize this. Remember cursor coords. -// Optimize to use CUU, CUD, CUB, CUF. Textarea.prototype.updateCursor = function() { - if (this.screen.focused !== this) return; + if (this.screen.focused !== this) { + return; + } - var last = this._clines[this._clines.length-1]; + var last = this._clines[this._clines.length-1] + , program = this.screen.program + , line + , cx + , cy; // Stop a situation where the textarea begins scrolling // and the last cline appears to always be empty from the @@ -2526,13 +2533,43 @@ Textarea.prototype.updateCursor = function() { last = this._clines[this._clines.length-2] || ''; } - var line = Math.min( + line = Math.min( this._clines.length - 1 - this.childBase, this.height - (this.border ? 2 : 0) - 1); - this.screen.program.cup( - this.top + 1 + (this.border ? 1 : 0) + line, - this.left + 1 + (this.border ? 1 : 0) + last.length); + cy = this.top + 1 + (this.border ? 1 : 0) + line; + cx = this.left + 1 + (this.border ? 1 : 0) + last.length; + + if (cy === this.cy && cx === this.cx) { + return; + } + + if (cy === this.cy) { + if (cx > this.cx) { + program.cuf(cx - this.cx); + } else if (cx < this.cx) { + program.cub(this.cx - cx); + } + } else if (cx === this.cx) { + if (cy > this.cy) { + if (cy - this.cy === 1) { + program.ind(); + } else { + program.cud(cy - this.cy); + } + } else if (cy < this.cy) { + if (this.cy - cy === 1) { + program.ri(); + } else { + program.cuu(this.cy - cy); + } + } + } else { + program.cup(cy, cx); + } + + this.cx = cx; + this.cy = cy; }; Textarea.prototype.input = @@ -2548,6 +2585,9 @@ Textarea.prototype.setInput = function(callback) { this.screen.grabKeys = true; + this.cx = -1; + this.cy = -1; + this.updateCursor(); this.screen.program.showCursor(); this.screen.program.sgr('normal');