From 070cbd900105d5aacc585cf736218002803f2671 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 13 Aug 2013 11:30:44 -0500 Subject: [PATCH] decode mouse events and responses properly. --- lib/program.js | 31 ++++++++++++++++++++++++------- test/program-mouse.js | 1 + 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/program.js b/lib/program.js index fec7f4a..0b28036 100644 --- a/lib/program.js +++ b/lib/program.js @@ -9,6 +9,7 @@ */ var EventEmitter = require('events').EventEmitter + , StringDecoder = require('string_decoder').StringDecoder , util = require('util') , Tput = require('./tput') , colors = require('./colors') @@ -105,9 +106,7 @@ Program.prototype._log = function(pre, msg) { Program.prototype.setupDump = function() { var self = this , write = this.output.write - , decoder; - - decoder = new (require('string_decoder')).StringDecoder('utf8'); + , decoder = new StringDecoder('utf8'); function stringify(data) { return caret(data @@ -349,8 +348,17 @@ Program.prototype.removeKey = function(key, listener) { // left click: ^[[M 3<^[[M#3< // mousewheel up: ^[[M`3> Program.prototype.bindMouse = function() { - this.on('data', this._bindMouse.bind(this)); - this.bindMouse = function() {}; + if (this._boundMouse) return; + this._boundMouse = true; + + var decoder = new StringDecoder('utf8') + , self = this; + + this.on('data', function(data) { + data = decoder.write(data); + if (!data) return; + self._bindMouse(data); + }); }; Program.prototype._bindMouse = function(s) { @@ -611,8 +619,17 @@ Program.prototype._bindMouse = function(s) { // All possible responses from the terminal Program.prototype.bindResponse = function() { - this.on('data', this._bindResponse.bind(this)); - this.bindResponse = function() {}; + if (this._boundResponse) return; + this._boundResponse = true; + + var decoder = new StringDecoder('utf8') + , self = this; + + this.on('data', function(data) { + data = decoder.write(data); + if (!data) return; + self._bindResponse(data); + }); }; Program.prototype._bindResponse = function(s) { diff --git a/test/program-mouse.js b/test/program-mouse.js index 0028d81..b5daa0f 100644 --- a/test/program-mouse.js +++ b/test/program-mouse.js @@ -30,6 +30,7 @@ program.key(['q', 'escape', 'C-c'], function() { }); program.on('keypress', function(ch, data) { + if (data.name === 'mouse') return; program.clear(); program.cup(0, 0); console.log(data);