├── .gitignore ├── LICENSE ├── README.md ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # single-line-stream 2 | 3 | Basically [single-line-log](https://github.com/freeall/single-line-log) by @freeall except this is a through stream 4 | 5 | ``` 6 | npm install single-line-stream 7 | ``` 8 | 9 | ## Usage 10 | 11 | ``` js 12 | var log = require('single-line-stream') 13 | var fs = require('fs') 14 | 15 | fs.createReadStream('/dev/urandom', {highWaterMark:16, encoding:'hex'}) 16 | .pipe(log()) 17 | .pipe(process.stdout) 18 | ``` 19 | 20 | The above example will print out a random hex string that updates inplace in your 21 | terminal. 22 | 23 | ## License 24 | 25 | MIT 26 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var log = require('single-line-stream') 2 | var fs = require('fs') 3 | 4 | fs.createReadStream('/dev/urandom', {highWaterMark:16, encoding:'hex'}) 5 | .pipe(log()) 6 | .pipe(process.stdout) 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require('through2') 2 | 3 | var MOVE_LEFT = new Buffer('1b5b3130303044', 'hex').toString() 4 | var MOVE_UP = new Buffer('1b5b3141', 'hex').toString() 5 | var CLEAR_LINE = new Buffer('1b5b304b', 'hex').toString() 6 | 7 | module.exports = function() { 8 | var prev 9 | return through(function(data, enc, cb) { 10 | var prefix 11 | 12 | if (prev) { 13 | var reset = '' 14 | var newlines = 0 15 | 16 | for (var i = 0; i < prev.length; i++) { 17 | if (prev[i] === 10) newlines++ 18 | } 19 | 20 | for (var i = 0; i < newlines+1; i++) { 21 | reset += MOVE_LEFT + CLEAR_LINE + (i < newlines ? MOVE_UP : '') 22 | } 23 | 24 | prefix = new Buffer(reset) 25 | } 26 | 27 | prev = data 28 | cb(null, prefix ? Buffer.concat([prefix, data]) : data) 29 | }) 30 | } 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-line-stream", 3 | "version": "1.1.0", 4 | "description": "Basically single-line-log by @freeall except this is a through stream", 5 | "main": "index.js", 6 | "dependencies": { 7 | "through2": "^2.0.0" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/mafintosh/single-line-stream" 12 | }, 13 | "keywords": [ 14 | "single", 15 | "line", 16 | "stream", 17 | "log", 18 | "ansi", 19 | "progress" 20 | ], 21 | "author": "Mathias Buus", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/mafintosh/single-line-stream/issues" 25 | }, 26 | "homepage": "https://github.com/mafintosh/single-line-stream" 27 | } 28 | --------------------------------------------------------------------------------