Created
July 12, 2013 01:38
-
-
Save wlaurance/5980789 to your computer and use it in GitHub Desktop.
disco http charm bar
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 charm = require('charm')(), | |
| http = require('http'); | |
| charm.pipe(process.stdout); | |
| charm.write('\n'); | |
| charm.cursor(false); | |
| var options = { | |
| hostname: 'nodejs.org', | |
| port: 80, | |
| path: '/dist/v0.10.13/node-v0.10.13.tar.gz', | |
| method: 'GET' | |
| }; | |
| var req = http.request(options); | |
| req.on('response', function(res){ | |
| var length = res.headers['content-length'], | |
| count = 0, | |
| prev = ''; | |
| res.on('data', function(chunk){ | |
| charm.left(prev.length); | |
| count += chunk.length; | |
| prev = count + '/' + length; | |
| charm.foreground(Math.floor((count / length) * 255)); | |
| charm.write(prev); | |
| }); | |
| res.on('end', function(){ | |
| charm.write('\n'); | |
| charm.cursor(true); | |
| }); | |
| }); | |
| req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment