Permalink
Browse files

add waiting animations

  • Loading branch information...
aheckmann committed Feb 9, 2013
1 parent 25d096b commit 3a9ce40017377fa3f38b6a391714ceb11f7e37d2
Showing with 136 additions and 3 deletions.
  1. +58 −1 lib/db.js
  2. +1 −0 lib/p.js
  3. +77 −2 lib/term.js
View
@@ -7,6 +7,7 @@ var mongodb = require('mongodb')
var slice = require('sliced')
var log = require('./log')
var p = require('./p')
+var noop = function(){}
/**
* Expose
@@ -34,7 +35,7 @@ function createConstructor (db) {
var collections = [];
function DB () {
- this.cols(false);
+ this.cols(false, noop);
}
/**
@@ -82,6 +83,7 @@ function createConstructor (db) {
});
});
}).help = help("Drops the database");
+ wrap(DB.prototype, 'drop');
/**
* Close this database connection
@@ -96,6 +98,7 @@ function createConstructor (db) {
db.close(true, cb);
}).help = help("Closes the database connection");
+ wrap(DB.prototype, 'close');
/**
* Open the database connection
@@ -114,6 +117,7 @@ function createConstructor (db) {
}
});
}).help = help("Opens the database connection")
+ wrap(DB.prototype, 'open');
/**
* Use a different database
@@ -179,6 +183,7 @@ function createConstructor (db) {
});
}).help = help("Creates a collection")
+ wrap(DB.prototype, 'createCol');
/**
* Refresh and return the list of collections on this database
@@ -239,6 +244,7 @@ function createConstructor (db) {
}
});
}).help = help("Retreives an array of collection names in the db")
+ wrap(DB.prototype, 'cols');
/**
* Execute a command on the database
@@ -273,6 +279,21 @@ function createConstructor (db) {
db[method](cmd, opts, cb);
}).help = help("Runs a command on the database")
+ wrap(DB.prototype, 'runCommand');
+
+ /**
+ * Retreive database stats
+ */
+
+ ;(DB.prototype.stats = function (scale, cb) {
+ if ('function' == typeof scale) cb = scale;
+ scale |= 0;
+
+ db.stats(function (err, stats) {
+ cb(err, stats);
+ })
+ }).help = help('Retreive database stats');
+ wrap(DB.prototype, 'stats');
/**
* console.log helper
@@ -286,6 +307,42 @@ function createConstructor (db) {
}
+/**
+ * Wrap async functions with animation etc
+ */
+
+function wrap (proto, name) {
+ var old = proto[name];
+
+ proto[name] = function () {
+ if (global.repl) global.repl.bufferStart();
+ var args = slice(arguments);
+ var last = args[args.length-1];
+ if ('function' == typeof last) {
+ args[args.length-1] = function () {
+ if (global.repl) global.repl.bufferEnd()
+ if (p != last) console.log();
+ last.apply(null, arguments)
+ if (global.repl) {
+ global.repl.displayPrompt();
+ global.repl.moveCursorToEnd();
+ }
+ }
+ } else {
+ args.push(function () {
+ if (global.repl) global.repl.bufferEnd()
+ p.apply(null, arguments);
+ if (global.repl) global.repl.moveCursorToEnd();
+ });
+ }
+ old.apply(this, args);
+ }
+
+ if (old.help) {
+ proto[name].help = old.help;
+ }
+}
+
/**
* Error reporting helper
*/
View
@@ -14,6 +14,7 @@ module.exports = exports = function p () {
console.log();
slice(arguments).forEach(function (arg, i) {
var out = util.inspect(arg, false, 100, true);
+ global.repl.context._ = arg;
if (i === 0) {
console.log('error: ', out);
} else {
View
@@ -1,14 +1,20 @@
var repl = require('repl')
+var vm = require('vm')
+var readline = require('readline')
var log = require('./log')
+var debug = require('./debug')
+var GLOBAL = true; // false?
+var term;
module.exports = exports = function (db) {
// start off with an empty prompt so start up logging/debug msgs
// are written without interuption
- var term = repl.start({
+ term = repl.start({
prompt: ''
- , useGlobal: true
+ , useGlobal: GLOBAL
, ignoreUndefined: true
+ , eval: eval_
});
// overwrite the empty prompt line
@@ -20,5 +26,74 @@ module.exports = exports = function (db) {
db.close(process.exit.bind(process));
})
+ term.bufferStart = function () {
+ debug('start buffering')
+ animation.start();
+ }
+
+ term.bufferEnd = function () {
+ debug('end buffering')
+ animation.end();
+ }
+
+ term.moveCursorToEnd = function () {
+ term.rli._moveCursor(+Infinity);
+ }
+
return term;
}
+
+var animation = (function(){
+ // overwrite user input every 20ms
+ // change chars every 200ms
+ var chars = ['-','\\','|','/']
+ var interval = 20;
+ var group = 100
+ var timer;
+ var delay;
+ var i = -1;
+
+ function start () {
+ delay = setTimeout(function(){
+ var char;
+ var change = group / interval;
+ timer = setInterval((function frame () {
+ if (0 === ++i % change) {
+ char = chars[i % chars.length];
+ }
+ readline.clearLine(term.outputStream);
+ readline.cursorTo(term.outputStream, 0);
+ term.outputStream.write(char);
+ return frame;
+ })(), interval);
+ }, 10);
+ }
+
+ function end () {
+ // overwrite the empty prompt line
+ process.stdout.write('\x1b[1A');
+ clearTimeout(delay);
+ clearInterval(timer);
+ timer = delay = null;
+ i = -1;
+ }
+
+ return {
+ start: start
+ , end: end
+ }
+})();
+
+function eval_ (code, context, file, cb) {
+ var err, res;
+ try {
+ if (GLOBAL) {
+ res = vm.runInThisContext(code, file);
+ } else {
+ res = vm.runInNewContext(code, context, file);
+ }
+ } catch (e) {
+ err = e;
+ }
+ cb(err, res);
+}

0 comments on commit 3a9ce40

Please sign in to comment.