Permalink
Browse files

Throbber

  • Loading branch information...
1 parent e824f4e commit f5d7a61e95d9f9582f0a4038bcd3449e964a73c9 @medikoo committed Jul 23, 2011
Showing with 38 additions and 0 deletions.
  1. +38 −0 lib/throbber.js
View
@@ -0,0 +1,38 @@
+'use strict';
+
+var write = process.stdout.write.bind(process.stdout)
+ , sequence = require('es5-ext/lib/Function/sequence')
+
+ , chars = '-\\|/'
+ , l = chars.length
+ , p;
+
+p = {
+ next: 0,
+ write: write,
+ throbbed: false,
+ ontick: function () {
+ if (this.throbbed) {
+ write('\u0008');
+ } else {
+ this.throbbed = true;
+ }
+ this.write(chars[this.next++%l]);
+ },
+ onstop: function () {
+ if (this.throbbed) {
+ write('\u0008');
+ this.next = 0;
+ this.throbbed = false;
+ }
+ }
+};
+
+module.exports = function (interval, formatting) {
+ var o = Object.create(p);
+ if (formatting) {
+ o.write = sequence(formatting, write);
+ }
+ interval.on('tick', o.ontick.bind(o));
+ interval.on('stop', o.onstop.bind(o));
+};

0 comments on commit f5d7a61

Please sign in to comment.