[Pkg-javascript-commits] [node-keypress] 13/14: Imported Upstream version 0.2.1

Mike Gabriel sunweaver at debian.org
Wed Aug 20 12:03:39 UTC 2014


This is an automated email from the git hooks/post-receive script.

sunweaver pushed a commit to branch master
in repository node-keypress.

commit 324bb33f5830da2145eb332c1c2c601e415256a5
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date:   Wed Aug 20 13:41:33 2014 +0200

    Imported Upstream version 0.2.1
---
 index.js     | 142 ++++++++++++++++++++++++++++++++++++++++++-----------------
 package.json |   2 +-
 2 files changed, 103 insertions(+), 41 deletions(-)

diff --git a/index.js b/index.js
index c2ba488..5539dd6 100644
--- a/index.js
+++ b/index.js
@@ -1,42 +1,51 @@
 
 /**
- * This module offers the internal "keypress" functionality from node-core's
- * `readline` module, for your own programs and modules to use.
- *
- * Usage:
- *
- *   require('keypress')(process.stdin);
- *
- *   process.stdin.on('keypress', function (ch, key) {
- *     console.log(ch, key);
- *     if (key.ctrl && key.name == 'c') {
- *       process.stdin.pause();
- *     }
- *   });
- *   proces.stdin.resume();
+ * Module dependencies.
  */
-var exports = module.exports = keypress;
 
-exports.enableMouse = function (stream) {
-  stream.write('\x1b' +'[?1000h')
-}
+var EventEmitter = require('events').EventEmitter;
 
-exports.disableMouse = function (stream) {
-  stream.write('\x1b' +'[?1000l')
-}
+/**
+ * Module exports.
+ */
 
