Skip to content

Instantly share code, notes, and snippets.

@vsemozhetbyt
Last active January 28, 2016 02:50
Show Gist options
  • Save vsemozhetbyt/388ef5995aa645852963 to your computer and use it in GitHub Desktop.
Save vsemozhetbyt/388ef5995aa645852963 to your computer and use it in GitHub Desktop.
'use strict';
/******************************************************************************/
const hrstart = process.hrtime();
const fs = require('fs');
const readline = require('readline');
const getByteLength = Buffer.byteLength;
const inputPath = process.argv[2];
const outputPath = process.argv[3];
if (!inputPath || !outputPath) {
console.error('Enter the input and output file paths, please.');
process.exit();
}
try {
fs.accessSync(inputPath);
} catch(e) {
console.error('Input file not found.');
process.exit();
}
const fileSize = fs.statSync(inputPath)['size'];
let lineCount = 0;
let byteCount = 0;
const parser = readline.createInterface({
input: fs.createReadStream(inputPath, 'utf8'),
terminal: false,
historySize: 0
});
const outputFile = fs.openSync(outputPath, 'w');
/******************************************************************************/
fs.writeSync(outputFile, '\uFEFF', null, 'utf8');
console.log('');
const updater = setInterval(updateProgressBar, 500);
/******************************************************************************/
parser.on('line', line => {
lineCount++;
if (lineCount === 1) line = line.replace(/^\uFEFF/, '');
byteCount += getByteLength(line, 'utf8') + 1;
fs.writeSync(outputFile, `${line}\n`, null, 'utf8');
}).on('close', () => {
clearInterval(updater);
updateProgressBar();
const hrend = process.hrtime(hrstart);
console.log(
`\n\nExecution time: ${ hrend[0] }.${ Math.floor(hrend[1]/1000000) }\n`
);
});
/******************************************************************************/
function updateProgressBar() {
const readPercent = Math.ceil((byteCount / fileSize) * 100);
if (readPercent > 100) readPercent = 100;
const barsNumber = Math.floor(readPercent / 2);
const padsNumber = 50 - barsNumber;
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 0);
process.stdout.write(
`${ '█'.repeat(barsNumber) }${ ' '.repeat(padsNumber) } ${readPercent}%`
);
}
/******************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment