Skip to content

Instantly share code, notes, and snippets.

@vsemozhetbyt

vsemozhetbyt/paginate.js

Last active Jan 17, 2016
Embed
What would you like to do?
'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