Permalink
Browse files

initial passing test just testing \n, \r, and \f

  • Loading branch information...
0 parents commit bfd77088cdf9cc0d4212f83c200b7948314f1957 @substack committed Sep 5, 2011
Showing with 151 additions and 0 deletions.
  1. +83 −0 index.js
  2. +39 −0 package.json
  3. +29 −0 test/lines.js
@@ -0,0 +1,83 @@
+var binary = require('binary');
+var EventEmitter = require('events').EventEmitter;
+
+module.exports = function (stream, width) {
+ if (width === undefined) width = 80;
+
+ function decode (buf) {
+ console.dir(buf);
+ }
+
+ var emit = (function () {
+ var nt = false;
+ return function () {
+ if (nt === false) {
+ nt = true;
+ process.nextTick(function () {
+ nt = false;
+ pos.emit('pos', pos.x, pos.y);
+ });
+ }
+ };
+ })();
+
+ binary(stream).loop(function (end) {
+ this
+ .word8('c')
+ .tap(function (vars) {
+ if (vars.c === '\n'.charCodeAt(0)) {
+ pos.y ++;
+ pos.x = 0;
+ emit();
+ }
+ else if (vars.c === '\r'.charCodeAt(0)) {
+ pos.x = 0;
+ emit();
+ }
+ else if (vars.c === '\f'.charCodeAt(0)) {
+ pos.y ++;
+ emit();
+ }
+ else if (vars.c === 0x1b) { // escape
+ this
+ .word8('x')
+ .tap(function (vars) {
+ if (vars.x === '['.charCodeAt(0)) {
+ this.loop(nextAlpha('values')).tap(function (vars) {
+ decode(vars.values);
+ });
+ }
+ })
+ ;
+ }
+ else {
+ pos.x ++;
+ emit();
+ }
+ })
+ ;
+ });
+
+ function nextAlpha (name) {
+ var values = [];
+
+ return function (end) {
+ this
+ .get('__nextChar')
+ .tap(function (vars) {
+ var c = vars.__nextChar;
+ values.push(c);
+ if (String.fromCharCode(c).match(/[A-Za-z]/)) {
+ vars[name] = new Buffer(values);
+ end();
+ }
+ })
+ ;
+ }
+ }
+
+ var pos = new EventEmitter;
+ pos.x = 0;
+ pos.y = 0;
+ return pos;
+};
@@ -0,0 +1,39 @@
+{
+ "name" : "cursory",
+ "version" : "0.0.0",
+ "description" : "compute the relative cursor position on a terminal stream",
+ "main" : "index.js",
+ "directories" : {
+ "lib" : ".",
+ "example" : "example",
+ "test" : "test"
+ },
+ "dependencies" : {
+ "binary" : ">=0.2.5 <0.3"
+ },
+ "devDependencies" : {
+ "tap" : "0.0.x"
+ },
+ "scripts" : {
+ "test" : "tap test"
+ },
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/substack/node-cursory.git"
+ },
+ "keywords" : [
+ "terminal",
+ "ansi",
+ "cursor",
+ "console",
+ "escape",
+ "sequence"
+ ],
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail@substack.net",
+ "url" : "http://substack.net"
+ },
+ "license" : "MIT/X11",
+ "engine" : { "node" : ">=0.4" }
+}
@@ -0,0 +1,29 @@
+var test = require('tap').test;
+var EventEmitter = require('events').EventEmitter;
+var cursory = require('../');
+var seq = require('seq');
+
+test('lines', function (t) {
+ var stream = new EventEmitter;
+ var pos = cursory(stream);
+
+ seq()
+ .seq(function () { pos.once('pos', this.ok) })
+ .seq(function () { setTimeout(this.ok, 10) })
+ .seq(function () {
+ t.equal(pos.x, 3);
+ t.equal(pos.y, 0);
+ stream.emit('data', new Buffer('\n'));
+ this();
+ })
+ .seq(function () { pos.once('pos', this.ok) })
+ .seq(function () { setTimeout(this.ok, 10) })
+ .seq(function () {
+ t.equal(pos.x, 0);
+ t.equal(pos.y, 1);
+ stream.emit('end');
+ t.end();
+ })
+ ;
+ stream.emit('data', new Buffer('abc'));
+});

0 comments on commit bfd7708

Please sign in to comment.