+var exports = module.exports = keypress;
 
 /**
- * accepts a readable Stream instance and makes it emit "keypress" events
+ * This module offers the internal "keypress" functionality from node-core's
+ * `readline` module, for your own programs and modules to use.
+ *
+ * The `keypress` function accepts a readable Stream instance and makes it
+ * emit "keypress" events.
+ *
+ * Usage:
+ *
+ * ``` js
+ * require('keypress')(process.stdin);
+ *
+ * process.stdin.on('keypress', function (ch, key) {
+ *   console.log(ch, key);
+ *   if (key.ctrl && key.name == 'c') {
+ *     process.stdin.pause();
+ *   }
+ * });
+ * proces.stdin.resume();
+ * ```
+ *
+ * @param {Stream} stream
+ * @api public
  */
 
 function keypress(stream) {
   if (isEmittingKeypress(stream)) return;
-  stream._emitKeypress = true;
+
+  var StringDecoder = require('string_decoder').StringDecoder; // lazy load
+  stream._keypressDecoder = new StringDecoder('utf8');
 
   function onData(b) {
-    if (stream.listeners('keypress').length > 0) {
-      emitKey(stream, b);
+    if (listenerCount(stream, 'keypress') > 0) {
+      var r = stream._keypressDecoder.write(b);
+      if (r) emitKey(stream, r);
     } else {
       // Nobody's watching anyway
       stream.removeListener('data', onData);
@@ -51,7 +60,7 @@ function keypress(stream) {
     }
   }
 
-  if (stream.listeners('keypress').length > 0) {
+  if (listenerCount(stream, 'keypress') > 0) {
     stream.on('data', onData);
   } else {
     stream.on('newListener', onNewListener);
@@ -61,31 +70,78 @@ function keypress(stream) {
 /**
  * Returns `true` if the stream is already emitting "keypress" events.
  * `false` otherwise.
+ *
+ * @param {Stream} stream readable stream
+ * @return {Boolean} `true` if the stream is emitting "keypress" events
+ * @api private
  */
 
 function isEmittingKeypress(stream) {
-  var rtn = stream._emitKeypress;
+  var rtn = !!stream._keypressDecoder;
   if (!rtn) {
-    // hack: check for the v0.6.x "data" event
-    stream.listeners('data').forEach(function (l) {
+    // XXX: for older versions of node (v0.6.x, v0.8.x) we want to remove the
+    // existing "data" and "newListener" keypress events since they won't include
+    // this `keypress` module extensions (like "mousepress" events).
+    stream.listeners('data').slice(0).forEach(function(l) {
       if (l.name == 'onData' && /emitKey/.test(l.toString())) {
-        rtn = true;
-        stream._emitKeypress = true;
+        stream.removeListener('data', l);
       }
     });
-  }
-  if (!rtn) {
-    // hack: check for the v0.6.x "newListener" event
-    stream.listeners('newListener').forEach(function (l) {
+    stream.listeners('newListener').slice(0).forEach(function(l) {
       if (l.name == 'onNewListener' && /keypress/.test(l.toString())) {
-        rtn = true;
-        stream._emitKeypress = true;
+        stream.removeListener('newListener', l);
       }
     });
   }
   return rtn;
 }
 
+/**
+ * Enables "mousepress" events on the *input* stream. Note that `stream` must be
+ * an *output* stream (i.e. a Writable Stream instance), usually `process.stdout`.
+ *
+ * @param {Stream} stream writable stream instance
+ * @api public
+ */
+
+exports.enableMouse = function (stream) {
+  stream.write('\x1b[?1000h');
+};
+
+/**
+ * Disables "mousepress" events from being sent to the *input* stream.
+ * Note that `stream` must be an *output* stream (i.e. a Writable Stream instance),
+ * usually `process.stdout`.
+ *
+ * @param {Stream} stream writable stream instance
+ * @api public
+ */
+
+exports.disableMouse = function (stream) {
+  stream.write('\x1b[?1000l');
+};
+
+/**
+ * `EventEmitter.listenerCount()` polyfill, for backwards compat.
+ *
+ * @param {Emitter} emitter event emitter instance
+ * @param {String} event event name
+ * @return {Number} number of listeners for `event`
+ * @api public
+ */
+
+var listenerCount = EventEmitter.listenerCount;
+if (!listenerCount) {
+  listenerCount = function(emitter, event) {
+    return emitter.listeners(event).length;
+  };
+}
+
+
+///////////////////////////////////////////////////////////////////////
+// Below this function is code from node-core's `readline.js` module //
+///////////////////////////////////////////////////////////////////////
+
 
 /*
   Some patterns seen in terminal key escape codes, derived from combos seen
@@ -141,8 +197,12 @@ function emitKey(stream, s) {
 
   key.sequence = s;
 
-  if (s === '\r' || s === '\n') {
-    // enter
+  if (s === '\r') {
+    // carriage return
+    key.name = 'return';
+
+  } else if (s === '\n') {
+    // enter, should have been called linefeed
     key.name = 'enter';
 
   } else if (s === '\t') {
@@ -305,6 +365,8 @@ function emitKey(stream, s) {
     return;
   }
 
+  // XXX: this "mouse" parsing code is NOT part of the node-core standard
+  // `readline.js` module, and is a `keypress` module non-standard extension.
   if (key.code == '[M') {
     key.name = 'mouse';
     var s = key.sequence;
@@ -339,7 +401,7 @@ function emitKey(stream, s) {
   }
 
   if (key && key.name == 'mouse') {
-    stream.emit('mousepress', key)
+    stream.emit('mousepress', key);
   } else if (key || ch) {
     stream.emit('keypress', ch, key);
   }
diff --git a/package.json b/package.json
index a527801..9673347 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "keypress",
-  "version": "0.1.0",
+  "version": "0.2.1",
   "description": "Make any Node ReadableStream emit \"keypress\" events",
   "author": "Nathan Rajlich <nathan at tootallnate.net> (http://tootallnate.net)",
   "main": "index.js",

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-keypress.git



More information about the Pkg-javascript-commits mailing list