Created
June 22, 2013 20:22
-
-
Save jhaynie/5842435 to your computer and use it in GitHub Desktop.
Console based spinner module in Node.js -- adapted from code in mocha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sprintf = require('util').format, | |
timer; | |
function play(arr, interval) { | |
var len = arr.length | |
, interval = interval || 100 | |
, i = 0; | |
timer = setInterval(function(){ | |
var str = arr[i++ % len]; | |
process.stdout.write('\u001b[0G' + str); | |
}, interval); | |
} | |
var spinner = 'win32' == process.platform | |
? ['|','/','-','\\'] | |
: ['◜','◠','◝','◞','◡','◟']; | |
exports.start = function(msg) { | |
msg = msg || ''; | |
var frames = spinner.map(function(c) { | |
return sprintf(' \u001b[96m%s \u001b[90m'+msg+'\u001b[0m', c); | |
}); | |
play(frames); | |
}; | |
exports.stop = function() { | |
if (timer){ | |
clearInterval(timer); | |
timer=null; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var spinner = require('./spinner'); | |
spinner.start('waiting'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment