Created
October 3, 2021 12:49
-
-
Save panna-ahmed/1989faa340538b47236674f4dc0eaea2 to your computer and use it in GitHub Desktop.
compare texts horizontally
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
const fs = require('fs') | |
let lineWidth = 0; | |
let betweenCharacters = 2; | |
let allTexts = []; | |
process.argv.forEach((val, index) => { | |
//console.log(val); | |
if(val.endsWith('.txt')){ | |
//console.log(val); | |
try { | |
const data = fs.readFileSync(val, 'utf8'); | |
let lines = data.split("\n"); | |
var longest = lines.sort( | |
function (a, b) { | |
return b.length - a.length; | |
} | |
)[0]; | |
allTexts.push({ data, startAt: 0, maxLineWidth: longest.length}); | |
} catch (err) { | |
console.error(err) | |
} | |
} | |
else if(val == '-s') | |
{ | |
lineWidth = Number(process.argv[index+1]); | |
} | |
else if(val == '-c') | |
{ | |
betweenCharacters = Number(process.argv[index+1]); | |
} | |
}); | |
const isBelowThreshold = (currentValue) => currentValue.startAt >= currentValue.data.length; | |
let allLines = ''; | |
while(true){ | |
if(allTexts.every(isBelowThreshold)) break; | |
let oneLine = ''; | |
for(let i = 0; i < allTexts.length; i++){ | |
let t = allTexts[i]; | |
let ol = t.data.slice(t.startAt, t.startAt + (lineWidth == 0 ? t.maxLineWidth : lineWidth)); | |
let nIndex = ol.indexOf('\n'); | |
if(nIndex == 0 && t.data[t.startAt - 1] != '\n'){ | |
t.startAt += 1; | |
i--; | |
continue; | |
} | |
if(nIndex >= 0) | |
{ | |
ol = ol.slice(0, nIndex) + ' '.repeat((lineWidth == 0 ? t.maxLineWidth : lineWidth) - nIndex); | |
t.startAt += nIndex + 1; | |
} | |
else | |
{ | |
t.startAt += ol.length; | |
ol += ' '.repeat((lineWidth == 0 ? t.maxLineWidth : lineWidth) - ol.length); | |
} | |
if(i != allTexts.length -1) | |
ol += ' '.repeat(betweenCharacters); | |
oneLine += ol; | |
}; | |
allLines += oneLine + '\n'; | |
} | |
process.stdout.write(allLines); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment