Last active
January 17, 2016 14:50
-
-
Save vsemozhetbyt/8876406e1fdde02dc412 to your computer and use it in GitHub Desktop.
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
| 'use strict'; | |
| /******************************************************************************/ | |
| const path = process.argv[2]; | |
| let linesPerPage = Number(process.argv[3]); | |
| if (!path || !linesPerPage) { | |
| console.log('Add the file path and lines per page number, please.'); | |
| process.exit(); | |
| } | |
| linesPerPage -= 2; | |
| /******************************************************************************/ | |
| const fs = require('fs'); | |
| const S = require('string'); | |
| const encoding = 'utf8'; //ascii utf8 utf16le | |
| const rl = require('readline').createInterface({ | |
| input: fs.createReadStream(path, encoding), | |
| terminal: false, | |
| historySize: 0 | |
| }); | |
| const outfile = fs.openSync(path.replace(/(\.[^.]+)?$/, '.paginated$1'), 'w'); | |
| let lines = 0; | |
| let page = 0; | |
| /******************************************************************************/ | |
| rl.on('line', line => { | |
| fs.writeSync(outfile, `${line}\n`, null, encoding); | |
| lines++; | |
| if (lines === linesPerPage) { | |
| fs.writeSync(outfile, | |
| `\n${S(++page).pad(80).s.trimRight()}\n`, null, encoding); | |
| lines = 0; | |
| } | |
| }).on('close', () => { | |
| if (lines !== 0) { | |
| fs.writeSync(outfile, | |
| `${'\n'.repeat(linesPerPage - lines + 1)}${S(++page).pad(80).s.trimRight()}`, | |
| null, encoding); | |
| } | |
| }); | |
| /******************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment